C++ Variables
A value or data stored in a memory. It is identified using a name. That is called a 'Variable'.
First, a variable should be declared.
The syntax is given below…
Datatype variable-name;
Eg:
int n;
float a;
double b;
char s[];
bool d;
Next, the variable should be initialised.
‘int n = 5;’
‘float a =3.4;’
‘double b = 5.67;’
‘char s[] = “string”;
‘bool d = true;’
Let us create a sample program to display the customer
details using C++
C++ Program:
#include <iostream>
using namespace std;
int main() {
int c_age;
// integer variable
string c_id;; // character variable
string c_name;
//String
float c_salary; //
float variable
cout<<
"Enter the Customer Name" << endl;
cin
>>c_name;
cout<<
"Enter the id"<<endl;
cin >>c_id;
cout<<
"Enter the Customer age" <<endl;
cin >>c_age;
cout<<"
Enter the Customer Salary" <<endl;
cin
>>c_salary;
cout <<
"Customer Details" <<endl;
cout <<
"Name: " << c_name << endl;
cout <<
"Age: " << c_age << endl;
cout <<
"Id: " << c_id << endl;
cout <<
"Salary: " << c_salary << endl;
return 0;
}
Output:
Enter the Customer Name
Jeyanth
Enter the id
C1
Enter the Customer age
25
Enter the Customer
Salary
44466.34
Customer Details
Name: Jeyanth
Age: 25
Id: C1
Salary: 44466.3
Note: The scope of the variable can be classified into
local, global, static and dynamic.
If a variable holds the constant value, let this program illustrates
it…
#include <iostream>
using namespace std;
int main() {
const float PI =
3.14;
float r,area =0.0;
cout
<<"Enter the radius"<<endl;
cin>>r;
area = PI*r*r;
cout <<
"The area of the circle is:"<<area<<endl;
return 0;
}
The output is given below…
Enter the radius
5
The area of the circle is:78.5
Hope, this code is useful to you to understand variables. Comment
your doubts… Keep coding…..
Comments
Post a Comment