C Program to Add Two Numbers

Here’s a C program that adds two integers:

				
					#include <stdio.h>

int main() {
  // Declare variables.
  int num1, num2, sum;

  // Get the two integers from the user.
  printf("Enter the first integer: ");
  scanf("%d", &num1);

  printf("Enter the second integer: ");
  scanf("%d", &num2);

  // Add the two integers and store the result in sum.
  sum = num1 + num2;

  // Print the sum.
  printf("The sum of the two integers is %d\n", sum);

  return 0;
}

				
			
Output
C Program to Add Two Integers
  • In this program, we declare three integer variables: num1 to store the first integer, num2 to store the second integer, and sum to store the sum of the two integers.

  • The user is prompted to enter the first integer using the printf function, and the scanf function is used to read the input and store it in the num1 variable.

  • Similarly, the user is prompted to enter the second integer, which is then stored in the num2 variable using scanf.

  • To add the two integers, we simply use the + operator and assign the result to the sum variable.

  • Finally, the program displays the sum of the two integers using printf with the %d format specifier for integers.