c++ programming unit 3 variables,data types
TRANSCRIPT
C++ Language
By:-AAKASH KAUSHIK#9289817971, [email protected]
UNIT -3
VARIABLES & DATA TYPES
VARIABLES
Variables are used to store values.
Variables are name of storage location where
data of particular type can be stored.
Variables stores the latest updated value.
We can use any valid Identifier as a variable
name.
AAKASH KAUSHIK 9891983083,9289817971
Declaration syntax:-Datatype variablename;
Where data type could be any valid Data type for ex.- int, char, float, etc. and variable name could be any valid identifier.
For ex.
int a;
char b;
float c;
DECLARATION OF VARIABLES
AAKASH KAUSHIK 9891983083,9289817971
Intialization means assigning value to declared variable.
For ex:-
a=10;
b=‘A’;
c=3.14;
INTIALIZATION OF VARIABLES
AAKASH KAUSHIK 9891983083,9289817971
Declaration and initialization can take place in same statement also.
For ex.:-
int a=10;
We can declare more than one variable of same in a single statement separated by comma
For ex.:-
int a,b,c;
MORE ON DECLARATION AND INTIALIZATION
AAKASH KAUSHIK 9891983083,9289817971
#include<iostream.h>#include<conio.h>void main(){clrscr();int a,b,sum;a=10;b=20;sum=a+b;cout<<“value of first number”;cout<<a; // syntax to display value of a variablecout<<“value of second number”;cout<<b;cout<<“sum of numbers”;cout<<sum;getch();}
Program of sum of 2 variables
AAKASH KAUSHIK 9891983083,9289817971
Static initialialization means when value of variable is already known and static initialization takes place at compile time.
For ex. int a=10;
Dynamic initialization means when value of a variable is decided at run time i.e., inside execution
For dynamic initialization we use CIN
STATIC V/S DYNAMIC INTIALIZATION
AAKASH KAUSHIK 9891983083,9289817971
SAMPLE PROGRAM OF DYNAMIC INTIALIZATION//PROGRAM TO FIND AREA OF A CIRCLE
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float r,area;
cout<<“enter radius of circle”;
cin>>r; //here value of r will be demanded at runtime
area=3.14*r*r;
cout<<“area of circle”;
cout<<area;
getch();
}AAKASH KAUSHIK
9891983083,9289817971
Cascading of input output statements
We can combine multiple input and output
statements into a single one.
For ex.:-
int a,b,sum;
cout<<“enter 2 numbers”;
cin>>a>>b;
sum=a+b;
cout<<“Sum of ”<<a<<“ & ”<<b<<“is ”<<sum;
AAKASH KAUSHIK 9891983083,9289817971
DATA TYPES
Data type is a data storage format that can
contain a specific type or range of values.
When computer programs store data in variables, each variable must be assigned a specific data type.
Data types represents the type of information that can be stored in a variable.
Data types are the keywords, which are used for Assigning a type to a variable.
AAKASH KAUSHIK 9891983083,9289817971
INTEGER DATA TYPES
AAKASH KAUSHIK 9891983083,9289817971
FLOAT/REAL DATA TYPES
AAKASH KAUSHIK 9891983083,9289817971
CHARACTER DATA TYPES
AAKASH KAUSHIK 9891983083,9289817971
EXERCISEWrite a program to calculate sum, difference,
product, division of two numbers.
Write a program to find area and perimeter of a rectangle.
Write a program to calculate area and circumference of a circle.
Write a program to swap two numbers using third variable.
Write a program to swap two numbers without using third variable.
AAKASH KAUSHIK 9891983083,9289817971
EXERCISEWrite a program to take input marks of 5
subjects and calculate total marks and average marks.
Write a program to calculate simple interest.
Write programs to convert temperature entered into Fahrenheit to Celsius and Celsius to Fahrenheit.
f=(c*9/5)+32
c=(f-32)*5/9
AAKASH KAUSHIK 9891983083,9289817971
THANK
YOU
AAKASH KAUSHIK 9891983083,9289817971