Core C#

Arithmetic Operators

This C# program demonstrates how the arithmetic operators are used. Arithmetic operators are comprised of the four basic arithmetic operations, along with the modulus operator for both integers and floating-point values.

Program.cs

using System;

namespace XoaX {
    class Program {
        static void Main(string[] args) {
            int iN = 11;
            int iM = 3;
            Console.WriteLine("For ints:");
            Console.WriteLine("({0} + {1}) = " + (iN + iM), iN, iM);
            Console.WriteLine("({0} - {1}) = " + (iN - iM), iN, iM);
            Console.WriteLine("({0} * {1}) = " + (iN * iM), iN, iM);
            Console.WriteLine("({0} / {1}) = " + (iN / iM), iN, iM);
            Console.WriteLine("({0} % {1}) = " + (iN % iM), iN, iM);

            double dX = 8.5;
            double dY = 3.2;
            Console.WriteLine("For doubles:");
            Console.WriteLine("({0} + {1}) = " + (dX + dY), dX, dY);
            Console.WriteLine("({0} - {1}) = " + (dX - dY), dX, dY);
            Console.WriteLine("({0} * {1}) = " + (dX * dY), dX, dY);
            Console.WriteLine("({0} / {1}) = " + (dX / dY), dX, dY);
            Console.WriteLine("({0} % {1}) = " + (dX % dY), dX, dY);
        }
    }
}
 

Output

For ints:
(11 + 3) = 14
(11 - 3) = 8
(11 * 3) = 33
(11 / 3) = 3
(11 % 3) = 2
For doubles:
(8.5 + 3.2) = 11.7
(8.5 - 3.2) = 5.3
(8.5 * 3.2) = 27.2
(8.5 / 3.2) = 2.65625
(8.5 % 3.2) = 2.1
Press any key to continue . . .
 
 

© 2007–2024 XoaX.net LLC. All rights reserved.