Open In App

C Program to Add Two Integers

Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will learn to write a C program for the addition of two integers. In C, the sum of two numbers can be done by the addition operator (+). This operator works by taking two operands and returning their sum as the result.

C Program to Add Two Integers

C Program to Add Two Numbers

Below is the program to find the sum of two numbers in C (more specifically integers) entered by the user.

C




// C program to add two numbers
#include <stdio.h>
  
int main()
{
    int A, B, sum = 0;
  
    // Ask user to enter the two numbers
    printf("Enter two numbers A and B : \n");
  
    // Read two numbers from the user || A = 2, B = 3
    scanf("%d%d", &A, &B);
  
    // Calculate the addition of A and B
    // using '+' operator
    sum = A + B;
  
    // Print the sum
    printf("Sum of A and B is: %d", sum);
  
    return 0;
}


Output

Enter two numbers A and B : 
Sum of A and B is: 0

Complexity Analysis

  • Time Complexity: O(1)
  • Auxiliary Space: O(1)

Explanation

In the above program, the user is first asked to enter two numbers, and the input is scanned using the scanf() function and stored in the variables A and B. Then, the variables A and B are added using the arithmetic operator + (addition operator), and the result is stored in the variable sum.


Last Updated : 17 Jul, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads