Simple, Quality, Awesome Software

Remainders

Below is the complete code for the Remainders Try It Out! problem.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CSharpBook.Examples.Remainders
{
    class Program
    {
        static void Main(string[] args)
        {
            // The basic remainder math.
            int a = 17;
            int b = 2;
            int quotient = a / b;
            int remainder = a % b;

            // Pick any of the following three options...

            // Print the pieces out one at a time. This is kind of wordy, but
            // does things one at a time, which may make the most sense, if you're
            // just getting started.
            Console.Write(a);
            Console.Write("/");
            Console.Write(b);
            Console.Write(" is ");
            Console.Write(quotient);
            Console.Write(" remainder ");
            Console.Write(remainder);
            Console.WriteLine(".");

            // Combines it all into one statement using the + operator to add
            // strings together.
            Console.WriteLine(a + "/" + b + " is " + quotient +
                        " remainder " + remainder + ".");

            // This last approach is more sophisticated, but if you've done a
            // bunch of programming before, you may prefer it. This uses a version
            // of the WriteLine method that we haven't talked about yet, which has
            // a variable number of arguments, which are substituted in.
            // This allows you to separate your formatting from your data, which
            // follows the principle of separation of concerns.
            Console.WriteLine("{0}/{1} is {2} remainder {3}.",
                        a, b, quotient, remainder);

            Console.ReadKey();
        }
    }
}