Friday, 21 September 2018

Primary data types used in C++ Programming with an example

Data types in C++ 


Data types are used during the declaration of a variable which determined the type of data a variable can store. Primarily they are of three types:
  • Integer 
          If we declare int a; then a can store only integers values like 12, 3, 457.
  • Character
          If we declare char a; then a can store character and symbols like &, @, a, h, i.
  • Float
          If we declare float a; then a can store decimal values like 7.3333, 8.25.


Write a program in C++ giving an idea of primary data types. 


We need to consider different variables with different data types. We need to store the input data in their respective variables and print them. This will let us know about different data types.

Program


#include<iostream.h>
#include<conio.h>
void main()
{
long int admno;
char name[20];
int age;
float prcnt;
clrscr();
cout<<"Enter your admission number=";       //Input part
cin>>admno;
cout<<"Enter your name=";
cin>>name;
cout<<"Enter your age=";
cin>>age;
cout<<"Enter your percentage=";
cin>>prcnt;
cout<<"\nInformation";                                  //Output part    
cout<<"\nAdmission number="<<admno;
cout<<"\nName="<<name;
cout<<"\nAge="<<age;
cout<<"\nPercentage="<<prcnt<<"%";
getch();
}



Output

Example of data types
DATA TYPES IN C++











Explanation


In the program, each variable has own data type and stores a particular value only. The float data type is used to store percentage as it is a decimal number. The int data type can't store decimal so a float data type is used. The char data type is used to store characters and symbols.
// is used to add a comment. It starts from // and ends at end of a line.





No comments:

Post a Comment