Program to perform all arithmatic operations in C
C Program to perform all arithmatic operations:
How to perform all arithmatic operations (like addtion, subtraction, multiplication division etc.) between two numbers in C programming.
Required knowledge for this exercise is:
Program to perform all arithmetic operations:
-
#include <stdio.h>
int main()
{
int num1, num2;
int sum, sub, mult, mod;
float div;
printf("Enter any two numbers: ");
scanf("%d%d", &num1, &num2);
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;
}
Note: In statements div = (float) num1 / num2; It has been typecasted num1 to float before the division to avoid integer division.
Visit to learn more about typecasting.
Output:
- Enter any two numbers: 100, 20
- Sum = 120
- Difference = 80
- Product = 2000
- Quotient = 5.000000
- Modulus = 0
Next Topic to be updated soon..>>
Help others by sharing this page.
Ahmad Irshad
Author & Editor
I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP
Thursday, June 04, 2020
C
0 Comments:
Post a Comment
Please don't enter any spam link in the comment box.