C# – What are the Arithmetic Operators?

An operator takes one or more operands to perform particular operation. C# provides below Arithmetic Operators to deal with different types of operations on numerical values.

Addition operator (+)

Addition operator is to add the values of two or more operands; the operands may be constants, named variables or an expression. Below is an example; 

int a = 666;
float f = 4.6692;

Console.WriteLine(a + f);

Subtraction operator (-)

Subtraction operator is used apply subtraction on numerical expressions. This operator will subtract the value of right side operand from left side operand; and the operands may be named variables, expressions or constants.

For example;

Console.WriteLine(3.1415926535 - 2.7182818284);

Multiplication operator (*)

This operator multiplies the values of two operands. You can use constants, named variables or expressions as an operands.

For example;

Console.WriteLine(1.41421 * 2.5029);

Division operator (/)

A division operator is used to divide one operand by another operand.

For example;

Console.WriteLine(8 / 2);

Let’s take another example;

Console.WriteLine(1 / 2);

Are you surprised with the result of above statement? It produces the result “0”. But we are expecting the result “0.5”. Why it is showing “0” instead of “0.5”? You need to learn bit more about this operator.

There are two types of divisions this operator usually do; one is integer division and the other one is floating-point division.

When it do integer division, it simply ignores the fraction part; hence you are seeing the value “0”. To display the fraction part, you need to use the floating-point division.

How to instruct division operator to use floating-point division? It is simple. You start using floating-point numbers. Above statement we can write its as;

Console.WriteLine(1 / 2.0);

Remainder operator (%)

It is used to compute the remainder of the division of two operands. Here is an example;

Console.WriteLine(3 % 2);

You will see the remainder “1” as the result.

(Raju)

C# – What are the Arithmetic Operators?

Leave a Reply

Your email address will not be published.

Scroll to top