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; } |
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.
C Program to Add Two Integers
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.