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
Fundamentals of C, Data types, Taking user input in C
In my previous post I explained how easily we can find the sum of two numbers. In case you missed here it is -
In this exercise, we will pedal bit more and compute results of all arithmetic operations at once.
Program to perform arithmetic operations
/**
* C program to perform all arithmetic operations
*/
#include <stdio.h>
int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;
/*
* Read two numbers from user
*/
printf("Enter any two numbers : ");
scanf("%d%d", &num1, &num2);
/*
* Performs all arithmetic operations
*/
sum = num1 + num2;
sub = num1 - num2;
mult = num1 * num2;
div = (float)num1 / num2;
mod = num1 % num2;
/*
* Prints the 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;
}
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 ;)
Recommended posts
- Basic C programming exercises index.
- C program to perform all arithmetic operations using pointers
- C program to find perimeter of rectangle.
- C program to find area of rectangle.
- C program to find diameter, circumference and area of circle.
- C program to convert length from meter to kilometer.
- C program to convert temperature from Celsius to Fahrenheit.