**Arithmetic operators in C# and Python**

In the previous lesson we used the * operator to calculate the product of two integers. Let us now discuss briefly about the arithmetic operators available in C# and Python.

**Symbol**

*

/

+

–

**Operation**

Multiplication

Division

Addition

Subtraction

**Operator precedence**

Lets take an example to understand the order in which operations are performed C# and Python. Suppose we have 10 dollars at the start of an year and we earn 2.5 dollars each day till the end of the year. The following program calculates the number of dollars we will have at the end of the year.

**C# code**

1 2 3 4 5 6 7 8 9 10 11 12 |
Using System; Class Test { static void Main() { int initial = 10; float daily = 2.5f; float final = initial + daily * 365; Console.WriteLine(final); } } |

**Python code**

1 2 3 4 5 6 7 8 9 10 11 12 |
initial = 10 daily = 2.5 final = initial + daily * 365 print(final) |

It is important to memorize the order in which a programming language performs operations. In the above example, the value of final is 922.5. This value is obtained by first multiplying 365 with 2.5 and adding 10 to the product. Irrespective of the order in which these operations are written, multiplication takes precedence over addition operation. If we wanted to add first, we must enclose the addition operation inside parenthesis.

1 2 3 |
float final = (10 + daily) * 365; |

1 2 3 |
final = (10 + daily) * 365 |

In this case, addition will be performed first since parenthesis take precedence over multiplication. The variable final will have a value of 4562.5. The precedence of basic operators is shown in the following table. Topmost symbol has the highest precedence.

**Operator Precedence**

()

/ *

+ –

Parenthesis

Multiplication and Division

Addition and Subtraction

< Variable declaration and initialization Getting string input >

## What we learnt

- Arithmetic operators available in C# and Python.
- The order in which operations are performed by a programming language must be kept in mind.