Arithmetic operators in C Programming language includes operators like Addition, Subtraction, Multiplication, Division and Modulus. All these operators are binary operators which means they operate on two operands. Below table shows all the Arithmetic Operators in C language with examples.

Arithmetic Operators in C |
Operation |
Example |
---|---|---|

+ |
Addition | 10 + 2 = 12 |

– |
Subtraction | 10 – 2 = 8 |

* |
Multiplication | 10 * 2 = 20 |

/ |
Division | 10 / 2 = 5 |

% |
Modulus – It returns the remainder after the division | 10 % 2 = 0 (Here remainder is zero). If it is 10 % 3 then it will be 1. |

**Arithmetic Operators in C Example**

For this example, We are using two variables a and b and their values are 12 and 3. We are going to use these two variables to perform various arithmetic operations present in C Language

**C PROGRAM CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
/* Program to Perform Arithmetic Operations in C */ #include<stdio.h> int main() { int a = 12, b = 3; int addition, subtraction, multiplication, division, modulus; addition = a + b; //addition of 3 and 12 subtraction = a - b; //subtract 3 from 12 multiplication = a * b; //Multiplying both division = a / b; //dividing 12 by 3 (number of times) modulus = a % b; //calculation the remainder printf("Addition of two numbers a, b is : %d\n", addition); printf("Subtraction of two numbers a, b is : %d\n", subtraction); printf("Multiplication of two numbers a, b is : %d\n", multiplication); printf("Division of two numbers a, b is : %d\n", division); printf("Modulus of two numbers a, b is : %d\n", modulus); } |

**OUTPUT**

**NOTE:** When we are using division (/) operator the result will be completely depend upon the data type it belongs to. For example, if the data type is integer then it will produce the integer value by rounding the value (5 / 2 = 2). If you want correct result then change the data type to float. Don’t get confused, let’s see one more example for better understanding

**Arithmetic Operators in C using Float**

For this example, We are using two variables a and b and their values are 7 and 3. We are going to use these two variables to show the problems we generally face while performing arithmetic operations on Int and Float Data type.

**C PROGRAM CODE**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
/* C Program to Perform Division and Modulus on Float data type */ #include<stdio.h> int main() { int a = 7, b = 3; int integerdiv, modulus; float floatdiv; integerdiv = a / b; // dividing 7 by 3 modulus = a % b; // calculation the remainder floatdiv = (float)a / b; // Converting int to float printf("Division of two numbers a, b is : %d\n", integerdiv); printf("Modulus of two numbers a, b is : %d\n", mod); printf("---------Correct Results is------- \n"); printf("Division of two numbers a, b is : %f\n", floatdiv); } |

**OUTPUT**

If you notice the above result we got two different results for same calculation because for the first result, both a and b are integers and the output is also integer (integerdiv) so the compiler neglects the term after decimal point and shows answer 2 instead of 2.3333 and a % b is 1 because remainder is 1. Next we changed output data type to float (floatdiv) and also converted the result to float to get our desired result.

**TIP:** Be careful while using division operator (Type casting plays major role here).

Thank you for Visiting our Blog.

## Leave a Reply