- Write a program in c for addition, subtraction, multiplication, division and modulus of two numbers.

**arithmetic operators**supported by C language, which are addition(+), subtraction(-), multiplication(*), division(/) and modulus(%) of two numbers. All arithmetic operators compute the result of specific arithmetic operation and returns its result.

## Arithmetic Operators in C

Operator | Description | Syntax | Example |
---|---|---|---|

+ | Adds two numbers | a + b | 15 + 5 = 20 |

- | Subtracts two numbers | a - b | 15 - 5 = 10 |

* | Multiplies two numbers | a * b | 15 * 5 = 75 |

/ | Divides numerator by denominator | a / b | 15 / 5 = 3 |

% | Returns remainder after an integer division | a % b | 15 % 5 = 0 |

## C program for addition, subtraction, multiplication, division and modulus of two numbers

This program performs basic binary arithmetic operation on two integer operands like addition, subtraction, division and modulus and prints result in screen. It first takes two integers as input from user using scanf function and stores them in 'firstNumber' and 'secondNumber' int variables and performs addition, subtraction, multiplication, division and modulus operations and stores results in 'sum', 'difference' , 'product', 'quotient' and 'modulo' variables respectively. Next, it prints the result on screen using printf function.

/* * C Program for Addition, Subtraction, Multiplication, Division * and Modulus of two numbers */ #include <stdio.h> #include <conio.h> int main(){ /* Variable declation */ int firstNumber, secondNumber; int sum, difference, product, modulo; float quotient; /* Taking input from user and storing it in firstNumber and secondNumber */ printf("Enter First Number: "); scanf("%d", &firstNumber); printf("Enter Second Number: "); scanf("%d", &secondNumber); /* Adding two numbers */ sum = firstNumber + secondNumber; /* Subtracting two numbers */ difference = firstNumber - secondNumber; /* Multiplying two numbers*/ product = firstNumber * secondNumber; /* Dividing two numbers by typecasting one operand to float*/ quotient = (float)firstNumber / secondNumber; /* returns remainder of after an integer division */ modulo = firstNumber % secondNumber; printf("\nSum = %d", sum); printf("\nDifference = %d", difference); printf("\nMultiplication = %d", product); printf("\nDivision = %.3f", quotient); printf("\nRemainder = %d", modulo); getch(); return 0; }

#### Program Output

Enter First Number: 25 Enter Second Number: 4 Sum = 29 Difference = 21 Multiplication = 100 Division = 6.250 Remainder = 1

**Points to Remember**

- If both operands are integers, then the output is also integer and the compiler neglects the term after decimal point.
- Integer/Float = Float and Float/Integer = Float because of explicit type conversion.
- Multiplication, division and modulus operator have highest priority than addition and subtraction operator.

## C program for addition, subtraction, multiplication, division and modulus of two numbers using a function

This program uses five user defined functions 'getSum', 'getDifference', 'getProduct', 'getQuotient' and 'getModulo' to perform addition, subtraction, multiplication, division and modulus of two numbers.

/* * C Program to Add, Subtract, multiply, divide and modulo two numbers * using function */ #include <stdio.h> int getSum(int num1, int num2); int getDifference(int num1, int num2); int getProduct(int num1, int num2); float getQuotient(int num1, int num2); int getModulo(int num1, int num2); int main(){ /* Variable declation */ int firstNumber, secondNumber; int sum, difference, product, modulo; float quotient; /* Taking input from user and storing it in firstNumber and secondNumber */ printf("Enter First Number: "); scanf("%d", &firstNumber); printf("Enter Second Number: "); scanf("%d", &secondNumber); /* Adding two numbers */ sum = getSum(firstNumber, secondNumber); /* Subtracting two numbers */ difference = getDifference(firstNumber, secondNumber); /* Multiplying two numbers*/ product = getProduct(firstNumber, secondNumber); /* Dividing two numbers */ quotient = getQuotient(firstNumber, secondNumber); /* Remainder when firstNumber is divided by secondNumber*/ modulo = getModulo(firstNumber, secondNumber); printf("\nSum = %d", sum); printf("\nDifference = %d", difference); printf("\nMultiplication = %d", product); printf("\nDivision = %.3f", quotient); printf("\nRemainder = %d", modulo); getch(); return 0; } /* * Function to add two numbers */ int getSum(int num1, int num2){ int sum; sum = num1 + num2; return sum; } /* * Function to subtract two numbers */ int getDifference(int num1, int num2){ int difference; difference = num1 - num2; return difference; } /* * Function to multiply two numbers */ int getProduct(int num1, int num2){ int product; product = num1 * num2; return product; } /* * Function to divide two numbers */ float getQuotient(int num1, int num2){ float quotient; quotient = (float)num1 / num2; return quotient; } /* * Function to return remainder when one * number is divided by other number */ int getModulo(int num1, int num2){ int modulo; modulo = num1 % num2; return modulo; }

**Related Topics**