Unit 1 - Exercise Solution
Unit 1 - Exercise Solution
CHAPTER 1
1.1 Write a program that will print your mailing address in the following form :
Algorithm: -
Flowchart:-
START
END
Program: -
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:--
1.2 Modify the above program to provide border lines to the address.
Algorithm: -
Flowchart :-
START
Display ------------
& go to new line
Display ------------
& go to new line
A
A
Display ------------
& go to new line
Display ------------
END
Program:-
#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: -
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:--
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:--
Flowchart:--
START
PIE = 3.14
Rad = 4
Area=PIE*Rad*Rad
Display
Area
END
Program :--
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
float Rad,Area;
Rad=4;
Area=PIE*Rad*Rad;
printf("Area of a circle is--> %f",Area);
getch();
}
Output:--
5 * 1 =5
5 * 2 =10
5 * 3 =15
. .
. .
. .
5 * 10 =50
Algorithm:--
Flowchart:--
START
START
Program:--
// 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:--
START
Sum=20+10
Diff=20-10
END
Program:--
//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;
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)
Algorithm:--
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:--
//X= a / (b - c)
#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
F = (9C/5)+32
Write a program to convert the temperature
(a) from Celsius to Fahrenheit and
(b) from Fahrenheit to Celsius.
Algorithm:--
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:--
//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);
F=300;
C=((F-32)*5)/9;
getch();
}
Output:--
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:--
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:--
//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));
getch();
}
Output:--
1.11 Distance between two points (x1,y1) and (x2,y2) is governed by the formula
D2 = (x2-x1)2+(y2-y1)2
Algorithm:--
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
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
#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();
getch();
}
Output:--
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:--
O1=0
O2=0
x1=4
x2=5
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>
void main()
{
int O1,O2,x1,y2;
float Rad,Circum,Area;
clrscr();
getch();
}
Output:--
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:--
Flowchart:--
START
x1=2
y1=2
x2=5
y2=6
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>
void main()
{
int x1,y1,x2,y2;
float Die,Rad,Area;
clrscr();
x1=2;
y1=2;
x2=5;
y2=6;
getch();
}
Output:--
Area is = 19.625000
ax+by=c
Algorithm:--
START
a=5
b=8
c=18
Display ax+by=c
END
Program:--
//ax+by=c
#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
x= y=
Sum= Difference=
Product= Division=
Algorithm:--
Flowchart:--
START
Sum= x+y
Diff= x-y
Prod= x*y
Div= x/y
Display Sum
Display Diff
Disply Prod
Display Div
END
Program:--
//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