C# Tutorial C# Advanced C# References

C# - arithmetic operators example



The example below shows the usage of arithmetic operators - addition(+), subtraction(-), multiply(*), division(/) and modulo(%) operators.

using System;

class MyProgram {
  static void Main(string[] args) {
    float a = 25;
    float b = 10;

    Console.WriteLine("a = "+a+", b = "+b+"\n");

    //Add a and b
    float result_add = a + b;
    Console.WriteLine("a + b = "+result_add);

    //Subtract b from a
    float result_sub = a - b;
    Console.WriteLine("a - b = "+result_sub);

    //Multiply a and b
    float result_mul = a * b;
    Console.WriteLine("a * b = "+result_mul);

    //Divide a by b
    float result_div = a / b;
    Console.WriteLine("a / b = "+result_div);

    //return division remainder 
    float result_modulo = a % b;
    Console.WriteLine("a % b = "+result_modulo);  
  }
}

The output of the above code will be:

a = 25, b = 10

a + b = 35
a - b = 15
a * b = 250
a / b = 2.5
a % b = 5

❮ C# - Operators