In this example, you will learn to add two integers by taking input from the user and display the results on the screen.
Contents
You can learn more C Programming concepts here.
Program to add two integers
#include <stdio.h>
int main() {
int sum, number1, number2;
printf("Enter two integer numbers");
scanf("%d %d", &number1, &number2);
// calculate sum
sum = number1 + number2;
printf("Sum = %d", sum);
return 0;
}
Run Code on Online C CompilerOutput
// For the inputs 10 and 20
Enter two integer numbers 10 20
Sum = 30
Program Explanation
In this example, the user is asked to enter two integers. The two numbers entered by the user will be stored in number1 and number2 respectively.
printf("Enter two integer numbers");
scanf("%d %d", &number1, &number2);
Then the number1 and number2 are added using the + operator and the result is stored in sum.
sum = number1 + number2;
The result is displayed on the screen.
printf("Sum = %d", sum);