< Prev
Next >

# C# Assignment and arithmetic Operators

## Arithmetic Operators

In C# language, there are 5 kinds of arithmetic operators:
• - operator(subtraction).
• * operator(multiplication).
• / operator(division).
• % operator(modulus).

## Arithmetic operators(+, -, *) operating on non-instance variables.

``````//C# Example of Arithmetic operators(+, -, *) operating on non-instance variables.

using System;

class A
{
public static void Main()
{
//Initializing two local variables
int x = 30, y = 30;

//addition of two local int variables
int sum = x + y;

//subtraction of two local int variables
int difference = x - y;

//multiplication of two local int variables
int multiplication = x * y;

Console.WriteLine("x = " + x);
Console.WriteLine("y = " + y);
Console.WriteLine("Sum of x and y = "+ sum);
Console.WriteLine("Subtraction of x and y = "+ difference);
Console.WriteLine("Multplication of x and y = "+ multiplication);
}
}``````

## Output is

``````x = 30
y=30
Sum of x and y = 60
Subtraction of x and y = 0
Multplication of x and y = 900``````

## Arithmetic operators(+ - *) operating on instance variables.

``````//C# Example of Arithmetic operators(+, -, *) operating on instance variables.

using System;

class A
{
//Instance variables
int a=20;
int b=30;
int result;

public static void Main()
{
//Creating an object of class A
A ob= new A();

//Printing the values of two instance variables a and b
Console.WriteLine("The value of instance variable a = " + ob.a);
Console.WriteLine("The value of instance variable b = " + ob.b);

//addition of two instance variables of type int
ob.result = ob.a + ob.b;
Console.WriteLine("Sum of instance variables a and b = "+ ob.result);

//subtraction of two instance variables of type int
ob.result = ob.a - ob.b;
Console.WriteLine("Subtraction of instance variables a and b = "+ ob.result);

//multiplication of two instance variables of type int
ob.result = ob.a * ob.b;
Console.WriteLine("Multiplication of instance variables a and b	 = "+ ob.result);
}
}``````

## Output -

``````The value of instance variable a = 20
The value of instance variable b = 30
Sum of instance variables a and b = 50
Subtraction of instance variables a and b = -10
Multiplication of instance variables a and b = 600```
```

## Division operator / and Modulus operator %

• Division operator / gives us the resulting quotient value after dividing two int values.
• Modulus operator % gives us the remainder value after dividing two int values.

``````//C# Example of Division operator / and Modulus operator %

using System;

class A
{
//Instance variables
int i=20;
int j=10;

public static void Main()
{
//Creating an object of class A
A ob = new A();

//Printing the values of two instance variables i and j
Console.WriteLine("The value of instance variable i = " + ob.i);
Console.WriteLine("The value of instance variable j = " + ob.j);

Console.WriteLine("Quotient after dividing i by j : " + ob.i / ob.j);
Console.WriteLine("Remainder after dividing i by j : " + ob.i % ob.j);

//Two local variables i.e. non-instance variables
int a = 90;
int b = 20;

//Printing the values of two local variables a and b
Console.WriteLine("The value of local variable a = " + a);
Console.WriteLine("The value of local variable b = " + b);

Console.WriteLine("Quotient after dividing a by b : "+ a / b);
Console.WriteLine("Remainder after dividing a by b : "+ a % b);
}
}``````

## Output-

``````The value of instance variable i = 20
The value of instance variable j = 10
Quotient after dividing i by j : 2
Remainder after dividing i by j : 0
The value of local variable a = 90
The value of local variable b = 20
Quotient after dividing a by b : 4
Remainder after dividing a by b : 10```
```

## Priorities of arithmetic operators in arithmetic expressions.

• The + and - operators have a least priority in an arithmetic expression.
• The *, / and % operators have an equal highest priority in an arithmetic expression.

## Calculating arithmetic expressions with equal and unequal priorities.

• Calculation of an arithmetic expression involving operators with unequal priorities starts with the calculation of a sub-expression that involves an operator with the highest priority.
• Calculation of an arithmetic expression involving operators with equal priorities is made by evaluating expressions from left to right.

For example, when calculating an arithmetic expression 3+2x4-2, sub-expression involving * operator will be calculated first, because operator * has highest priority. Hence -
• 3+2*4-2, first 2*4 will be calculated, which results in the next expression
• 3+8-2, now + and - operator have an equal priority, so, expression will be calculated from left to right
• 11-2, which results in
• 9

• Let's understand with a code -:
``````//C# Example of priorities of arithmetic operators in arithmetic expressions

using System;

class A
{
public static void Main()
{
// + - operators have equal priorities
int result = 3+2-4+2;
Console.WriteLine("Result of  3+2-4+2 = " + result);

// + * + have unequal priorities
result = 3+2*4+2;
Console.WriteLine("Result of  3+2*4+2 = " + result);

// + * / operators have unequal priorities
result = 3+2*4/2;
Console.WriteLine("Result of  3+2*4/2 = " + result);

// % * / operators have equal priorities
result = 3%2*4/2;
Console.WriteLine("Result of  3%2*4/2 = " + result);
}
}``````

## Output-

``````Result of  3+2-4+2 = 3
Result of  3+2*4+2 = 13
Result of  3+2*4/2 = 7
Result of  3%2*4/2 = 2``````

## Program Analysis

• First arithmetic expression 3+2-4+2, is calculated left to the right as + - operator have equal priorities.
• Second expression 3+2*4+2, * operator have more priority than + , hence 2*4 is calculated first.
• Third expression 3+2*4/2, * / operators have more priority than + , hence 2*4 is calculated first
• Fourth expression 3%2*4/2, % * / operators have an equal priority, hence expression is calculated from left to the right, starting with 3%2.

## Arithmetic expressions with sub-expressions in parenthesis ( ) will always be executed first

``````//C# Example of arithmetic expressions with sub-expressions in parenthesis ( ) will always be executed first

using System;

class A
{
public static void Main()
{
int result = (3+2)*4+2;
Console.WriteLine("Result of (3+2)*4+2 = " + result);

result = 3%(2*4)/2;
Console.WriteLine("Result of 3%(2*4)/2 = " + result);
}
}``````

## Output-

``````Result of (3+2)*4+2 = 22
Result of 3%(2*4)/2 = 1``````

## Program Analysis

• First expression (3+2)*4+2, although * has more priority than + but the sub-expression in () has the most priority hence (3+2) is calculated first.
• Second expression 3%(2*4)/2, although * % / have equal priority so this expression could be calculated left to right but the sub-expression in () has most priority hence (2*4) is calculated first.   