#include <stdio.h>
int main()
{
// Declare variables to store the two numbers and the sum
int num1, num2, sum;
// Ask the user for input
printf("Enter the first number: ");
scanf("%d", &num1);
printf("Enter the second number: ");
scanf("%d", &num2);
// Calculate the sum of the two numbers
sum = num1 + num2;
// Display the result
printf("The sum of %d and %d is %d\n", num1, num2, sum);
return 0;
}
Explanation
return 0;
: This line indicates that the program finished successfully, returning an exit status of 0
.
#include <stdio.h>
:
This line includes the standard input-output library, which allows us to use functions like printf
and scanf
for output and input, respectively.
int main()
:
This line defines the main function, where the execution of the program begins. The int
indicates that this function will return an integer value.
Variable Declaration:
int num1, num2, sum;
: Here, we declare three integer variables: num1
and num2
for storing the user inputs, and sum
for storing the result of the addition.
Input from the User:
printf("Enter the first number: ");
: This line prompts the user to enter the first number.
scanf("%d", &num1);
: This line reads an integer input from the user and stores it in the variable num1
. The &
operator is used to get the address of the variable where the input will be stored.
The same two lines are repeated for the second number (num2
).
Calculating the Sum:
sum = num1 + num2;
: This line adds the two numbers stored in num1
and num2
, and stores the result in the variable sum
.
Displaying the Result:
printf("The sum of %d and %d is %d\n", num1, num2, sum);
: This line prints the result to the console. The %d
placeholders are used to insert the integer values of num1
, num2
, and sum
into the string.
Return Statement:
OUTPUT
Enter the first number: 5
Enter the second number: 7
The sum of 5 and 7 is 12