C++ SOLUTION (Chapter 1 ) : Starting Programming
For any type of consultation, query or doubt. You may contact to the following: (+91) 9804 436 193 debabrataguha20@gmail.com
You will get all the exercise in the Facebook group https://www.facebook.com/groups/331231161093613/
1. Enter 2 numbers and find the result after addition, subtraction, multiplication and division.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i,
j, add, sub, mul, div;
cout<<"\n enter the first number";
cin>>i;
cout<<"\n enter the second number";
cin>>j;
add=i+j;
sub=i-j;
mul=i*j;
div=i/j;
cout<<"\n addition="<<add;
cout<<"\n substraction="<<sub;
cout<<"\n multiplication="<<mul;
cout<<"\n division="<<div;
getch();
}
2. Write a program to input your age and mobile number, then print the same.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
long
int j;
cout<<"\n enter your age";
cin>>i;
cout<<"\n enter the phone number";
cin>>j;
cout<<"\name="<<i;
cout<<"\nphone number="<<j;
getch();
}
3. Write a program to print a block F using hash (#), where the F has a height of six characters and width of five and four characters.
(
Expected Output:
######
#
#
#####
#
#
#
)
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
cout<<"\n ######";
cout<<"\n #";
cout<<"\n #";
cout<<"\n #####";
cout<<"\n #";
cout<<"\n #";
cout<<"\n #";
getch();
}
4. Write a program to compute the perimeter and area of a rectangle with a height of 7 inches. and width of 5 inches.
(
Expected Output:
Perimeter of the rectangle = 24 inches
Area of the rectangle = 35 square inches
)Perimeter of the rectangle = 24 inches
Area of the rectangle = 35 square inches
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
l,b,ar,peri;
l=7;
b=5;
ar=l*b;
peri=2*(l+b);
cout<<"\nArea="<<ar<<" Square Inches";
cout<<"\nPerimeter="<<peri<<" Inches";
getch();
}
5. Write a program to compute the perimeter and area of a circle with a radius of 6 inches.
(
Expected Output:
Perimeter of the Circle = 37.680000 inches
Area of the Circle = 113.040001 square inches
Perimeter of the Circle = 37.680000 inches
Area of the Circle = 113.040001 square inches
)
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float
r,ar,peri;
r=6;
ar=3.14*r*r;
peri=2*3.14*r;
cout<<"\nArea="<<ar<<" square inches";
cout<<"\nPerimeter="<<peri<<" inches";
getch();
}
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
a,b,ar,peri;
cout<<"\n enter the length of the rectangle";
cin>>a;
cout<<"\n enter the breadth of the rectangle";
cin>>b;
ar=a*b;
peri=2*(a+b);
cout<<"\nArea="<<ar;
cout<<"\nPerimeter="<<peri;
getch();
}
7. Write a program that takes hours and minutes as input, and calculates the total number of minutes.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
i,hr,min,tot;
cout<<"\n enter the time in hours";
cin>>hr;
cout<<"\n enter the time in minutes";
cin>>min;
tot=(hr*60)+min;
cout<<"\n the total number of minutes are "<<tot;
getch();
}
8. Enter the length in meter and find the equivalent in centimeter and kilometer.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float
m,km,cm;
cout<<"\n enter the length in meter";
cin>>m;
km=m/1000;
cm=m*100;
cout<<"\n kilometer="<<km;
cout<<"\n centimeter="<<cm;
getch();
}
9. Write a program to find the third angle of a triangle if two angles are given
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
a,b,c,d;
cout<<"\n enter the first angle";
cin>>a;
cout<<"\n enter the second angle";
cin>>b;
c=a+b;
d=180-c;
cout<<"\n the third angle of the triangle is"<<d;
getch();
}
10. Enter the temperature in centigrade and find the equivalent in Fahrenheit.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
clrscr();
float
c,f;
cout<<"\n enter the temperature in centigrade";
cin>>c;
f=(
(9*c) / 5 ) + 32;
cout<<"\ntemperature in fahrenheit="<<f;
getch();
}
11. Enter the radius of a circle and find the area, diameter and perimeter.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float
r,ar,peri,dia;
cout<<"\n enter the radius of circle";
cin>>r;
peri=2*3.14*r;
dia=r*2;
ar=3.14*r*r:
cout<<"\nperimeter="<<peri;
cout<<"\narea="<<ar;
cout<<"\ndiameter="<<dia;
getch();
}
Alternative Soln.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float
r,ar,peri,dia;
cout<<"\n enter the radius of circle";
cin>>r;
peri=2*3.14*r;
dia=r*2;
ar=3.14*pow(r,2):
cout<<"\nperimeter="<<peri;
cout<<"\narea="<<ar;
cout<<"\ndiameter="<<dia;
getch();
}
12. Write a program that calculates the volume of a sphere. Volume of a sphere is (4/3)*Ï€ *r3
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float
r,volume;
cout<<"\n enter the radius of the sphere";
cin>>r;
volume=(4*3.14*r*r*r)/3;
cout<<"\nvolume="<<volume;
getch();
}
13. Write a program that converts kilometers per hour to miles per hour. (1km = 0.621371 miles)
(
Expected Output :
Input kilometers per hour: 15
9.320568 miles per hour
)Input kilometers per hour: 15
9.320568 miles per hour
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float km,miles;
cout<<"\n enter the distance in kilometer per hour";
cin>>km;
miles=0.621371*km;
cout<<"\n “<<miles<< “miles per hour";
getch();
}
14. Write a program to read an amount (integer value) and break the amount into smallest possible number of bank notes.
(
e.g.
Input the amount: 485
Expected Output:
There are:
Expected Output:
There are:
0 Note(s) of 2000.00
0 Note(s) of 500.00
0 Note(s) of 500.00
2 Note(s) of 200.00
0 Note(s) of 100.00
1 Note(s) of 50.00
2 Note(s) of 20.00
0 Note(s) of 10.00
1 Note(s) of 5.00
0 Note(s) of 2.00
0 Note(s) of 1.00
0 Note(s) of 100.00
1 Note(s) of 50.00
2 Note(s) of 20.00
0 Note(s) of 10.00
1 Note(s) of 5.00
0 Note(s) of 2.00
0 Note(s) of 1.00
)
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int i;
cout<<"\n enter the amount";
cin>>i;
cout<<"\n2000 rs note : "<<i/2000;
i=i%2000;
cout<<"\n500 rs note : "<<i/500;
i=i%500;
cout<<"\n200 rs note : "<<i/200;
i=i%200;
cout<<"\n100 rs note : "<<i/100;
i=i%100;
cout<<"\n50 rs note : "<<i/50;
i=i%50;
cout<<"\n20 rs note : "<<i/20;
i=i%20;
cout<<"\n10 rs note : "<<i/10;
i=i%10;
cout<<"\n5 rs note : "<<i/5;
i=i%5;
cout<<"\n2 rs note : "<<i/2;
i=i%2;
cout<<"\n1 rs note : "<<i;
getch();
}
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
i,square,cube;
cout<<"\n enter the number";
cin>>i;
square=i*i;
cube=i*i*i;
cout<<"\n square="<<square;
cout<<"\n cube="<<cube;
getch();
}
Alternative Soln.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
int
i,square,cube;
cout<<"\n enter the number";
cin>>i;
square=
pow ( i,2 );
cube=
pow ( i,3 );
cout<<"\n square="<<square;
cout<<"\n cube="<<cube;
getch();
}
16. Enter a number find the double and triple of that number.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
i,d,t;
cout<<"\n enter the number";
cin>>i;
d=i*2;
t=i*3;
cout<<"\n double of "<<i<<" = "<<d;
cout<<"\n triple of "<<i<<" ="<<t;
getch();
}
17. Enter time in seconds , then show it in the Hour: Minute : Second format.
(
e.g.
Input seconds: 25300
Expected Output:
There are:
H:M:S - 7:1:40
)Expected Output:
There are:
H:M:S - 7:1:40
Soln.
#include<iostream.h>
#include<conio.h>
void main();
{
clrscr();
int
i,h,m,s;
cout<<"\n enter time in seconds";
cin>>i;
h=i/3600;
i=i%3600;
m=i/60;
s=i%60;
cout<<"\n H : M : S="<<h<<” :
“<<m<<” : “<<s;
getch();
}
18. Write a program to convert a given integer (in days) to years, months and days, assumes that all months have 30 days and all years have 365 days.
Input no. of days: 2535
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s).
Expected Output:
6 Year(s)
11 Month(s)
15 Day(s).
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
i,years,months,days;
cout<<"\n enter the period of time in days";
cin>>i;
years=i/365;
i=i%365;
months=i/30;
days=i%30;
cout<<"years="<<years;
cout<<"months="<<months;
cout<<"days="<<days;
getch();
}
19. Enter 2 numbers and store it into 2 variables a and b respectively. Swap the values of the variable.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
a,b,c;
cout<<"\n enter the first number";
cin>>a;
cout<<"\n enter the second number";
cin>>b;
c=a;
a=b;
b=c;
cout<<"\n swapped value of a="<<a;
cout<<"\n swapped value of b="<<b;
getch();
}
20. Enter 2 numbers and store it into 2 variables a and b respectively. Swap the values of the variable without using a third variable.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
a,b;
cout<<"\n enter the first number";
cin>>a;
cout<<"\n enter the second number";
cin>>b;
a=a+b;
b=a-b;
a=a-b;
cout<<"swapped value of a="<<a;
cout<<"swapped value of b="<<b;
getch();
}
21. Enter u,t and f and then find the distance (s) using the formula s=ut+(1/2)*ft2
Soln.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
float
u,t,f,distance;
cout<<"\n enter the value of u";
cin>>u;
cout<<"\n enter the value of t";
cin>>t;
cout<<"\n enter the value of f";
cin>>f;
//distance=(u*t)+(0.5*f*t*t);
distance=(u*t)+(0.5*f*pow(t,2));
cout<<"\n distance="<<distance;
getch();
}
22. Enter 2 numbers and find the modulus without using the modulus operator (%).
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
clrscr();
int
i,j,k,m;
cout<<"\n enter the first number\n";
cin>>i;
cout<<"\n enter the second number\n";
cin>>j;
k=i /
j;
m=i –
(j*k);
cout<<"\n the modulus is"<<m;
getch();
}
23. Write a program to enter P, T, R and calculate Simple Interest.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
clrscr();
int
p,t,r,SI;
cout<<"\n enter the principal";
cin>>p;
cout<<"\n enter the rate of interest';
cin>>r;
cout<<"\n enter the time";
cin>>t;
SI=p*r*t/100;
cout<<"\n Simple Interest="<<SI;
getch();
}
24. Write a program to enter P, T, R,N and calculate Compound Interest.
Soln.
#include<iostream.h>
#include<conio.h>
#include<math.h>
void main()
{
clrscr();
clrscr();
int
p,t,r,n,CI,A;
cout<<"\n enter the principal";
cin>>p;
cout<<"\n enter the
time";
cin>>t;
cout<<"\n enter the rate of interest";
cin>>r;
cout<<"\n enter the value of n";
cin>>n;
A=p*pow((1+r/n),(n*t));
CI=A-p;
cout<<"\n CI="<<CI;
getch();
}
25. Write a program to enter base and height of a right angled triangle and find its area.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{ clrscr();
float
base,height,area;
cout<<"\n enter the base of the triangle";
cin>>base;
cout<<"\n enter the height of the triangle";
cin>>height;
area=0.5*base*height;
cout<<"\n area="<<area;
getch();
}
26. Write a program to enter the coordinates (x,y) of 2 points and find the distance between those 2 points.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
x1,y1,x2,y2,dist;
cout<<"\n enter the first coordinate of point one";
cin>>x1;
cout<<"\n enter the second coordinate of point two";
cin>>y1;
cout<<"\n enter the first coordinate of point two";
cin>>x2;
cout<<"\n enter the second coordinate of point two";
cin>>y2;
dist=pow(pow(
(x1-x2) , 2) + pow( (y1-y2) , 2) , 0.5 );
cout<<"\n Distance="<<dist;
getch();
}
27. Write a program to enter two timings with hour, minute and second. Then add them and print the answer in the same format.
Soln.
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int
h1,m1,s1,h2,m2,s2,h,m,s,tm,ts;
// 1st
timing
cout<<"\n Enter the 1st timing ";
cout<<"\n Enter hour";
cin>>h1;
cout<<"\n Enter minute";
cin>>m1;
cout<<"\n Enter second";
cin>>s1;
// 2nd
timing
cout<<"\n Enter the 2nd timing ";
cout<<"\n Enter hour";
cin>>h2;
cout<<"\n Enter minute";
cin>>m2;
cout<<"\n Enter second";
cin>>s2;
ts =
s1 + s2;
s = ts % 60;
tm =
m1 + m2 + (ts / 60);
m = tm % 60;
h = h1 + h2 + (tm / 60 )
cout<<"\n total timing is "<<h<<" hours "<<m<<" minutes " << s<<"seconds";
getch();
}
Comments
Post a Comment