C Program to Add Two Integers
Last Updated :
17 Jul, 2023
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 Numbers
Below is the program to find the sum of two numbers in C (more specifically integers) entered by the user.
C
#include <stdio.h>
int main()
{
int A, B, sum = 0;
printf ( "Enter two numbers A and B : \n" );
scanf ( "%d%d" , &A, &B);
sum = A + B;
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.
Share your thoughts in the comments
Please Login to comment...