c++ programming unit 3 variables,data types

17
C++ Language By:-AAKASH KAUSHIK #9289817971, 98919893083 [email protected]

Upload: aakash-kumar

Post on 22-Jan-2018

1.428 views

Category:

Education


1 download

TRANSCRIPT

Page 1: c++ programming Unit 3 variables,data types

C++ Language

By:-AAKASH KAUSHIK#9289817971, [email protected]

Page 2: c++ programming Unit 3 variables,data types

UNIT -3

VARIABLES & DATA TYPES

Page 3: c++ programming 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

Page 4: c++ programming Unit 3 variables,data types

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

Page 5: c++ programming Unit 3 variables,data types

Intialization means assigning value to declared variable.

For ex:-

a=10;

b=‘A’;

c=3.14;

INTIALIZATION OF VARIABLES

AAKASH KAUSHIK 9891983083,9289817971

Page 6: c++ programming Unit 3 variables,data types

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

Page 7: c++ programming Unit 3 variables,data types

#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

Page 8: c++ programming Unit 3 variables,data types

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

Page 9: c++ programming Unit 3 variables,data types

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

Page 10: c++ programming Unit 3 variables,data types

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

Page 11: c++ programming Unit 3 variables,data types

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

Page 12: c++ programming Unit 3 variables,data types

INTEGER DATA TYPES

AAKASH KAUSHIK 9891983083,9289817971

Page 13: c++ programming Unit 3 variables,data types

FLOAT/REAL DATA TYPES

AAKASH KAUSHIK 9891983083,9289817971

Page 14: c++ programming Unit 3 variables,data types

CHARACTER DATA TYPES

AAKASH KAUSHIK 9891983083,9289817971

Page 15: c++ programming Unit 3 variables,data types

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

Page 16: c++ programming Unit 3 variables,data types

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

Page 17: c++ programming Unit 3 variables,data types

THANK

YOU

AAKASH KAUSHIK 9891983083,9289817971