Write A Program To Add 2 Numbers In C Programming
\\ If you have any query or questions on my post then please comment below //
Program 1: WAP To add 2 numbers:
-----------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
printf("Enter 2 numbers:");
scanf("%d%d", &a,&b);
c=a+b;
printf("Addition of 2 Numbers is:%d \n"c);
getch();
}
----------------------------------------------
Output: if you enter the value of a = 3 & b =8, then
Addition of 2 Numbers is: 11
Program 2: Write a program to add 2 numbers by using 2 variable:
-------------------------------------------------------
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
printf("Enter 2 numbers:");
scanf("%d%d", &a,&b);
b=a+b;
printf("Addition of 2 Numbers is:%d \n"b);
getch();
}
--------------------------------------------------------
Here if you enter the value of a = 3 & b =8, then the output would be same.
The difference between both the program is:
In our first program:
we are using 3 variables, first variable a, second variable b. Here these two variables are used to store input value, And for storing result value we used c variable. So in our first program we used 3 variables.
In our Second Program:
here we used only 2 variables, first a is used to store the input first value & b is used to store the input second value as well as storing result. Now you are thinking how, So answers would be:
first a & b variable storing input value from keyboard to memory by using "scanf("%d%d", &a,&b);".
Then now time to add these 2 numbers so for that i am written "b=a+b;" It means first compiler adding the value of a and b (a+b), then result is storing in b variable.
Now the questions arising is, if we store 2 numbers in same variable then what happen:
So if first we store b = 5 and after some time we are storing b = 8 so second value replace the first value, it remove the first value and then it move. So after this if we print the value of b then output will be: 8.
I hope you understand this post, this question and program is widely asked in interview questions. Better you prepare this question very well. if you still have any query then please ask question with us in blow comment box or fb comment box.
Program To Add 2 Number By Using 2 & 3 Variables in hindi & english language:
Click Here To Know How To Download Above Video Tutorials Or YouTube Videos Free