Professional Documents
Culture Documents
Week 6
Week 6
Header file
#include <iostream>
using namespace std;
Main Function
int main()
{ Function Body
cout<<“First Program.”<<endl;
return 0;
Print Statement
}
Concept of a Variable
Variable stores data in RAM
They have a type, memory address and a name to access
When a variable is created, then operating system allocate memory to that variable in
RAM
The memory may be taken back by OS during program execution or when the program
exit
Variables & Data types
Data types
int (4 bytes)
float (4 bytes)
Char (1 byte)
bool (1 byte)
short int (2 bytes)
long (4 bytes)
double (8 bytes)
sizeof operator tells the space occupied by a variable
cout<<sizeof(int);
Using variables
Create variable
DATA_TYPE name = value;
int area = 62;
int length, width = 45;
length = 40;
area = length * width;
cout<<area;
Taking input from user
cin is console input object defined in iostream
>> is stream extraction operator
Input in stored in a variable
cin>>length;
Examples
Make a length unit converter
Take input in meters
Convert that into kilometers
Take radius of circle and show its area
Take length and width of rectangle and show its parameter and area
Homework
Evaluate quadratic formula Take a, b, c from user and find out x
You need to find square root, so there is a function sqrt defined in math.h header file
You can use that function like sqrt(a) where a is a variable or a number
Write a program to calculate area of parallelogram, take values form user
Write a program in C++ to calculate the volume of a sphere ( ⁴⁄ ₃π r³). Take radius from keyboard.
Find how much Oil can be stored in a tanker of height h and a radius R if one liter oil occupies
1000cm3 and volume of tanker (π r2 h).). Input h and R from user and assume R and H is in cm.
Input a single digit number and make it four digit (e.g. input 5 result is 5555)
Take 3 digit number form user and show 2nd digit only (Hint: use /, %)
Saleman has 12500 basic pay. He is given 10.5% bonus on each sale. Input the sale from user and
show net pay.
Reading