Exercise:
Write a C Program to add two integers.
Click Here to View the Solution!
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// Calculating sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
Click Here to View the Output!
Enter two integers: 4 5
4 + 5 = 9
Click Here to View the Explanation!
- This program is used to print the sum of two numbers.
- Three integer variables
number1
,number2
andsum
are initialized. - On execution, the program requests the user to enter two numbers using
scanf() function
and these inputs are stored in the respective variables. - A variable
sum
is initialized that holds an addition operation. The two numbers are added using the+ operator
and their result is stored in the variablesum
. - The
printf() function
prints the variable sum and displays the result on the screen. - The
return 0 statement
exists the program.