Arithmetic Operators in c#

Arithmetic Operators in c#

Arithmetic operators in the C# programming language are used to perform basic mathematical operations, including addition, subtraction, multiplication, division, subtraction, increment, and decrement. Popular arithmetic numerical data operators in C# include +, -, *, /, %, ++, –, etc.

Arithmetic Operators in c#

List of popular arithmetic operators in the C# programming language.

OperatorOperator DetailArithmetic ExampleArithmetic output
+Addition (use to add 2 different integer variable)p + qAdd total of p and q variable value
Subtraction (use to subtract larger number with small number variable)p – qSubtract of p and q variable value
*Multiplication (use to multiply two different number variable)p * qMultiply of p and q variable value
/Division (use to divide smaller number with larger number variable)p / qDivision of p divided by q variable value
%Modulus (Remainder) (use to divide larger number with small number variable if remainder produce then it displays)p % qRemainder of p divided by q variable value
++Increment (use to increment number of variable value)p ++ or ++ pIncreases p variable value by 1
Decrement (use to decrement number of variable value)p — or — pDecreases p variable value by 1

Examples of popular arithmetic operators in the C# programming language.

using System;

class ArithmeticOperator

{

static void Main()

{

int p = 24;

int q = 7;

Console.WriteLine($”The Addition of p & q – {p + q}”); // Result is – 31

Console.WriteLine($”The Subtraction of p & q – {p – q}”); // Result is – 17

Console.WriteLine($”The Multiplication of p & q – {p * q}”); // Result is – 168

Console.WriteLine($”The Division of p & q – {p / q}”); // Result is – 3 (integer division of both variables)

Console.WriteLine($”The Modulus of p & q – {p % q}”); // Result is – 3

p++; //here it Increment variable value by 1

Console.WriteLine($”After Increment value of p – {p}”); // Result is – 25

q–; // Here it decrements the variable value by 1

Console.WriteLine($”After Decrement of value q – {q}”); // Result is – 6

}

}

Important Notes in C# Programming Language.

Behavior of Division Arithmetic Operators.

Integer division (int p = 11 / 4;) in a C# program displays the truncated decimal part of the variable.

Here, use the double or float data type for proper division operations with decimals.

double output = 11.0 / 4.0; // Output is – 2.75…

Post-increment (a++) vs. pre-increment (++a) operator in a C#.

  • Post-increment – In a C# program, the declared program increments the variable value by one using its current value.
  • Pre-increment – In a C# program, a program declares a program variable to increment its value before using it.

Increment Operator Example.

using System;

class IncrementOperator

{

static void Main()

{

int p = 9;

Console.WriteLine(p++); // Result is – 9 (then p variable value becomes 10)

Console.WriteLine(++p); // Result is – 11

}

}

Decrement Operator Example.

using System;

class DecreamentOperator

{

static void Main()

{

int p = 9;

Console.WriteLine(p–); // Result is – 9 (then p becomes 8)

Console.WriteLine(–p); // Result is – 7 (p is first decremented to 7)

}

}

Uses of the modulus operator (%) in C#.

In C# programs, the reminder operator is often used to test divisibility or a condition within a variable’s range. If the value is completely divisible by 3, the reminder will not fire and the output will be true; otherwise, the output will be false.

Modulus operator (%) example.

using System;

class Remainder

{

static void Main()

{

Console.WriteLine(11 % 3 == 0); // Result is – False here (11 is not completely divisible by 3)

}

}

Leave a Reply