C Programming

C program to add two numbers ( Super Easy Explained)

In a normal world, the addition of the two numbers is very easy but when it comes to programming then we have to use certain predefined functions to add these numbers. In this post, I will try to simply explain ” How to add two number using c programming “. If you really interested in c then I will surely recommend you Let us c book ( My favorite ones ) in which each and every concept is briefly & easily explained.

Now coming to the topic, In this simple program firstly we will learn the step-by-step algorithm which we will use to create this program. Please try to understand this program very clearly so that your basic concept of c programming becomes clear.

c program to add two numbers

Our Aim: Addition of two numbers in c

The aim of this program is very simple, firstly we have to take two numbers may be an integer, float or any other data format and with the help of arithmetic operator ( +,-,*,/ ) we have to find there sum.

But before reading this program, please make sure you have a decent knowledge of c data types because for making this program simple we will only use integer data type.

Now finally,

Algorithm ( Program to add two number in c )

  1. Start
  2. Accept two numbers.
  3. Add these numbers.
  4. Finally, print the result.
  5. End

Program for the addition of two numbers

#include<stdio.h>
int main()
{
int a,b,c;
printf("Enter two numbers :\n");
scanf("%d%d",&a,&b);
c=a+b;
printf("The sum of a & b is",c);

return 0;
}

Output

Enter two numbers :

2

3

The sum of a & b is 5

Super Easy Explanation

  • Header File: Since we are making this program in c. So the first rule is to declare the header file of c which we are going to use in our program. Here our header file is stdio.h.
  • Main() Function: By default, we have to create all of our programs in main() function. We may create other functions in c but to use that functions we have to implement it in main() function.
  • Declaration of Data Type: Here we have used integer data type which can only accept the integer values. We have used an integer data type with 3 variable i.e., a, b &c.
  • Input From User: The scanf() function is used to take input from users, here we have stored our inputs in variable a & b. This a & b is nothing but a variable to store two number which we have to add.
  • Addition: In our program, we have added two numbers stored in a & b and stored it in variable c with the help of statement – ” c=a+b “.
  • Display Result: Finally we have to display the result which is stored in our variable c with the help of printf() function.

Program to add two number without any third variable

#include<stdio.h>
int main()
{
int num1,num2;
printf("Enter any two numbers :"\n);
scanf("%d%d",&num1,&num2);
a=a+b;
printf("Sum of these numbers is :%d",a);

return 0;
}

Output

Enter any two numbers :

2

3

Sum of these numbers is 5

Explanation

  • In this program, rather than using any third variable we simply use two variables i.e, num1 & num2 to add these two numbers.
  • The result will be stored in the first variable num1.
  • The reason for using only two variables only is, it will save our much data because of less number of variables.

Leave a Reply