Learn about arithmetic operators in C# programming i.e. + plus, – minus, * multiplication, /division and % modulo operators with simple example and explanation.

Below are the arithmetic operators in C# language that are used to perform arithmetic operations on primitive data types i.e. int, float and double etc in C#.

• + Plus
• – Minus/Subtraction
• * Multiplication
• / Division
• % Modulus or remainder operator

NOTE:

About Modulus Operator, some of the people may not be understanding % modulus operator. So, here is brief about % MODULUS OPERATOR.

The % operator returns the remainder of two numbers. For example, 10 % 4 is 2 because 10 divided by 4 leaves a remainder of 2.

Another example,

5%2 leaves a remainder of 1.

Code Example on arithmetic operators in C#

In below C# program, two numbers are given 20 and 30 in variables a and b respectively. The program has demonstrated how to perform arithmetic operations
using on two numbers using each arithmetic operators’ and output.

public class Sample
{

public static void Main(String[] args) {

double a = 20;
double b = 10;

Console.WriteLine("first number =" + a);
Console.WriteLine("Second number =" + b);

Console.WriteLine("Arithmetic operations :");
double sum = a + b;
Console.WriteLine("\tSum =" + sum);

// Subtraction (-)
double sub = a - b;
Console.WriteLine("\tSub =" + sub);

// Division (/)
double div = a / b;
Console.WriteLine("\tDiv =" + div);

// Multiplication (*)
double multiplication = a * b;
Console.WriteLine("\tMultiplication =" + multiplication);

// Modulo (*)
double module = a % b;
Console.WriteLine("\tModulo =" + module);

}
}

Output

first number =20
Second number =10
Arithmetic operations :
Sum =30
Sub =10
Div =2
Multiplication =200
Modulo =0

More on Arithmetic operators in C#

Arithmetic operators follow the BODMOS Rule that we have already studied in school. That is perform div, then multiplication, then + or – etc.

C# language follows the same.

For example, expression 2+2/2*2 will result into 4.

C# code:

public class Sample
{

public static void Main(String[] args) {

double result;

result = 2+2/2*2; //Expression

Console.WriteLine("result =" + result);

}
}

Output:
result =4.0

Published inC# Tutorials