C program to perform basic arithmetic operations of addition, subtraction, multiplication, and division of two numbers/integers that user inputs.
Division in C
In C language, when we divide two integers, we get an integer result, e.g., 5/2 evaluates to 2.
As a general rule integer/integer = integer, float/integer = float and integer/float = float. So we convert denominator to float in our program, you may also write float in the numerator. This explicit conversion is known as typecasting.
C program for arithmetic operations
int first, second, add, subtract, multiply;
printf("Enter two integers\n");
scanf("%d%d", &first, &second);
add = first + second;
subtract = first - second;
multiply = first * second;
divide = first / (float)second; //typecasting, you can also write: divide = (float)first/second
printf("Sum = %d\n", add);
printf("Difference = %d\n", subtract);
printf("Multiplication = %d\n", multiply);
printf("Division = %.2f\n", divide); // "%.2lf" to print two decimal digits, by default (%lf) we get six
Output of program:
Download Arithmetic operations program.
If we calculate 13/5 in a C program, the result is 2, so you might be thinking how can I get the remainder. To get it, use the modulus operator (%), yes it's a percentage sign.
The value of n is 3 (The remainder we obtain when 13 is divided by 5).