In any programming language adding two numbers is a common program. Every beginner should try this program. To write this program you should know how to take user input, how to show output, how to set equations. Here in this article, we will show and explain the c program to add two numbers.
Main Theory
If X, Y are two number then the addition of two numbers is (X + Y)
We have to get the two numbers using user input. Then we have to store each number inside a variable. Add them and store the result inside another variable. Print that variable to show the result. That is the main theory of this program.
Code
#include <stdio.h>
int main() {
int number1;
int number2;
int sum;
printf("Enter 1st Number: ");
scanf("%d", &number1);
printf("Enter 2nd Number: ");
scanf("%d", &number2);
//sum calculation
sum = number1 + number2;
printf("%d + %d = %d", number1, number2, sum);
return 0;
}
Output:
Enter 1st Number: 4
Enter 2nd Number: 2
4 + 2 = 6
Process returned 0 (0x0) execution time : 6.829 s
Press any key to continue.
Code Explanation
At first, we declare the header file. Then in the main function, we declare three integer type variables. Two of them are for number another one is for the sum of those two numbers. Using scanf() we take two numbers from the user and then we store them inside the 'number1' and 'number2' variables. Then add them and store the result inside the 'sum' variable. Print the 'sum'. Using printf() you can print the result and show it to the user.
Read more: C Program To Calculate The Area of A Square
Conclusion
This is a beginner-friendly program. If you understand the full concept of this program then you can actually write another program like this. You just need to understand the formula and according to the formula, you can easily write the code. Always try to understand the code never try to memories it. Read our other articles about c programming and subscribe to our email subscription for all new updates.