C Program to Add Two Integers
Program to Add Two Integers
#include <stdio.h>
int main()
{
int firstNumber, secondNumber, sumOfTwoNumbers;
printf("Enter two integers: ");
// Two integers entered by user is stored using scanf() function
scanf("%d %d", &firstNumber, &secondNumber);
// sum of two numbers in stored in variable sumOfTwoNumbers
sumOfTwoNumbers = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, sumOfTwoNumbers);
return 0;
}
Output
Enter two integers: 12 11 12 + 11 = 23
In this program, user is asked to enter two integers. Two integers the user enters are stored in variables firstNumber and secondNumber respectively. This is done using scanf()
function.
Then, variables firstNumber and secondNumber are added using + operator and the result is stored in sumOfTwoNumbers.
Finally, the sumofTwoNumbers is displayed on the screen using printf()
the function.
example C program that prompts the user to enter two integers, adds them together, and then shows the result on the console:
#include <stdio.h>
int main() {
int num1, num2, sum;
printf("Enter first integer: ");
scanf("%d", &num1);
printf("Enter second integer: ");
scanf("%d", &num2);
sum = num1 + num2;
printf("Sum of %d and %d is %d", num1, num2, sum);
return 0;
}
In this program, we declare three variables of type int
: num1
, num2
, and sum
. We prompt the user to enter the first integer using the printf()
and scanf()
functions, and store it in the num1
variable. We then do the same for the second integer, storing it in the num2
variable.
Next, we add the two integers together and store the result in the sum
variable. Finally, we use another printf()
function call to display the sum of the two integers using the %d
format specifier. We include the values of num1
, num2
, and sum
as arguments to the printf()
function, separated by commas.