Variables in c++
Variable are used in C++, where we need storage for any value, which will change in program. Variable can be declared in multiple ways...
Variable are used in C++, where we need storage for any value, which will change in program. Variable can be declared in multiple ways...
1-displaying a variable
#include<iostream>
using namespace std;
int main ()
{
int x = 23;
cout<<"hello world"<<endl;
cout<<"my name is John"<<endl;
cout <<"i am \n";
cout<<x;
cout<<"year old";
return 0;
}
Output:-
Note:-The integer can be declared in either way and you will get the same result..
int x = 23;
or
int x;
x=23;
also in the code, The variables shall never be written in quotation marks,, because if you will type
cout<<"x" ;
then it will type the letter x, and not the variable x
EXAMPLE 2- Adding two variables
#include<iostream>
using namespace std;
int main ()
{
int a=50;
int b=25;
int result = a+b;
cout<<result;
return 0;
}
In above example we predefined the value of the variable..but if we want user to select a value for that variable, we use cin with an extraction operator(<<)
Note that cout is written with insersion operator (>>)
Example 3- Doubling a number given by user
#include<iostream>
using namespace std;
int main ()
{
int a;
cout<<"please enter a numbers to be doubled "<<endl;
cin>>a;
cout<<(a*2);
return 0;
}
Output:-
Example 4- Accepting multiple inputs from user
#include<iostream>
using namespace std;
int main ()
{
int a,b,c;
cout<<"please enter a number"<<endl;
cin>>a ;
cout<<"please enter another number"<<endl;
cin>>b ;
c=a+b;
cout<<"the sum of two numbers is"<<c ;
return 0;
}
#include<iostream>
using namespace std;
int main ()
{
float a,b,c;
cout<<"please enter a number"<<endl;
cin>>a ;
cout<<"please enter another number"<<endl;
cin>>b ;
c=a+b;
cout<<"the sum of two numbers is"<<c ;
return 0;
}
In the next post, you will learn about conditional statements..
Conditional statements
#include<iostream>
using namespace std;
int main ()
{
int x = 23;
cout<<"hello world"<<endl;
cout<<"my name is John"<<endl;
cout <<"i am \n";
cout<<x;
cout<<"year old";
return 0;
}
Output:-
Note:-The integer can be declared in either way and you will get the same result..
int x = 23;
or
int x;
x=23;
also in the code, The variables shall never be written in quotation marks,, because if you will type
cout<<"x" ;
then it will type the letter x, and not the variable x
EXAMPLE 2- Adding two variables
#include<iostream>
using namespace std;
int main ()
{
int a=50;
int b=25;
int result = a+b;
cout<<result;
return 0;
}
In above example we predefined the value of the variable..but if we want user to select a value for that variable, we use cin with an extraction operator(<<)
Note that cout is written with insersion operator (>>)
Example 3- Doubling a number given by user
#include<iostream>
using namespace std;
int main ()
{
int a;
cout<<"please enter a numbers to be doubled "<<endl;
cin>>a;
cout<<(a*2);
return 0;
}
Output:-
Example 4- Accepting multiple inputs from user
#include<iostream>
using namespace std;
int main ()
{
int a,b,c;
cout<<"please enter a number"<<endl;
cin>>a ;
cout<<"please enter another number"<<endl;
cin>>b ;
c=a+b;
cout<<"the sum of two numbers is"<<c ;
return 0;
}
if you want to declare an variable as decimal , float is used e.g float a,b,c
#include<iostream>
using namespace std;
int main ()
{
float a,b,c;
cout<<"please enter a number"<<endl;
cin>>a ;
cout<<"please enter another number"<<endl;
cin>>b ;
c=a+b;
cout<<"the sum of two numbers is"<<c ;
return 0;
}
In the next post, you will learn about conditional statements..
Conditional statements
No comments:
Post a Comment