Print the Results
In this step, you'll enhance the output formatting of your arithmetic program to make the results more readable and informative. We'll modify the arithmetic.c
file to improve result presentation.
Open the arithmetic.c
file:
cd ~/project
nano arithmetic.c
Update the code with improved result printing:
#include <stdio.h>
int main() {
// Declare variables to store two numbers and results
int num1, num2, sum, difference;
// Print a welcome message
printf("Simple Arithmetic Calculator\n");
printf("----------------------------\n");
// Prompt user to enter first number
printf("Enter the first number: ");
scanf("%d", &num1);
// Prompt user to enter second number
printf("Enter the second number: ");
scanf("%d", &num2);
// Perform addition
sum = num1 + num2;
// Perform subtraction
difference = num1 - num2;
// Print formatted results
printf("\nCalculation Results:\n");
printf("-------------------\n");
printf("First Number: %d\n", num1);
printf("Second Number: %d\n", num2);
printf("Addition: %d + %d = %d\n", num1, num2, sum);
printf("Subtraction: %d - %d = %d\n", num1, num2, difference);
return 0;
}
Compile and run the updated program:
gcc arithmetic.c -o arithmetic
./arithmetic
Example output:
Simple Arithmetic Calculator
----------------------------
Enter the first number: 15
Enter the second number: 7
Calculation Results:
-------------------
First Number: 15
Second Number: 7
Addition: 15 + 7 = 22
Subtraction: 15 - 7 = 8
Key improvements:
- Added welcome and results headers
- Improved formatting with aligned output
- Included more descriptive labels
- Added blank lines for better readability