A directory of Objective Type Questions covering all the Computer Science subjects.
Here you can access and discuss Multiple choice questions and answers for various competitive exams and interviews.

C program to perform addition, subtraction, multiplication, and division/

C program to perform addition, subtraction, multiplication, and division

Problem Description

This C program performs basic arithmetic operations like addition, subtraction, multiplication and division. It asks the user to input two integer numbers and then displays the results. If the first number is smaller than the second number, it displays the subtraction with minus sign.

C program to perform addition, subtraction, multiplication, and division - Source code

/* C program to perform addition, subtraction,
multiplication, and division */
#include <stdio.h>
int main()
{
int num1, num2, add, sub, mul;
float div;
printf("Enter two integer numbers\n");
scanf("%d%d", &num1, &num2);
add = num1 + num2;
sub = num1 - num2;
mul = num1 * num2;
div = num1 / (float)num2;
printf("Sum = %d\n",add);
printf("Difference = %d\n",sub);
printf("Multiplication = %d\n",mul);
printf("Division = %.2f\n",div);
return 0;
}

Program Output

Case 1:
Enter two integer numbers
45
32
Sum = 77
Difference = 13
Multiplication = 1440
Division = 1.41
Case 2:
Enter two integer numbers
25
50
Sum = 75
Difference = -25
Multiplication = 1250
Division = 0.50

Program Explanation

1. num1, num2, add, sub and mul variables are declared to store integer values. div variable is declared to store float values as division of two integer numbers can be a float number.
2. Addition, subtraction, multiplication and division is calculated of the two numbers using simple mathematical formulas.
3. While calculation the division, the denominator is type casted to float in order to obtain the result in float.
4. While printing the value of division result on the console %.2f is used, to restrict the result up to two decimal points.