Sum of Variables in C++ programming

< sum.fashionandcs.com

To find sum of two variables:



#include < iostream >
using namespace std;
int main()
{
int a ;
int b ;
int sum = 0 ;
a = 2 ;
b = 3 ;
sum = a + b ;
cout << sum << endl ;
return 0;
}

In the following program we declares two variables a and b and give there values as 2
and 3 respectively.
We also declare a variable 'sum' that is equals to 0 to avoid the
garbage value
And then we add both variables that is equals to sum.
Now the compiler will give 5 as output.
First the values of both a and b i.e.; 2 and 3 will add and then it will stores in
the variable sum. And as we our displaying sum so whatever the value is store will be dislay as output

Garbage Value:
Whenever we declare a variable a garble value is already stored in it. And when we assigns our own
value to that variable it will replace.
The garbage value will then destroy.

Comments

Popular posts from this blog

How to display the output in C++