Algorithm
1. Declare Variables:
Declare three integer variables: number1, number2, and sum.
2. User Input:
Display a message to the user prompting them to enter two integers.
Use scanf to read two integers from the user and store them in the variables number1 and number2.
3. Calculate Sum:
Add number1 and number2 and store the result in the variable sum.
4. Display Result:
Print the values of number1, number2, and sum using printf. The format specifier %d is used to print integers.
Display the sum in the format: number1 + number2 = sum.
5. Return Statement:
Use return 0; to indicate successful execution of the program to the operating system.
Code Examples
#1 Code Example Program to Add Two Integers
Code -
C Programming
#include <stdio.h>
int main() {
int number1, number2, sum;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
// calculate the sum
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
Copy The Code &
Try With Live Editor
Output
11
12 + 11 = 23
#2 Code Example with C Programming User Input
Code -
C Programming
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
Copy The Code &
Try With Live Editor
Output
11
12 + 11 = 23
#3 Code Example with Calculate Sum in C programing
Code -
C Programming
sum = number1 + number2;
Copy The Code &
Try With Live Editor
Output
11
12 + 11 = 23
#4 Code Example with C Programming
Code -
C Programming
printf("%d + %d = %d", number1, number2, sum);
Copy The Code &
Try With Live Editor
Output
11
12 + 11 = 23
Demonstration
C programing Example to add two integers using functions-DevsEnv