Input/output Statements in C++: Muhammad Saleem Raza
Input/output Statements in C++: Muhammad Saleem Raza
Input/output Statements in C++: Muhammad Saleem Raza
JHANG 2020
Department of Computer Science
INPUT/OUTPUT STATEMENTS
IN C++
(It is chapter No. 04,
IT Series book:- Object Oriented
Programming using C++ by Prof. Tasleem Mustafa )
1. Input-Output statements .
2. Standard Output.
3. Escape sequences.
4. Standard Input.
----------------------------------------------------------------------
Note:
Prepare this chapters. If there is any difficulty, contact me.
Muhammad Saleem Raza
Department of Computer Sciences
Government Postgraduate College, Jhang
0345-4707500,0313-7035400
[email protected]
AREEHA_ABUTALIB
0
GOVERNMENT POSTGRADUATE COLLEGE 1
JHANG
Department of Computer Science
INPUT-OUTPUT STATEMENTS IN C ++
QUESTION NO.01:- What is the concept of Input/Output statements
in C++ language?
INPUT STATEMENTS
Statements used to get input data and assign it to the variables are called
Input statements.
OUTPUT STATEMENTS
Statements used to retrieve processed data from Computer memory and
then send it to the output devices (Monitor, printer etc.) are called Output
statements.
PROGRAM-01
Write a program in C++ Language to print a text of 4 lines on Computer Screen.
SOLUTION Check the message of the Day
#include<conio.h> Government of Punjab is appointing lecturers
#include<iostream.h> In different subjects
At Government Colleges
void main(void){ clrscr( );
cout<<" Check the message of the Day"<<endl;
cout<<" Government of the Punjab is appointing lecturers"<<endl;
cout<<" in different subjects."<<endl;
cout<<" at Government Colleges "<<endl;
getch( );}
GOVERNMENT POSTGRADUATE COLLEGE 2
JHANG
Department of Computer Science
cout<<" Jhang";
getch ( ); }
\a (Alarm or Beep)
“a” stands for alert or alarm. This character causes a beep sound in the
Computer internal speaker.
\b (BACKSPACE)
“b” stands for backslash. This moves the cursor one space left.
PROGRAM-03
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( ); Govt. College Jhang
PROGRAM-04
#include<conio.h>
GOVERNMENT POSTGRADUATE COLLEGE 3
JHANG
Department of Computer Science
#include<iostream.h>
void main(void){ clrscr( );
cout<<" Faisalabad\b";
cout<<" Multan"; Faisalaba Multan
getch( );}
Note:-In this program, printing of next statement i.e cout<<“Multan”) will be start from the
character ‘d’ and hence this character will be deleted.
\t Tab (Horizontal)
“t” stands for tab. This escape sequence causes the print control on the
Output Device to move one
tab forward.
PROGRAM-05
#include<conio.h>
#include<iostream.h> Pakistan Multan Faisalabad
void main(void){ clrscr( );
cout<<" Pakistan \t Multan\t Faisalabad"<<endl;
getch( );}
\r (Carriage Return)
“r” stands for carriage return. It moves the curser to the beginning current
line. When \r is used between two words, the word after \r will come before
first word.
PROGRAM-06
#include<conio.h>
#include<iostream.h>
Punjab Pakistan
void main(void){ clrscr( );
cout<<" Punjab \r Pakistan ";
getch( );}
\\ (Backslash)
These are two backslash characters. One is used as control character and
second is used to print a backslash ‘\’ character on the monitor screen.
PROGRAM-07
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
cout<<" C:\\ABC"; C:\ABC
getch( );}
Note:- When we run this program, only single slash will be printed on the monitor screen.
GOVERNMENT POSTGRADUATE COLLEGE 4
JHANG
Department of Computer Science
PROGRAM=08
#include<conio.h>
C:ABC
#include<iostream.h>
void main(void){ clrscr( );
cout<<" C:\ABC";
getch( );}
Note:- If a single backslash character is used then there will be no effect on the output as shown
in the program.
\” (Double Quote)
This escape sequence is used to print double quotation mark (”) on the
computer screen.
PROGRAM-09
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
cout<<" \" Pakistan";
" Pakistan
getch( );}
\f (Form Feed)
“ f ” stands for form feed. If this escape sequence is used and printer
is attached with the computer then computer will give one blank paper and
curser will move to the top of the next page on the printer.
\’ Single Quote)
This escape sequence is used to print single quotation mark.
PROGRAM-10
#include<conio.h>
’ Pakistan
#include<iostream.h>
void main(void){ clrscr( );
cout<<" \'Pakistan ";
getch( );}
\? (Question Mark)
This escape sequence is used to print question mark on the Computer
screen.
PROGRAM-11
#include<conio.h>
#include<iostream.h>
Who invented the Bicycle?
void main(void){ clrscr( );
cout<<" Who invented the Bicycle\? ";
GOVERNMENT POSTGRADUATE COLLEGE 5
JHANG
Department of Computer Science
getch( ); }
PROGRAM-12
Write a program to print the following message on the screen. He said
to me,” Hurrah we have won the match.”
Solution
#include<conio.h> He said to me, "Hurrah we have won the match"
#include<iostream.h>
void main(void){ clrscr( );
cout<<"He said to me,\”Hurrah we have won the match\”";
getch( ); }
PROGRAM-13
Write a program which shows the following output.
Each word is
Tabbed over once
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
cout<<" Each\t word \t is\n" ;
cout<<" Tabbed \t over \t once";
getch ( ) ; }
PROGRAM-14
Write a program to implement the escape sequence character to print
the following output.
PAKISTAN
"PAKISTAN
'PAKISTAN
'PAKISTAN'
P A K I S T A N
/ PAKISTAN /
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
cout<<" PAKISTAN\n";
cout<<" \" PAKISTAN\n";
cout<<" \' PAKISTAN\n";
cout<<" \'PAKISTAN \' \n";
GOVERNMENT POSTGRADUATE COLLEGE 6
JHANG
Department of Computer Science
PROGRAM -15
Write a simple program to show the use of “cin>>” and “cout<< “object.
SOLUTION
#include<conio.h>
Enter any positive integer: =10
#include<iostream.h> The value of b=20
void main(void){ clrscr( );
int a, b;
cout<<"Enter any positive integer:=";
cin>>a;
b=2*a;
cout<<"The value of b="<<b<<endl;
getch( );}
PROGRAM-16
If a five-digit number is input through keyboard then write a program to reverse
the number.
SOLUTION
#include<conio.h>
#include<iostream.h>
GOVERNMENT POSTGRADUATE COLLEGE 7
JHANG
Department of Computer Science
PROGRAM-17
If a five-digit number is input through the keyboard then write a program to
calculate the sum of its digits.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
long int n, a , b , c , d ,sum;
cout<<"Enter any five digit positive number: = ";
cin>>n;
a=n%10; Enter any five digit positive number:=54984
n=n/10; Number in original order is
5 4 9 8 4
b=n%10; Sum of digits is:=30
n=n/10;
c=n%10;
n=n/10;
d=n%10;
n=n/10;
cout<<" Number in original order is:\n";
cout<<n<<"\t"<<d<<"\t"<<c<<"\t"<<b<<"\t"<<a<<endl;
sum=a+b+c+d+n;
cout<<"Sum of digits is:="<< sum<<endl;
GOVERNMENT POSTGRADUATE COLLEGE 8
JHANG
Department of Computer Science
getch( ); }
PROGRAM-18
If a five-digit number is input through the keyboard then write a program to
calculate the sum of its first and last digit.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
long int n, a , b , c , d ,sum;
cout<<"Enter any five digit positive number: = ";
cin>>n;
a=n%10; Enter any five digit positive number:=54984
n=n/10; Number in original order is
5 4 9 8 4
b=n%10; Sum of its first and last digits is:=9
n=n/10;
c=n%10;
n=n/10;
d=n%10;
n=n/10;
cout<<" Number in original order is:\n";
cout<<n<<"\t"<<d<<"\t"<<c<<"\t"<<b<<"\t"<<a<<endl;
sum=a+n;
cout<<"Sum of its first and last digits is:="<< sum<<endl;
getch( ); }
PROGRAM-19
Write a program to input temperature in Fahrenheit. Convert the temperature to
Celsius degrees.
SOLUTION Enter temperature in Fahrenheit: =98.6
#include<conio.h> Temperature in Celsius is =37
#include<iostream.h>
void main(void){ clrscr( );
float f, c;
cout<<"Enter temperature in Fahrenheit :=";
cin>>f;
c=(f-32)*5.0/9.0;
cout<<"Temperature in Celsius is ="<< c<<endl;
getch( );}
GOVERNMENT POSTGRADUATE COLLEGE 9
JHANG
Department of Computer Science
PROGRAM-20
Write a program to input the Radius of the sphere. Compute its volume and surface
area using formulae Area=4R2 and Volume= 4 R3 where =3.1415932
3
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
float r, volume, area ;
cout<<"Enter the Radius of Sphere :="; Enter the Radius of Sphere :=9.87
cin>>r; Area of the sphere is = 1224.18
volume of the sphere is = 4027.54
area=4.0*3.14159*r*r ;
volume=(4.0/3.0)*3.1415932*r*r*r;
cout<<"Area of the sphere is ="<< area<<endl;
cout<<" volume of the sphere is ="<< volume<<endl;
getch( );}
PROGRAM-21
Write a program to input temperature in Celsius degrees. Convert the temperature
to Fahrenheit.
SOLUTION
#include<conio.h> Enter temperature in Celsius : =37
#include<iostream.h> Temperature in Fahrenheit is =98.6
void main(void){ clrscr( );
float f, c;
cout<<"Enter temperature in Celsius :=";
cin>>c;
f=(9.0/5.0*c+32);
cout<<"Temperature in Fahrenheit is ="<<f<<endl;
getch( );}
PROGRAM-22
Write a program to compute the area of a triangle. Formula of the area of triangle
(a + b + c)
is given as: S = , Area= (s(s − a)( s − b)( s − c))
2
SOLUTION
#include<math.h>
#include<conio.h>
#include<iostream.h> Enter the temperature in
void main(void){ clrscr( ); Fahrenheit :=");Enter the
int a,b,c; temperature in Fahrenheit
:=");
GOVERNMENT POSTGRADUATE COLLEGE 10
JHANG
Department of Computer Science
float s, area ;
cout<<"Enter length of first side :="; Enter length of first side: = 180
cin>>a; Enter length of second side: =178
Enter length of third side: = 190
cout<<"Enter length of second side :="; Area of the triangle is =14411.7
cin>>b;
cout<<"Enter length of third side :=";
cin>>c;
s=(a+b+c)/2.0;
area=sqrt(s*(s-a)*(s-b)*(s-c));
cout<<"Area of the triangle is ="<< area<<endl;
getch( );}
Enter the temperature in
PROGRAM-23 Fahrenheit :="); Enter the
temperature in Fahrenheit :=");
Write a program that accepts a positive number from user and print square root of
the number on the computer screen.
SOLUTION
#include<conio.h>
#include<iostream.h>
#include<math.h>
void main(void){ clrscr( );
Enter positive number :=3
int n; Square Root is=1.7321
float s;
cout<<"Enter positive number: =";
cin>>n;
s=sqrt(n);
cout<<"Square Root is ="<<s<<endl;
getch( );}
PROGRAM-24
Write a program to compute the distance covered by motorcycle in 50 seconds.
Its initial velocity is 10 m/sec and acceleration is 5m/sec2 by using the formula.
S=vit+ ½ at2
SOLUTION
#include<conio.h>
GOVERNMENT POSTGRADUATE COLLEGE 11
JHANG
Department of Computer Science
#include<iostream.h>
void main(void){ clrscr( );
int t, v, a;
float s; Distance covered is :=6750
t=50;
v=10;
a=5;
s=v*t+(a*t*t)/2.0;
cout<<"Distance covered is :="<< s<<endl;
getch( );}
PROGRAM-25
Remish Basic salary is input through keyboard. His dearness allowance is 40% of
Basic salary. House rent allowance is 20% of Basic salary. Write a program to
calculate the grass salary.
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void ){ clrscr( );
float basic, d_all, house_rent, grass_pay;
cout<<"Enter Basic Salary:=";
cin>>basic;
d_all=basic*40/100;
Enter Basic Salary:=5000
house_rent=basic*42/100; Total Salary is:=9100.00
grass_pay=basic+d_all+house_rent;
cout<<" Total Salary is:= "<< grass_pay;
getch( ); }
PROGRAM-26
Distance between two cities (in kilometers) is input through keyboard. Write a
program to convert distance in Meters, Feet, Inches and Centimeters
SOLUTION Enter distance in kilometers:=150
#include<conio.h> Distance in meters is:=15000.00
Distance in feet is:= 492126.00
#include<iostream.h> Distance in inches is:= 5905512.00
void main(void ){ clrscr( );
float kilometers, meters, feet, inches, centimeters;
cout<<"Enter distance in kilometers: =";
cin>>kilometers;
meters=kilometers*1000;
feet= meters*3.28084;
GOVERNMENT POSTGRADUATE COLLEGE 12
JHANG
Department of Computer Science
inches=feet*12;
cout<<"Distance in meters is:="<<meters<<endl;
cout<<"Distance in feet is:="<<feet<<endl;
cout<<"Distance in inches is:="<< inches<<endl;
getch( ); }
PROGRAM-27
Write a program to compute cube of the number entered through keyboard.
SOLUTION
#include<conio.h>
Enter any positive number:=5
#include<iostream.h> Cube of number is:=125
void main(void){ clrscr( );
int a,b;
cout<<"Enter any positive number:= ";
cin>>a;
b=a*a*a;
cout<<"Cube of number is:="<<b<<endl;
getch( );}
PROGRAM-28
Write a program to input the marks obtained by a student in four subjects.
Calculate the total marks and their average.
SOLUTION
#include<conio.h>
#include<iostream.h> Enter marks in English :=57
Enter marks in Urdu := 62
void main(void){ clrscr( ); Enter marks in Physics :=75
float en, ur, phy, comp, sum, average; Enter marks in Computer:=77
Total marks in four subjects:=271
cout<<"Enter marks in English := "; Average of the marks is:= 67.75
cin>>en;
cout<<"Enter marks in Urdu := ";
cin>>ur;
cout<<"Enter marks in Physics := ";
cin>>phy;
cout<<"Enter marks in Computer:= ";
cin>>comp;
sum= (en+ ur+ phy+comp);
cout<<"Total marks in four subjects:="<< sum<<endl;
average=sum/4;
cout<<"Average of the marks is:="<< average<<endl;
getch( );}
GOVERNMENT POSTGRADUATE COLLEGE 13
JHANG
Department of Computer Science
PROGRAM-29
Write a program in C++language to convert the inches into millimeters. Input value
in inches through keyboard during program execution. Where 1inch=25.4 millimeter
SOLUTION
#include<conio.h>
#include<iostream.h> Enter length in inches: = 15
void main(void ){ clrscr( ); Length is millimeters is =381.00
PROGRAM-30
Write a program to compute Kinetic Energy of the body of moss “m” moving with
velocity “v”. Input the mass and velocity of the particle during the program execution.
Where Kinetic Energy= 12 mv2
SOLUTION
#include<conio.h>
#include<iostream.h>
void main(void){ clrscr( );
float m, v, K_E;
cout<<"Enter the mass of particle:= "; Enter the mass of particle:=2
Enter the velocity: =5
cin>>m; Kinetic energy is :=25.000000
cout<<"Enter the velocity:= ";
cin>>v;
K_E=0.5*m*v*v;
cout<<"Kinetic energy is :="<<K_E<<endl;
getch( );}
PROGRAM-31
Write a Program to print a text of 4 lines consisting of characters integers
values and floating-point values
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void){ clrscr( );
int a, b, c, d;
a = 234 ;
GOVERNMENT POSTGRADUATE COLLEGE 14
JHANG
Department of Computer Science
b = 360;
c = 786; Integers: 234 360 786 3454
d = 3454; Floats: 34.5 67.12 112.12 3.1416
float m, n, r, p; Characters: # @ > |
m = 34.5; We have Displayed the text in 4 lines
n = 67.12;
r = 112.12;
p = 3.1416;
char ch1, ch2, ch3, ch4;
ch1 = '#';
ch2 = '@';
ch3 = '>';
ch4 = '|';
cout<<"Integers: "<<a<<" "<<b<<" "<<c<<" "<<d<<endl;
cout<<"Floats: "<<m<<" "<<n<<" "<<r<<" "<<p<<endl;
cout<<"Characters: "<<ch1<<" "<<ch2<<" "<<ch3<<" "<<ch4<<endl;
cout<<"We have Displayed the text in 4 lines "<<endl;
getch();
PROGRAM-32
Write a Program that input 4 Numbers and Calculate the (sum average and
product ) of all the Numbers
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void){ clrscr( );
int a, b, c, d, sum, product;
float average;
cout<<"Enter Number1: "; Enter Number1: 50
Enter Number2: 40
cin>>a;
Enter Number3: 35
cout<<"Enter Number2: ";
Enter Number4: 15
cin>>b; Sum of Four(4) Numbers :=140
cout<<"Enter Number3: "; Average of Four(4) Numbers:= 35
cin>>c; Product of Four(4) Numbers:=1050000
cout<<"Enter Number4: ";
cin>>d;
sum = a + b + c + d;
average = sum / 4.0;
product = a * b * c * d;
cout<<" Sum of Four(4) Numbers :="<<sum<<endl;
cout<<"Average of Four(4) Numbers:= "<<average<<endl;
cout<<"Product of Four(4) Numbers:="<<product<<endl;
getch(); }
GOVERNMENT POSTGRADUATE COLLEGE 15
JHANG
Department of Computer Science
PROGRAM-33
Write a Program that input age in years and display age in days and Months.
SOLUTION
#include<iostream.h>
#include<conio.h> Enter Your Age in Years: 45
void main(void){ clrscr( ); Your Age in Days is: 16425
int y,m,d; Your Age in Months is: 540
PROGRAM-34
Write a Program that inputs a Number from the User and display its square and
cube.
SOLUTION
Enter a Number to Find its Square & Cube: 15
#include<iostream.h>
Square of 15 is: 225
#include<conio.h>
Cube of 15 is: 3375
void main(void){ clrscr( );
int n, square, cube;
cout<<"Enter a Number to Find its Square & Cube: ";
cin>>n;
square = n*n;
cube = n*n*n;
cout<<"Square of "<<n<<" is: "<<square<<endl;
cout<<" Cube of "<<n<<" is: "<<cube<<endl;
getch(); }
PROGRAM-35
Write a Program that input total Number of pages of book, number of pages a
person read in one day and the number of days a person has read the book. It Displays
the number of pages that have been read and the number of pages remaining.
SOLUTION Enter Total Number of Pages of Book:=300
#include<iostream.h> Enter the Pages a person Read in One Day:= 15
#include<conio.h> Enter the Number of days a Person Read the Book: 10
void main(void){ Pages Read by the Person are: 150
The Remaining Pages in this Book are: 150
clrscr( );
int page, ppr, page_read, day, t_page_read, Remaining;
cout<<"Enter Total Number of Pages of Book:=";
GOVERNMENT POSTGRADUATE COLLEGE 16
JHANG
Department of Computer Science
cin>>page;
cout<<"Enter the Pages a person Read in One Day:= ";
cin>>page_read;
cout<<"Enter the Number of days a Person Read the Book: ";
cin>>day;
t_page_read = page_read * day;
Remaining = page - t_page_read;
cout<<" Pages Read by the Person are: "<< t_page_read <<endl;
cout<<"The Remaining Pages in this Book are: "<< Remaining<<endl;
getch(); }
PROGRAM-36
A Car can travel 5.3 miles in 1 liter. Write a Program that input petrol in liters
and displays, how much distance the car can cover using the available petrol.
SOLUTION
#include<iostream.h>
Enter the Liters of Petrol available in Your Car: 100
#include<conio.h> Your Car can Cover 529 miles
void main(void){ clrscr( ); with 100 Liters.
int liters, distance;
cout<<"Enter the Liters of Petrol available in Your Car: ";
cin>>liters;
distance = 5.3 * liters;
cout<<"Your Car can Cover "<<distance<<" miles" <<endl;
cout<<" with "<<liters<<" Liters.";
getch(); }
PROGRAM-37
Write a Program that inputs total number of Students in a Class and fee per
Student. Then it displays the total fee collected from the Class.
SOLUTION
Enter the Total Number of Students in any Class: 100
#include<iostream.h>
Enter the Fee Collected per Student: 2500
#include<conio.h>
The Total Fee Collected:=250000
void main(void){ clrscr( );
int total_student, fee, total_fee;
cout<<"Enter the Total Number of Students in any Class: ";
cin>>total_student;
cout<<"Enter the Fee Collected per Student: ";
cin>>fee;
total_fee = total_student * fee;
cout<<"The Total Fee Collected:="<<total_fee<<endl;
getch(); }
GOVERNMENT POSTGRADUATE COLLEGE 17
JHANG
Department of Computer Science
PROGRAM-38
Write a Program that inputs a 3-digit number and display its Digits in separate
3 lines. e.g. if user input 123 then Program displays the output as:
1
2
3
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void){ clrscr( );
int n, a, b;
cout<<"Enter any 3-digit Number: ";
cin>>n;
a = n % 10;
n = n / 10;
b = n % 10;
n = n / 10;
cout<<n<<endl<<b<<endl<<a<<endl;
getch(); }
PROGRAM-39
Write a Program to Show following output using one ( cout<< ) statement:
1 2 3 4 5
6 7 8 9 10
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void){ clrscr( );
cout<<"1\t 2\t 3\t 4\t 5\n 6\t 7\t 8\t 9\t10"<<endl;
getch(); }
PROGRAM-40
Write a Program to calculate the volume of a cube by taking measures from the
user (Formula: V = length * width * height).
SOLUTION Enter the Length of Cube: 10
#include<iostream.h> Enter the Width of Cube: 15
#include<conio.h> Enter the Height of Cube: 20
void main(void){ clrscr( ); The Volume of Cube is: 3000
float l, w, h, v;
cout<<"Enter the Length of Cube: ";
cin>>l;
cout<<"Enter the Width of Cube: ";
GOVERNMENT POSTGRADUATE COLLEGE 18
JHANG
Department of Computer Science
cin>>w;
cout<<"Enter the Height of Cube: ";
cin>>h;
v = l * w * h;
cout<<"The Volume of Cube is: "<<v;
getch(); }
PROGRAM-41
Write a Program that input the x, y coordinates for two points and computes the
distance between two points using the formula: √(𝑋2 − 𝑋1 )2 + (𝑦2 − 𝑦1 )2
SOLUTION
#include<iostream.h> Coordinates of First Point
#include<conio.h> Enter the x Coordinate: 1
Enter the y Coordinate: 2
#include<math.h>
Coordinates of Second Point
void main(void){ clrscr( );
Enter the x Coordinate: 3
float x1, y1, x2, y2, d; Enter the y Coordinate: 4
cout<<"Coordinates of First Point"<<endl;
cout<< "Enter the x Coordinate: ";
cin>>x1; Distance Between two points is: 2.82843
cout<<" Enter the y Coordinate: ";
cin>>y1;
cout<<"Coordinates of Second Point "<<endl;
cout<<"Enter the x Coordinate: ";
cin>>x2;
cout<<" Enter the y Coordinate: ";
cin>>y2;
d = sqrt((x2-x1)*(x2-x1)+(y2-y1)*(y2-y1));
cout<<"Distance Between two points is: "<<d<<endl;
getch();}
PROGRAM-42
Write a Program to swap three values with using fourth variable
SOLUTION
#include<iostream.h>
#include<conio.h> Enter value for First Number: - 10
Enter value for Second Number: - 20
void main(void) { clrscr( ); Enter value for Third Number:- 30
int x, y, z ;
cout<< "Enter value for First Number:- ";
cin>>x;
cout<< "Enter value for Second Number:- ";
cin>>y;
cout<< "Enter value for Third Number:- ";
GOVERNMENT POSTGRADUATE COLLEGE 19
JHANG
Department of Computer Science
cin>>z;
cout<<"Values before Swapping "<<endl;
cout<<"Value of x:="<< x<<endl;
cout<<" Value of y:="<< y<<endl; Values before Swapping
cout<<"Value of z:="<< z<<endl; Value of x:=10
Value of y:=20
x=x+y+z; Value of z:=30
y= x-(y+z);
z= x-(y+z);
x= x-(y+z); Values after Swapping
cout<<"Values after Swapping "<<endl; Value of x:=30
Value of y:=10
cout<<"Value of x:="<< x<<endl; Value of z:=20
cout<<" Value of y:="<< y<<endl;
cout<<"Value of z:="<< z<<endl;
getch( ) ; }
PROGRAM-43
Write a Program that calculates the arc of length of a convex lens by taking
radius of arc and angle made by arc. Formula is: length=radius*angle
SOLUTION
#include<iostream.h>
#include<conio.h> Enter value for Radius: - 15
Enter value for angle: - 45
void main(void) { clrscr( ); Arc of length is:- 675
float l, r, angle ;
cout<< "Enter value for Radius :- ";
cin>>r;
cout<< "Enter value for angle:- ";
cin>>angle;
l=r*angle;
cout<< "Arc of length is :- "<<l<<endl;
getch();}
PROGRAM-44
Write a Program that inputs pounds from the user and convert into kilograms.
Where 1-Pound=2.205kilogram
SOLUTION Enter number of pounds :- 85
#include<iostream.h> kilograms is:- 38.5488
#include<conio.h>
void main(void) { clrscr( );
float p,k ;
cout<< "Enter number of pounds :- ";
cin>>p;
GOVERNMENT POSTGRADUATE COLLEGE 20
JHANG
Department of Computer Science
k=p/2.205;
cout<< "kilograms is:- "<<k<<endl;
getch();}
PROGRAM-45
Write a Program that inputs time in seconds and converts it hh:mm:ss format.
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void) { clrscr( );
int sec,h, m,s ;
cout<< "Enter time in Seconds: - ";
cin>>sec; Enter time in Seconds: - 5300
h=sec/3600; Total Hours:=- 1
Total Minutes :=- 28
sec=sec%3600; Total Seconds:=- 20
m=sec/60;
s=sec%60;
cout<< "Total Hours:=- "<<h<<endl;
cout<< "Total Minutes :=- "<<m<<endl;
cout<< "Total Seconds:=- "<<s<<endl;
getch();}
PROGRAM-46
Write a Program that prompts the user to enter number of hours. It computes and
display number of weeks, days and hours.
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void) { clrscr( );
int w,h, d;
cout<< "Enter time in hours : - ";
cin>>h;
Enter time in hours : - 5300
w=h/168;
Total Weeks:=- 31
h=h%168; Total Days :=- 3
Total Hours :=- 20
d=h/24;
h=h%24;
cout<< "Total Weeks:=- "<<w<<endl;
cout<< "Total Days :=- "<<d<<endl;
cout<< "Total Hours :=- "<<h<<endl;
getch();}
GOVERNMENT POSTGRADUATE COLLEGE 21
JHANG
Department of Computer Science
PROGRAM-47
Write a Program that compute the area of sector of a circle where theta is angle
in radians measures.
SOLUTION
#include<iostream.h>
#include<conio.h> Enter value for Radius:- 15
Enter value for angle: - 45
#include<math.h> Area of sector of a circle: -5062.5
void main(void) { clrscr( );
float theta, r, area ;
cout<< "Enter value for Radius :- ";
cin>>r;
cout<< "Enter value for angle:- ";
cin>>theta;
area =(r*r*theta)/2.0;
cout<< " Area of sector of a circle:-"<<area<<endl;
getch();}
PROGRAM-48
Write a Program that reads a positive number and then computes the logarithm
of the value to the base 2.
SOLUTION
#include<iostream.h>
Enter the positive Number :- 20
#include<conio.h>
Logarithm of Number20to the base 2 is := 4.32193
#include<math.h>
void main(void) { clrscr( );
float n, ans ;
cout<< "Enter the positive Number :- ";
cin>>n;
ans=log(n)/log(2.0);
cout<<"Logarithm of Number"<<n<<"to the base 2 is := "<<ans<<endl;
getch();}
PROGRAM-49
Write a Program to enter a letter through keyboard and display the next two
letters.
SOLUTION
#include<iostream.h>
#include<conio.h> Enter any letter :- R
Given character is:= R
void main(void) { clrscr( ); Next First character is:= S
char ch,ch1,ch2; Next Second character is:= T
JHANG
Department of Computer Science
ch=getche();
cout<<endl;
cout<<" Given character is:= "<<ch<<endl;
ch1=ch+1;
ch2=ch+2;
cout<<" Next First character is:= "<<ch1<<endl;
cout<<" Next Second character is:= "<<ch2<<endl;
getch();}
PROGRAM-50
Write a Program that inputs two times a in hh:mm:ss format. Add both times
and display result.
SOLUTION
#include<iostream.h>
#include<conio.h>
void main(void) { clrscr( );
int h,h1,h2,m,m1,m2,s,s1,s2;
cout<< "Enter the First Time in format HH:MM:SS:=:- ";
cin>>h1>>m1>>s1;
cout<< "Enter the Second Time in format HH:MM:SS:=:- ";
cin>>h2>>m2>>s2;
Enter the First Time in format HH:MM:SS:=:- 2 45 30
s=s1+s2; Enter the Second Time in format HH:MM:SS:=:- 4 35 42
Total Time is:
m=m1+m2+s/60;
Hours:= 7
h=h1+h2+m/60; Minutes:= 21
s=s%60; Seconds := 12
m=m%60;
cout<<"Total Time is:"<<endl;
cout<<" Hours:= "<<h<<endl;
cout<<" Minutes:= "<<m<<endl;
cout<<" Seconds := "<<s<<endl;
getch();}