C Program to Add Two Integers

@codevarsity
 · 
750 views
 · 

In this program, user is asked to enter two integers. Then, the sum of those two integers is stored in a variable and displayed on the screen. For this, the knowledge of data types, variables, and operators is required.

Related Topics

Data Types  Variables  Operators 

Code

/*C Program to Add Two Integers */
#include <stdio.h>
int main()
{
    int first, second, sum;
    
    printf("Enter two integers: ");

    // Two integers entered by user are stored using scanf() function
    scanf("%d %d", &first, &second);

    // sum of two numbers in stored in variable sum
    sum = first + second;

    // Displays sum      
    printf("%d + %d = %d", first, second, sum);

    return 0;
}

Output

Enter two integers: 12
11
12 + 11 = 23

Explanation

C Program to Add Two Integers

In this program, user is asked to enter two integers. Two integers entered by the user is stored in variables first and second using scanf() respectively.

The variables first and second are added using + operator and the result is stored in the variable sum.

Atlast, the sum of two numbers is printed using printf() function.

Tags

#return 

Comments