Print the Result
In this step, you'll learn how to format and print the results of modulus operations in a more meaningful way. We'll enhance the previous program to provide a clear explanation of the calculation.
Open the existing file and update the code:
cd ~/project
nano modulus_example.c
Replace the previous code with the following:
#include <stdio.h>
int main() {
// Declare integer variables
int dividend, divisor, remainder, quotient;
// Prompt user for input
printf("Enter the dividend (number to be divided): ");
scanf("%d", ÷nd);
printf("Enter the divisor (number to divide by): ");
scanf("%d", &divisor);
// Calculate quotient and remainder
quotient = dividend / divisor;
remainder = dividend % divisor;
// Print detailed explanation of the division
printf("\nDivision Calculation:\n");
printf("%d ÷ %d = %d remainder %d\n",
dividend, divisor, quotient, remainder);
// Verification equation
printf("\nVerification:\n");
printf("%d = %d × %d + %d\n",
dividend, divisor, quotient, remainder);
return 0;
}
Compile the updated program:
gcc modulus_example.c -o modulus_example
Example output when running the program:
Enter the dividend (number to be divided): 17
Enter the divisor (number to divide by): 5
Division Calculation:
17 ÷ 5 = 3 remainder 2
Verification:
17 = 5 × 3 + 2
Key changes in this version:
- Added
quotient
calculation using integer division
- Included more detailed output showing the division process
- Added a verification line to demonstrate the mathematical relationship