C program to perform all arithmetic operations

Write a C program to input two numbers and perform all arithmetic operations. How to perform all arithmetic operation between two numbers in C programming. C program to find sum, difference, product, quotient and modulus of two given numbers.

Example
Input

First number: 10
Second number: 5

Output

Sum = 15
Difference = 5
Product = 50
Quotient = 2
Modulus = 0

Required knowledge

In previous post I explained to find the sum of two numbers.

Read more - Program to find sum of two numbers

In this exercise, we will pedal bit more and compute results of all arithmetic operations at once.

Program to perform all arithmetic operations

/**
* C program to perform all arithmetic operations
*/

#include <stdio.h>

int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;

/*
* Input two numbers from user
*/
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);

/*
* Perform all arithmetic operations
*/
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;

/*
* Print result of all arithmetic operations
*/
printf("SUM = %d\n", sum);
printf("DIFFERENCE = %d\n", sub);
printf("PRODUCT = %d\n", mult);
printf("QUOTIENT = %f\n", div);
printf("MODULUS = %d", mod);

return 0;
}

In statement div = (float) num1 / num2;, I have typecasted num1 to float before the divide operation, to avoid integer division.

Read more - Type conversion in C programming.

Note: \n is an escape sequence character used to print new lines (move to the next line).

Output

Enter any two numbers : 20 10
SUM = 30
DIFFERENCE = 10
PRODUCT = 200
QUOTIENT = 2.000000
MODULUS = 0

Happy coding ðŸ˜‰