Loading...
C Program to add two integers

C Program to Add Two Integers

In this example, we will learn to get two integer as user input then add them and print the output on the screen.

Remainder :

To understand this example, you should have the knowledge of the following C programming topics:


Program to Add Two Integers

#include <stdio.h>

int main() {
  int num1, num2, sum;
  printf("Enter two number : ");
  scanf("%d %d", &num1, &num2);
  sum = num1 + num2;
  printf("The sum of %d and %d is %d", num1, num2, sum);
  return 0;
}

Output

Enter two number : 34 23
The sum of 34 and 23 is 57

Working of the above program

  • First we declare three integer variables num1, num2 and sum, in which num1, num2 used to store integers and sum to store the sum of both integers .
  • int num1, num2, sum;
  • Then we asked the user to enter two integers.
  • printf("Enter two number : ");
  • Then we take the user input using scanf() function and store the entered value in num1 and num2 variable respectively.
  • scanf("%d %d", &num1, &num2);
  • Then we add num1 and num2 and save the resultant value in variable sum.
  • sum = num1 + num2;
  • Finally, we print the value of num variable using %d format specifier in printf() function.
  • printf("The sum of %d and %d is %d", num1, num2, sum);

- Related Topics