If you have just started learning C language and looking for basic C program for adding two numbers or program to get sum of two numbers in c, here is the good article for it, I have provided the code for it and have explained about the code also.
int data-type in C
Before we proceed for the C program of adding two numbers, let's discuss int (Integer data type) in C
Integers are whole numbers that can have both positive and negative values but no decimal values. Example: 0, -5, 10.
In C programming, keyword int is used for declaring integer variable. For example:
int num;
We have just declared the "int" type variable "num", we haven't initialized it yet. You can also declare multiple variables in one line in C programming. For example:
int id, age;
The size of int is either 2 bytes(In older PC's) or 4 bytes. If you consider an integer having size of 4 byte( equal to 32 bits).
You can get the size of int in a C code, as below
#include <stdio.h>
int main()
{
printf("sizeof(int) = %d bytes\n",sizeof(int));
return 0;
}
C program to add two numbers
Now, that we have a basic understanding of what is int and how we can declare int in C, let's take a look at a sample program for the addition of two numbers in C
Here are the steps to create a C program for adding two numbers:
- Declare three variables as explained above, i.e, firstNumber, SecondNumber & TotalSumOfBoth
- Ask the user to enter: firstNumber & SecondNumber (using printf)
- Save both the values in the above-declared variables (firstNumber & SecondNumber, using scanf)
- Simply add both the number using '+' and save it in third variable (TotalSumOfBoth)
- Print the Sum (TotalSumOfBoth)
So here is the complete code based on the above steps
#include <stdio.h>
void main()
{
//declare three variables, for firstNumber+secondNumber=TotalSumOfBoth
int firstNumber, secondNumber, TotalSumOfBoth;
//ask the user to get first number to add
printf("Enter first number: ");
//store it in first variable using scanf() function
scanf("%d", &firstNumber);
//ask the user to get second number to add
printf("Enter second number:");
//store it in second variable using scanf() function
scanf("%d", &secondNumber);
// sum of two numbers in stored in variable TotalSumOfBoth
TotalSumOfBoth = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, TotalSumOfBoth);
}
I have added code with comments to explain each line again inside the code.
Output:
Enter first number: 10
Enter second number:20
10 + 20 = 30
In this program, the user is asked to enter two integers one by one, both two integers entered by the user is stored in variables firstNumber and secondNumber respectively using scanf() function.
Once the values are stored in the variables, both the variables are added using "+" operator and value is saved into TotalSumOfBoth.
At last, we print TotalSumOfBoth using printf() function.
You can also remove two scanf() function and get both variables from user using single scanf() in single line like below code
#include <stdio.h>
void main()
{
//declare three variables, for firstNumber+secondNumber=TotalSumOfBoth
int firstNumber, secondNumber, TotalSumOfBoth;
//ask the user to get first number to add
printf("Enter two numbers to add: ");
//store the values in variables using scanf() function
scanf("%d %d", &firstNumber, &secondNumber);
// sum of two numbers in stored in variable TotalSumOfBoth
TotalSumOfBoth = firstNumber + secondNumber;
// Displays sum
printf("%d + %d = %d", firstNumber, secondNumber, TotalSumOfBoth);
}
Output:
Enter two numbers to add: 10
22
10 + 22 = 32
You may also like to read:
Infix to postfix conversion program in C using stack (With algorithm)
Creating palindrome program in C (checking string & number)
Program to check prime number in C using while loop
Program to swap two numbers in C (With pointers & without pointers)
Stack Program in C (Concept, Algorithm & C program example)
That's it, if you have any questions, feel free to ask it in comments section.