C Program to Add Two Integers

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 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;
}

Output

Enter two integers: 12
11
12 + 11 = 23

In this program, the user is asked to enter two integers. These two integers are stored in variables number1 and number2 respectively.

printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);

Then, these two numbers are added using the + operator, and the result is stored in the sum variable.

sum = number1 + number2;
Adding two integers in C programming
Add Two Numbers

Finally, the printf() function is used to display the sum of numbers.

printf("%d + %d = %d", number1, number2, sum);

Before we wrap up, let's put your understanding of this example to the test! Can you solve the following challenge?

Challenge:

Write a function to add 10 to a given number.

  • For example, with input num = 5, the return value should be 15.
Did you find this article helpful?

Our premium learning platform, created with over a decade of experience and thousands of feedbacks.

Learn and improve your coding skills like never before.

Try Programiz PRO
  • Interactive Courses
  • Certificates
  • AI Help
  • 2000+ Challenges