Solution of Excercises Unit 1
CHAPTER 1
1.1 Write a program that will print your mailing address in the following form :
First line : Name
Second line : Door No, Street
Third line : City, Pin code
Algorithm: -
Algorithm to print your mailing address.
Step 1 : Display your Name and Go to new line.
Step 2 : Display your Door No and Street and Go to new line.
Step 3 : Display your City and Pin Code.
Flowchart:-
START
Display your Name
& Go to new line
Display your Door
No and Street & go
to new line
Display your City
and Pin Code
END
Program: -
// Write a program that will print your mailing
//address in the following form:
//First line : Name
//Second line : Door No, Strret
//Third line : City, Pin Code
//Date : 11/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("Name :-- Ritesh Kumar Jain\n");
printf("Door No :-- 57 , Street :-- Parkho Ki Gali\n");
printf("City :-- Nimbahera , Pin Code :-- 312601");
getch();
}
Output:--
Name :-- Ritesh Kumar Jain |
Door No:-- 57, Street:- Parkho Ki Gali
City:-- Nimbahera, Pin Code:-- 312601
1.2 Modify the above program to provide border lines to the address.
Algorithm: -
Algorithm to provide border lines to address.
Step 1 : Display ------------------------------------------------- line and Go to new line.
Step 2 : Display ------------------------------------------------- line and Go to new line.
Step 3 : Display ||, your Name, || and Go to new line.
Step 4 : Display ||, your Door No and Street, || and Go to new line.
Step 5 : Display ||, your City, Pin Code, || and Go to new line.
Step 6 : Display ------------------------------------------------- line and Go to new line.
Step 7 : Display ------------------------------------------------- line.
Flowchart :-
START
Display ------------
& go to new line
Display ------------
& go to new line
A
A
Display ||, your Name, ||
& go to new line
Display ||, your Door No
and Street, || & go to new
line
Display ||, your City and
Pin Code, || and go to new
line
Display ------------
& go to new line
Display ------------
END
Program:-
// Write a program that will print your mailing
//address in the following form:
//-----------------------------------------
//-----------------------------------------
//|| First line : Name ||
//|| Second line : Door No, Strret||
//|| Third line : City, Pin Code ||
//-----------------------------------------
//-----------------------------------------
//Date : 11/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf(" -----------------------------------------------\n");
printf(" -----------------------------------------------\n");
printf("|| Name :-- Ritesh Kumar Jain ||\n");
printf("|| Door No :-- 57 , Street :-- Parkho Ki Gali ||\n");
printf("|| City :-- Nimbahera , Pin Code :-- 312601 ||\n");
printf(" -----------------------------------------------\n");
printf(" -----------------------------------------------\n");
getch();
}
Output:-
---------------------------------------------------
---------------------------------------------------
|| Name :-- Ritesh Kumar Jain ||
|| Door No:-- 57, Street:- Parkho Ki Gali||
||City:-- Nimbahera, Pin Code:-- 312601 ||
---------------------------------------------------
---------------------------------------------------
1.3 Write a program using one print statement to print the pattern of asterisks as
shown below :
*
* *
* * *
* * * *
Algorithm: -
Algorithm to print the pattern of pattern of asterisks.
Step 1: Display * and go to new line
Step 2: Display * * and go to new line.
Step 3: Display * * * and go to new line.
Step 4: Display * * * *
Flowchart:-
START
Display * & Go to
new line
Display * * & go to
new line
Display * * * & go
to new line
Display * * * *
Program :- END
//Write a program using one print statement to
//print the pattern of asterisks as shown below :
//*
//* *
//* * *
//* * * *
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("* \n* * \n* * * \n* * * *");
getch();
}
Output: -
*
* *
* * *
* * * *
1.4 Write a program that will print the following figure using suitable charactes.
------------- --------------
| | | |
| | >>------------------- | |
------------- ---------------
Algorithm:--
Algorithm to print the figure using suitable characters.
Step 1: Display ---------- and spaces
Step 2: Display ---------- and Go to new line
Step 3: Display | | and spaces
Step 4: Display | | and go to new line
Step 5: Display | |
Step 6: Display >>-----------
Step 7: Display | | and go to new line
Step 8: Display ---------- and spaces
Step 9: Display ----------
Flowchart:-
START
Display ---------- and spaces
Display ---------- and Go to new line
Display | | and spaces
Display | | and go to new line
Display | |
Display >>-----------
Display | | and go to new line
Display ---------- and spaces
Display ----------
END
Program:--
//Write a program that will print the following figure using suitable charactes.
// ------------- --------------
// | | | |
// | | >>-------------------> | |
// ------------- --------------
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("-------------");
printf(" ")
printf("-------------\n);
printf("| |");
printf(" ")
printf("| |\n");
printf("| |");
printf(">>---------->");
printf("| |\n");
printf("-------------");
printf(" ")
printf("-------------");
getch();
}
Output :--
---------------- ---------------
| | | |
| | >>-------------------> | |
--------------- ---------------
1.5 Given the radius of a circle, write a program to compute and display its area.
Use a symbolic constant to define the π value and assume a suitable value for
radius.
Algorithm:--
Algorithm to compute the area of circle.
Step 1: Store 3.14 to varable PIE.
Step 2: Store 4 to variable Rad.
Step 3: Compute the area of circle and assign it to variable Area.
Area = PIE * Rad * Rad
Step 4: Display the variable.
Flowchart:--
START
PIE = 3.14
Rad = 4
Area=PIE*Rad*Rad
Display
Area
END
Program :--
//Given the radius of a circle, write a program to compute
//and display its area. Use a symbolic constant to define the
//PIE value and assume a suitable value for radius.
#include<stdio.h>
#include<conio.h>
#Define PIE 3.14
void main()
{
clrscr();
float Rad,Area;
Rad=4;
Area=PIE*Rad*Rad;
printf("Area of a circle is--> %f",Area);
getch();
}
Output:--
Area of a circle is 50.240002
1.6 Write a program to output the following multiplication table.
5 * 1 =5
5 * 2 =10
5 * 3 =15
. .
. .
. .
5 * 10 =50
Algorithm:--
Algorithm to print multiplication table.
Step 1: Display 5 * 1 = 5 and go to new line
Step 2: Display 5 * 2 = 10 and go to new line
Step 3: Display 5 * 3 = 15 and go to new line
Step 4: Display 5 * 4 = 20 and go to new line
Step 5: Display 5 * 5 = 25 and go to new line
Step 6: Display 5 * 6 = 30 and go to new line
Step 7: Display 5 * 7 = 35 and go to new line
Step 8: Display 5 * 8 = 40 and go to new line
Step 9: Display 5 * 9 = 45 and go to new line
Step 10: Display 5 * 10 = 50 and go to new line
Flowchart:--
START
Display 5 * 1 = 5 and go to new line
Display 5 * 2 = 10 and go to new line
Display 5 * 3 = 15 and go to new line
Display 5 * 4 = 20 and go to new line
Display 5 * 5 = 25 and go to new line
Display 5 * 6 = 30 and go to new line
Display 5 * 7 = 35 and go to new line
Display 5 * 8 = 40 and go to new line
Display 5 * 9 = 45 and go to new line
Display 5 * 10 = 50 and go to new line
START
Program:--
//Write a program to output the following multiplication table.
// 5 * 1 =5
// 5 * 2 =10
// 5 * 3 =15
// . .
// . .
// . .
// 5 * 10 =50
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("5 * 1 = 5\n");
printf("5 * 2 = 10\n");
printf("5 * 3 = 15\n");
printf("5 * 4 = 20\n");
printf("5 * 5 = 25\n");
printf("5 * 6 = 30\n");
printf("5 * 7 = 35\n");
printf("5 * 8 = 40\n");
printf("5 * 9 = 45\n");
printf("5 * 10 = 50\n");
getch();
}
Output:--
5 * 1 =5
5 * 2 =10
5 * 3 =15
5 * 3 =20
5 * 3 =25
5 * 3 =30
5 * 3 =35
5 * 3 =40
5 * 3 =45
5 * 3 =50
1.7 Given two integers 20 and 10, write a program that uses a function add() to add
these two numbers and sub() to find the difference of these two numbers and
then display the sum and difference in the following form:
20 + 10 = 30
20 – 10 = 10
Algorithm:--
Step 1: Display First Number 20.
Step 2: Display Second Number 10.
Step 3: Call function add(20,10) to add these two numbers and store result in variable Sum.
Step 4: Call function sub(20,10) to Subtract these two numbers and store result in variable Diff.
Step 5: Display 20 + 10 =
Step 6: Display Sum and go to new line.
Step 7: Display 20 – 10 =
Step 6: Display Diff.
Flowchart:--
START
Display First Number 20
Display Second Number 10
Sum=20+10
Diff=20-10
Display 20 + 10 = and Sum
& go to new line
Display 20 - 10 = and Diff
END
Program:--
//Given two integers 20 and 10, write a program that
//uses a function add() to add these two numbers and
//sub() to find the difference of these two numbers
//and then display the sum and difference in the following form:
//20 + 10 = 30
//20 - 10 = 10
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int Sum,Diff;
printf("First Number 20\n");
printf("Second Number 10\n");
Sum=20+10;
Diff=20-10;
printf("20 + 10 = %d\n", Sum);
printf("20 - 10 = %d", Diff);
getch();
}
Output:--
20 + 10 = 30
20 – 10 = 10
1.8 Given the values of three variables a, b and c, write a program to compute and
display the values of x, where
X= a / (b - c)
Execute your program for the following values:
(a) a=250, b==85,c=25
(b) a=300, b=70, c=70
Comment on the output in each case.
Algorithm:--
Algorithm to compute the value of x.
Step 1: Store 250, 85 and 25 to variables a, b and c respectively.
Step 2: Compute a / ( b – c ) and store the result in variable x.
Step 3: Display x
Step 4: Store 300, 70 and 70 to variables a, b and c respectively.
Step 5: Compute a / ( b – c ) and store the result in variable x.
Step 6: Display x
Flowchart:--
START
a=250
b=85
c=25
x= a / (b – c)
Display
X
A
A
a=250
b=85
c=25
x= a / (b – c)
Display
X
START
Program:--
//Given the values of three variables a, b and c,
//write a program to compute and display the values of x, where
//X= a / (b - c)
//Execute your program for the following values:
//(a) a=250, b==85,c=25
//(b) a=300, b=70, c=70
//Comment on the output in each case.
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
float x;
a=250;
b=85;
c=25;
x=a/(b-c);
printf("x = %f\n",x);
a=300;
b=70;
c=70;
x=a/(b-c);
printf("x = %f\n",x);
getch();
}
Output:--
x=4.000000
Divide error
1.9 Relationship between Celsius and Fahrenheit is governed by the formula
F = (9C/5)+32
Write a program to convert the temperature
(a) from Celsius to Fahrenheit and
(b) from Fahrenheit to Celsius.
Algorithm:--
Algorithm to convert from Celsius to Fahrenheit and from Fahrenheit to Celsius.
Step 1: Store 0 to F & C.
Step 2: Store 200 to C.
Step 3: Compute ((9*c)/5)+32 and store the result in F.
Step 4: Display F.
Step 5: Store 300 to F.
Step 6: Compute ((F-32)*5)/9 and store the result in C.
Step 7: Display C.
Flowchart:--
START
F=0
C=0
C=200
F= (((9*C)/5)) +32
Display F
F=300
C= ((F-32)*5)/9
Display F
END
Program:--
//Relationship between Celsius and Fahrenheit is governed by the formula
//F = (9C/5)+32
//Write a program to convert the temperature
//(a) from Celsius to Fahrenheit and
//(b) from Fahrenheit to Celsius.
#include<stdio.h>
#include<conio.h>
void main()
{
float F,C;
clrscr();
C=200;
F=(((9*C)/5)+32);
printf("Celsius = %f to Fahrenheit = %f\n",C,F);
F=300;
C=((F-32)*5)/9;
printf("Fahrenheit = %f to Celsius = %f\n",F,C);
getch();
}
Output:--
Celsius =200.000000 to Fahrenheit = 392.000000
Fahrenheit = 300.000000 to Celsius = 148.888885
1.10 Area of a triangle is given by the formula
A=sqrt(S(S-a)(S-b)(S-c))
Where a, b and c are sides of the triangle and 2S=a+b+c. Write a program to
compute the area of the triangle given the values of a, b and c.
Algorithm:--
Algorithm to compute the area of a triangle.
Step 1: Store 0 to a, b ,c and S.
Step 2: Store 20, 30 and 40 to a, b and c respectively.
Step 3: Compute (a+b+c)/2 and store the result in S.
Step 4: Compute sqrt(S*(S-a)*(S-b)*(S-c)) and store the result in Area.
Step 5: Display Area.
Flowchart:--
START
a=0
b=0
c=0
S=0
a=20
b=30
c=40
S= (a+b+c)/c
Area=sqrt(S*(S-a)*(S-b)*(S-c))
END
Program:--
//Area of a triangle is given by the formula
//A=sqrt(S(S-a)(S-b)(S-c))
//Where a, b and c are sides of the triangle and 2S=a+b+c.
//Write a program to compute the area of the triangle
//given the values of a, b and c.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
float S,Area;
a=b=c=S=0;
clrscr();
a=20;
b=30;
c=40;
S=(a+b+c)/2;
Area=sqrt(S*(S-a)*(S-b)*(S-c));
printf("Area of a triangle is= %f",Area);
getch();
}
Output:--
Area of a triangle is= 290.473755
1.11 Distance between two points (x1,y1) and (x2,y2) is governed by the formula
D2 = (x2-x1)2+(y2-y1)2
Write a program to compute D given the coordinates of the points.
Algorithm:--
Algorithm to compute the distance between to points.
Step 1: Store 0 to D.
Step 2: Store 20,30,40 and 50 in x1,x2,y1and y2 respectively.
Step 3: Compute sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1)) and store the result in D.
Step 4: Display D.
Flowchart:-- START
D=0
x1=20
x2=30
y1=40
y2=60
D=sqrt(((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1))
Display D
END
Program:--
//Distance between two points (x1,y1) and (x2,y2) is governed by the formula
//D2 = (x2-x1)2+(y2-y1)2
//Write a program to compute D given the coordinates of the points.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x1,x2,y1,y2;
float D;
D=0;
x1=20;
x2=30;
y1=40;
y2=50;
clrscr();
D=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1));
printf("Distance between to points is= %f",D);
getch();
}
Output:--
Distance between twoo points is = 14.142136
1.12 A point on the circumference of a circle whose center is (0, 0) is (4, 5). Write
a program to compute perimeter and area of the circle.
Algorithm:--
Algorithm to compute perimeter and area of the circle.
Step 1: Store the coordinate of origin O1 and O2 to 0, 0 respectively.
Step 2: Store the coordinate of point x1 and y1 to 4, 5 respectively.
Step 3: Compute sqrt((x1-O1)*(x1-O1)+ (y1-O2)* (y1-O2)) and store the result in Rad.
Step 4: Compute 2*PIE*Rad and store the result in Circum.
Step 5: Compute PIE*Rad*Rad and store the result in Area.
Step 6: Display Circum & Area.
Flowchart:--
START
O1=0
O2=0
x1=4
x2=5
Rad= sqrt((x1-O1)*(x1-O1)+ (y1-O2)* (y1-O2))
Circum=2*PIE*Rad
Area= PIE*Rad*Rad
Display Circum
Display Area
START
Program:--
//A point on the circumference of a circle whose center is (0, 0) is (4, 5). Write
//a program to compute perimeter and area of the circle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PIE 3.14
void main()
{
int O1,O2,x1,y2;
float Rad,Circum,Area;
clrscr();
Rad=sqrt((x1-O1)*(x1-O1)+ (y1-O2)* (y1-O2)));
Circum=2*PIE*Rad;
Area=PIE*Rad*Rad;
printf("Circumference is= %f \n Area is= %f",Circum,Area);
getch();
}
Output:--
Circumference is= 40.211620
Area is= 128.740005
1.13 The line joining the points (2,2) and (5,6) which lie on the circumference of a circle is
the diameter of the circle. Write a program to compute the area of the circle.
Algorithm:--
Algorithm to compute the area of the circle.
Step 1: Store 2, 2, 5 and 6 in x1, y1, x2 and y2 respectively.
Step 2: Compute sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1)) and store the result in Dia.
Step 3: Compute Dia/2 and store the result in Rad.
Step 4: Compute PIE*Rad*Rad and store the result in Area.
Step 5: Display Area.
Flowchart:--
START
x1=2
y1=2
x2=5
y2=6
Dia= sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1))
Rad=Die/2
Area= PIE*Rad*Rad
Display Area
START
Program:--
//The line joining the points (2,2) and (5,6) which lie
//on the circumference of a circle is the diameter of the circle.
//Write a program to compute the area of the circle.
#include<stdio.h>
#include<conio.h>
#include<math.h>
#define PIE 3.14
void main()
{
int x1,y1,x2,y2;
float Die,Rad,Area;
clrscr();
x1=2;
y1=2;
x2=5;
y2=6;
Die=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1));
Rad=Die/2;
Area=PIE*Rad*Rad;
printf("Area is= %f",Area);
getch();
}
Output:--
Area is = 19.625000
1.14 Write a program to display the equation of a line in the form
ax+by=c
for a=5, b=8 and c=18.
Algorithm:--
Algorithm to display the equation.
Step 1: Store 5, 8 and 18 to a, b and c respectively.
Step 2: Display ax+by=c
Flowchart:--
START
a=5
b=8
c=18
Display ax+by=c
END
Program:--
//Write a program to display the equation of a line in the form
//ax+by=c
//for a=5, b=8 and c=18.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int a,b,c;
clrscr();
a=5;
b=8;
c=18;
printf(" %d x + %d y = %d",a,b,c);
getch();
}
Output:--
5 x + 8 y = 18
1.15 Write a program to display the following simple arithmetic calculator
x= y=
Sum= Difference=
Product= Division=
Algorithm:--
Algorithm to display simple arithmetic calculator.
Step 1: Store 6, 5 to x, y respectively.
Step 2: compute x+y and store the result in Sum.
Step 3: compute x-y and store the result in Diff.
Step 4: compute x*y and store the result in Prod.
Step 5: compute x/y and store the result in Div.
Step 6: Display Sum, Diff, Prod and Div.
Flowchart:--
START
Sum= x+y
Diff= x-y
Prod= x*y
Div= x/y
Display Sum
Display Diff
Disply Prod
Display Div
END
Program:--
//Write a program to display the following simple arithmetic calculator
//x= y=
//Sum= Difference=
//Product= Division=
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int x,y;
float Sum,Diff,Prod,Div;
clrscr();
x=6;
y=5;
Sum=x+y;
Diff=x-y;
Prod=x*y;
Div=x/y;
printf("x= %d y= %d\n",x,y);
printf("Sum= %f Difference= %f\n",Sum,Diff);
printf("Product= %f Dividion= %f",Prod,Div);
getch();
}
Output:--
x= 5 y= 6
Sum= 11.000000 Difference= 1.000000
Product= 30.000000 Division= 1.000000