3. 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
4. DECLARATION OF VARIABLES
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;
AAKASH KAUSHIK
9891983083,9289817971
5. INTIALIZATION OF VARIABLES
Intialization means assigning value to declared
variable.
For ex:-
a=10;
b=‘A’;
c=3.14;
AAKASH KAUSHIK
9891983083,9289817971
6. MORE ON DECLARATION AND
INTIALIZATION
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;
AAKASH KAUSHIK
9891983083,9289817971
7. Program of sum of 2 variables
#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 variable
cout<<“value of second number”;
cout<<b;
cout<<“sum of numbers”;
cout<<sum;
getch();
AAKASH KAUSHIK
}
9891983083,9289817971
8. STATIC V/S DYNAMIC INTIALIZATION
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
AAKASH KAUSHIK
9891983083,9289817971
9. 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
10. 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
11. 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
15. EXERCISE
Write 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
16. EXERCISE
Write 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