0% found this document useful (0 votes)
37 views

Progrmming in Ansi C Structured Programming Language by e Balagurusamy Solutions With Flowchart Program

This document contains programs to print a mailing address and an asterisk pattern with borders. The first program prints a name, address, and city with pin code on separate lines. The second program adds top and bottom border lines with pipes around the address fields. The third program prints an ascending triangle of asterisks using a single print statement, with one more asterisk printed on each line.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Progrmming in Ansi C Structured Programming Language by e Balagurusamy Solutions With Flowchart Program

This document contains programs to print a mailing address and an asterisk pattern with borders. The first program prints a name, address, and city with pin code on separate lines. The second program adds top and bottom border lines with pipes around the address fields. The third program prints an ascending triangle of asterisks using a single print statement, with one more asterisk printed on each line.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 114

lOMoARcPSD|21254335 lOMoARcPSD|21254335

CHAPTER 1 11/03/2010
#include<stdio.h>
#include<conio.h>
1.1 Write a program that will print your mailing address in the following form :
void main()
First line : Name {
Second line : Door No, Street clrscr();
Third line : City, Pin code printf("Name :-- Ritesh Kumar Jain\n");
printf("Door No :-- 57 , Street :-- Parkho Ki Gali\n");
printf("City :-- Nimbahera , Pin Code :-- 312601");
Algorithm: - getch();
}
Algorithm to print your mailing address.

Step 1 : Display your Name and Go to new line. Output:--


Step 2 : Display your Door No and Street and Go to new line.
Step 3 : Display your City and Pin Code. Name :-- Ritesh Kumar Jain |
Door No:-- 57, Street:- Parkho Ki Gali
City:-- Nimbahera, Pin Code:-- 312601
Flowchart:-

START
1.2 Modify the above program to provide border lines to the address.

Algorithm: -

Display your Name Algorithm to provide border lines to address.


& Go to new line
Display your Door Step 1 : Display ------------------------------------------------- line and Go to new line.
No and Street & go Step 2 : Display ------------------------------------------------- line and Go to new line.
to new line Step 3 : Display ||, your Name, || and Go to new line.
Display your City Step 4 : Display ||, your Door No and Street, || and Go to new line.
and Pin Code 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 :-

END START

Display ------------
& go to new line
Display ------------
Program: - & go to new line
// 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 A
//Date : 11/03/2010

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

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");
A printf(" -----------------------------------------------\n");
printf(" -----------------------------------------------\n");
getch();
}
Display ||, your Name, ||
& go to new line Output:-
Display ||, your Door No
and Street, || & go to new ---------------------------------------------------
line ---------------------------------------------------
Display ||, your City and || Name :-- Ritesh Kumar Jain ||
Pin Code, || and go to new || Door No:-- 57, Street:- Parkho Ki Gali||
||City:-- Nimbahera, Pin Code:-- 312601 ||
line
---------------------------------------------------
---------------------------------------------------
1.3 Write a program using one print statement to print the pattern of asterisks as
shown below :

*
Display ------------ * *
& go to new line * * *
Display ------------ * * * *

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.
END Step 3: Display * * * and go to new line.
Step 4: Display * * * *
Program:- Flowchart:-

// Write a program that will print your mailing START


//address in the following form:
//-----------------------------------------
//----------------------------------------- Display * & Go to
//|| First line : Name || new line
//|| Second line : Door No, Strret|| Display * * & go to
//|| Third line : City, Pin Code || new line
//----------------------------------------- Display * * * & go
//----------------------------------------- to new line
//Date : 11/03/2010 Display * * * *

#include<stdio.h>
#include<conio.h>
void main()
{ Program :- END
clrscr();

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

//Write a program using one print statement to


//print the pattern of asterisks as shown below : Flowchart:-
START
//*
//* *
//* * *
//* * * *

#include<stdio.h>
#include<conio.h>
void main()
Display ---------- and spaces
{ Display ---------- and Go to new line
clrscr(); Display | | and spaces
printf("* \n* * \n* * * \n* * * *"); Display | | and go to new line
getch(); Display | |
}
Display >>-----------
Display | | and go to new line
Display ---------- and spaces
Display ----------
Output: -

*
* *
* * *
* * * * END

Program:--
1.4 Write a program that will print the following figure using suitable charactes. //Write a program that will print the following figure using suitable charactes.
------------- -------------- // ------------- --------------
| | | | // | | | |
| | >>------------------- | | // | | >>-------------------> | |
------------- --------------- // ------------- --------------

#include<stdio.h>
#include<conio.h>
Algorithm:-- void main()
{
Algorithm to print the figure using suitable characters. clrscr();
printf("-------------");
Step 1: Display ---------- and spaces printf(" ")
Step 2: Display ---------- and Go to new line printf("-------------\n);
Step 3: Display | | and spaces printf("| |");
Step 4: Display | | and go to new line printf(" ")
Step 5: Display | | printf("| |\n");
Step 6: Display >>----------- printf("| |");
Step 7: Display | | and go to new line printf(">>---------->");
Step 8: Display ---------- and spaces printf("| |\n");
Step 9: Display ----------

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("-------------"); //and display its area. Use a symbolic constant to define the
printf(" ") //PIE value and assume a suitable value for radius.
printf("-------------");
getch();
} #include<stdio.h>
#include<conio.h>
Output :--
#Define PIE 3.14
---------------- ---------------
| | | | void main()
| | >>-------------------> | | {
--------------- --------------- clrscr();

float Rad,Area;
1.5 Given the radius of a circle, write a program to compute and display its area. Rad=4;
Use a symbolic constant to define the π value and assume a suitable value for Area=PIE*Rad*Rad;
radius. printf("Area of a circle is--> %f",Area);
getch();
Algorithm:-- }

Algorithm to compute the area of circle. Output:--

Step 1: Store 3.14 to varable PIE. Area of a circle is 50.240002


Step 2: Store 4 to variable Rad.
Step 3: Compute the area of circle and assign it to variable Area.
Area = PIE * Rad * Rad 1.6 Write a program to output the following multiplication table.
Step 4: Display the variable.
5 * 1 =5
Flowchart:-- 5 * 2 =10
5 * 3 =15
START . .
. .
. .
PIE = 3.14
5 * 10 =50
Rad = 4
Algorithm:--

Area=PIE*Rad*Rad Algorithm to print multiplication table.

Step 1: Display 5 * 1 = 5 and go to new line


Display Step 2: Display 5 * 2 = 10 and go to new line
Area 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
END 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
Program :--

Flowchart:--
//Given the radius of a circle, write a program to compute

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

START
Output:--

Display 5 * 1 = 5 and go to new line 5 * 1 =5


Display 5 * 2 = 10 and go to new line 5 * 2 =10
Display 5 * 3 = 15 and go to new line 5 * 3 =15
Display 5 * 4 = 20 and go to new line 5 * 3 =20
Display 5 * 5 = 25 and go to new line 5 * 3 =25
Display 5 * 6 = 30 and go to new line 5 * 3 =30
Display 5 * 7 = 35 and go to new line 5 * 3 =35
Display 5 * 8 = 40 and go to new line 5 * 3 =40
Display 5 * 9 = 45 and go to new line 5 * 3 =45
Display 5 * 10 = 50 and go to new line 5 * 3 =50

START

Program:--

//Write a program to output the following multiplication table. 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
// 5 * 1 =5 then display the sum and difference in the following form:
// 5 * 2 =10
// 5 * 3 =15 20 + 10 = 30
// . . 20 – 10 = 10
// . .
// . .
// 5 * 10 =50

#include<stdio.h> Algorithm:--
#include<conio.h>
Step 1: Display First Number 20.
void main() Step 2: Display Second Number 10.
{ Step 3: Call function add(20,10) to add these two numbers and store result in variable Sum.
clrscr(); Step 4: Call function sub(20,10) to Subtract these two numbers and store result in variable Diff.
Step 5: Display 20 + 10 =
printf("5 * 1 = 5\n"); Step 6: Display Sum and go to new line.
printf("5 * 2 = 10\n"); Step 7: Display 20 – 10 =
printf("5 * 3 = 15\n"); Step 6: Display Diff.
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();
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- printf("20 - 10 = %d", Diff);

getch();
START }

Output:--
Display First Number 20
Display Second Number 10 20 + 10 = 30
20 – 10 = 10

1.8 Given the values of three variables a, b and c, write a program to compute and
Sum=20+10 display the values of x, where
Diff=20-10
X= a / (b - c)

Execute your program for the following values:


Display 20 + 10 = and Sum (a) a=250, b==85,c=25
& go to new line (b) a=300, b=70, c=70
Display 20 - 10 = and Diff
Comment on the output in each case.

Algorithm:--
END
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.
Program:--
Step 3: Display x
//Given two integers 20 and 10, write a program that 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.
//uses a function add() to add these two numbers and
Step 6: Display x
//sub() to find the difference of these two numbers
//and then display the sum and difference in the following form:
Flowchart:--
START
//20 + 10 = 30
//20 - 10 = 10
a=250
b=85
#include<stdio.h> c=25
#include<conio.h>

void main()
{ x= a / (b – c)
clrscr();

int Sum,Diff;
Display
printf("First Number 20\n");
X
printf("Second Number 10\n");

Sum=20+10;
Diff=20-10; A

printf("20 + 10 = %d\n", Sum);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

A a=300;
b=70;
c=70;
a=250 x=a/(b-c);
b=85
c=25 printf("x = %f\n",x);

getch();
}
x= a / (b – c)

Output:--
Display
X x=4.000000
Divide error

START

1.9 Relationship between Celsius and Fahrenheit is governed by the formula


Program:--
F = (9C/5)+32
//Given the values of three variables a, b and c, Write a program to convert the temperature
//write a program to compute and display the values of x, where (a) from Celsius to Fahrenheit and
(b) from Fahrenheit to Celsius.
//X= a / (b - c)

//Execute your program for the following values:


Algorithm:--
//(a) a=250, b==85,c=25
//(b) a=300, b=70, c=70
Algorithm to convert from Celsius to Fahrenheit and from Fahrenheit to Celsius.
//Comment on the output in each case.

Step 1: Store 0 to F & C.


#include<stdio.h> Step 2: Store 200 to C.
#include<conio.h> Step 3: Compute ((9*c)/5)+32 and store the result in F.
Step 4: Display F.
void main() Step 5: Store 300 to F.
{ Step 6: Compute ((F-32)*5)/9 and store the result in C.
clrscr(); Step 7: Display C.

int a,b,c;
float x;
a=250;
b=85;
c=25;
x=a/(b-c);

printf("x = %f\n",x);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
float F,C;
START clrscr();

C=200;
F=0 F=(((9*C)/5)+32);
C=0
printf("Celsius = %f to Fahrenheit = %f\n",C,F);

C=200 F=300;
C=((F-32)*5)/9;

printf("Fahrenheit = %f to Celsius = %f\n",F,C);


F= (((9*C)/5)) +32
getch();
}
Display F
Output:--

Celsius =200.000000 to Fahrenheit = 392.000000


F=300 Fahrenheit = 300.000000 to Celsius = 148.888885

C= ((F-32)*5)/9
1.10 Area of a triangle is given by the formula

A=sqrt(S(S-a)(S-b)(S-c))
Display F 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.

END
Algorithm:--

Program:--
Algorithm to compute the area of a triangle.
//Relationship between Celsius and Fahrenheit is governed by the formula

//F = (9C/5)+32
//Write a program to convert the temperature Step 1: Store 0 to a, b ,c and S.
//(a) from Celsius to Fahrenheit and Step 2: Store 20, 30 and 40 to a, b and c respectively.
//(b) from Fahrenheit to Celsius. 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.

#include<stdio.h>
#include<conio.h>

void main() Flowchart:--


{

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

b=30;
START c=40;

S=(a+b+c)/2;
a=0
b=0 Area=sqrt(S*(S-a)*(S-b)*(S-c));
c=0
S=0 printf("Area of a triangle is= %f",Area);

getch();
a=20 }
b=30
c=40 Output:--

Area of a triangle is= 290.473755


S= (a+b+c)/c
1.11 Distance between two points (x1,y1) and (x2,y2) is governed by the formula

D2 = (x2-x1)2+(y2-y1)2
Area=sqrt(S*(S-a)*(S-b)*(S-c))
Write a program to compute D given the coordinates of the points.

Algorithm:--

END 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.
Program:-- Step 3: Compute sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1)) and store the result in D.
Step 4: Display D.

//Area of a triangle is given by the formula Flowchart:-- START

//A=sqrt(S(S-a)(S-b)(S-c))
//Where a, b and c are sides of the triangle and 2S=a+b+c. D=0
//Write a program to compute the area of the triangle x1=20
//given the values of a, b and c. x2=30
y1=40
y2=60
#include<stdio.h>
#include<conio.h>
#include<math.h> D=sqrt(((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1))

void main()
{ Display D
int a,b,c;
float S,Area;
a=b=c=S=0; END

clrscr(); Program:--

a=20;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
//Distance between two points (x1,y1) and (x2,y2) is governed by the formula START

//D2 = (x2-x1)2+(y2-y1)2
O1=0
//Write a program to compute D given the coordinates of the points. O2=0
x1=4
#include<stdio.h> x2=5
#include<conio.h>
#include<math.h>

void main() Rad= sqrt((x1-O1)*(x1-O1)+ (y1-O2)* (y1-O2))


{
Circum=2*PIE*Rad
Area= PIE*Rad*Rad
int x1,x2,y1,y2;
float D;
D=0;
x1=20;
x2=30; Display Circum
y1=40; Display Area
y2=50;

clrscr(); START

D=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1)); Program:--


printf("Distance between to points is= %f",D); //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.
getch();
}
#include<stdio.h>
#include<conio.h>
#include<math.h>
Output:--
#define PIE 3.14
Distance between twoo points is = 14.142136 void main()
{
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. int O1,O2,x1,y2;
float Rad,Circum,Area;
Algorithm:--
clrscr();
Algorithm to compute perimeter and area of the circle.
Rad=sqrt((x1-O1)*(x1-O1)+ (y1-O2)* (y1-O2)));
Step 1: Store the coordinate of origin O1 and O2 to 0, 0 respectively. Circum=2*PIE*Rad;
Step 2: Store the coordinate of point x1 and y1 to 4, 5 respectively. Area=PIE*Rad*Rad;
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. printf("Circumference is= %f \n Area is= %f",Circum,Area);
Step 5: Compute PIE*Rad*Rad and store the result in Area.
Step 6: Display Circum & Area. getch();
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Output:-- #include<stdio.h>
#include<conio.h>
Circumference is= 40.211620 #include<math.h>
Area is= 128.740005
#define PIE 3.14
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. void main()
{
Algorithm:--
int x1,y1,x2,y2;
Algorithm to compute the area of the circle. float Die,Rad,Area;

Step 1: Store 2, 2, 5 and 6 in x1, y1, x2 and y2 respectively. clrscr();


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. x1=2;
Step 4: Compute PIE*Rad*Rad and store the result in Area. y1=2;
Step 5: Display Area. x2=5;
y2=6;
Flowchart:--
START Die=sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1));
Rad=Die/2;
Area=PIE*Rad*Rad;
x1=2
y1=2 printf("Area is= %f",Area);
x2=5
y2=6 getch();
}

Dia= sqrt((x2-x1)*(x2-x1)+ (y2-y1)* (y2-y1))


Rad=Die/2 Output:--
Area= PIE*Rad*Rad
Area is = 19.625000

1.14 Write a program to display the equation of a line in the form

Display Area ax+by=c

for a=5, b=8 and c=18.

START 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
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.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--

START Output:--

5 x + 8 y = 18
a=5 1.15 Write a program to display the following simple arithmetic calculator
b=8
c=18 x= y=
Sum= Difference=
Product= Division=

Display ax+by=c 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.
END
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.

Program:--
Flowchart:--
START
//Write a program to display the equation of a line in the form

//ax+by=c

//for a=5, b=8 and c=18. Sum= x+y


Diff= x-y
Prod= x*y
#include<stdio.h> Div= x/y
#include<conio.h>
#include<math.h>

Display Sum
void main() Display Diff
{ Disply Prod
Display Div
int a,b,c;

clrscr();
END
a=5;
b=8;
c=18;

printf(" %d x + %d y = %d",a,b,c);

getch();
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- CHAPTER 2 12/03/2010

2.1 Write a program to determine and print the sum of following harmonic series for
//Write a program to display the following simple arithmetic calculator given value of n:
1+1/2+1/3+…….+1/n.
//x= y= The value of n should be given interactively through the terminal.
//Sum= Difference=
//Product= Division=
Algorithm:--

Algorithm to display the sum of harmonic series.


#include<stdio.h>
#include<conio.h> Step 1. Store value 6 to n..
#include<math.h> Step 2. compute 1+1/2+1/3+1/4+1/5+1/n and store the result in Sum.
Step 3 Display Sum.

void main()
{ Flowchart:--

int x,y;

float Sum,Diff,Prod,Div;
START
clrscr();

x=6;
y=5; n=6

Sum=x+y;
Diff=x-y;
Prod=x*y; Sum =
Div=x/y; 1+1/2+1/3+1/4+1/5+1/n

printf("x= %d y= %d\n",x,y);
printf("Sum= %f Difference= %f\n",Sum,Diff); Display Sum
printf("Product= %f Dividion= %f",Prod,Div);

getch(); End
}

Output:-- Program:--

// Write a program to determine and print the sum of


x= 5 y= 6 //following harmonic series for given value of n:
Sum= 11.000000 Difference= 1.000000 //1+1/2+1/3+…….+1/n.
Product= 30.000000 Division= 1.000000 //The value of n should be given interactively through the terminal.

// Date: 12/03/2010

#include<stdio.h>
#include<conio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

void main() Program:--


{
//Write a program to read the price of an item in decimal
int n; //form and print it in paise (like 2563).
float sum;
//Date: 12/03/2010
clrscr();

n=6; #include<stdio.h>
#include<conio.h>
sum=1+1/2+1/3+1/4+1/5+1/n;

printf("Sum is %f",sum); void main()


{
getch();
} float Price;
Output:-- int Paise;

Sum is 1.000000 clrscr();

2.2 Write a program to read the price of an item in decimal form and print it in paise printf("Enter the price of Item in decimal\n");
(like 2563). scanf("%f",&Price);
Algorithm:--
Paise=Price*100;
Algorithm to display price of item in paise form.
printf("Price in Paise is %d ",Paise);
Step 1: Enter a value in Price.
Step 2: Compute Price*100 and store the result in Paise. getch();
Step 3: Display Paise. }
Flowchart:--

START Output:--

Enter the price of Item in decimal 27.68


Price in Paise is 2768
Enter
Price
2.3 Write a program that prints the even no. from 1 to 10.

Paise=Price*100
Algorithm:--

Algorithm to ptints even no. from 1 to 10.


Display Paise
Step 1: Store 2 to i.
Step 2: Display i.
End Step 3: Compute i=i+2 & Display i
Step 4: Compute i=i+2 & Display i.
Step 4: Compute i=i+2 & Display i
Step 5: Compute i=i+2 & Display i
Step 4: End

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

#include<conio.h>
Flowchart:-- Start
void main()
{

i=2 int i=2;

clrscr();

Display i printf("Even Numbers from 1 t0 10 are :--\n");


printf(“%d ”,i);
i=i+2;
i=i+2 printf(“%d ”,i);
i=i+2;
printf(“%d ”,i);
Display i i=i+2;
printf(“%d ”,i);
i=i+2;
printf(“%d ”,i);
i=i+2
getch();
}
Display i
Output:--

Even Numbers from 1 t0 10 are :--

i=i+2 2 4 6 8 10

2.4 Write a program that requests two float type numbers from the user and then
divides the first number by the second and display the result along with the
Display i numbers.

Algorithm:--
i=i+2
Algorithm to display the division of two numbers.

Display i Step1: Enter the first number and store in x


Step2: Enter the second number and store in y
Step3: Compute x/y and store in Div.
Step4: Display x, y and Div.
End

Program:--

//Write a program that prints the even no. from 1 to 100.

//Date: 12/03/2010

#include<stdio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- Output:--
START
Enter Two Values:--
42
Enter x x= 4.000000 y= 2.000000 Div= 2.000000
Enter y

Div=x/y
2.5 The price of one kg of rice is Rs. 16.75 and one kg of sugar is Rs.15. Write a
program to get these values from the user and display the prices as follows:
*** LIST OF ITEMS***
Item Price
Display x
Rice Rs. 16.75
Display y
Sugar Rs. 15.00
Display
Div

End
Algorithm:--

Algorithm to Display List of Items.

Step1: Enter the price of rice in RicePrice.


Step2: Enter the price of sugar in SugarPrice.
Step3: Display *** LIST OF ITEMS*** and go to new line.
Program:-- Step4: Display ITEM and spaces and PRICE and go to new line
Step5: Display Rice, spaces and RicePrice and go to nw line.
//Write a program that requests two float type numbers Step6: Display Sugar, spaces and SugarPrice.
//from the user and then divides the first number by the
//second and display the result along with the numbers. Flow Chart
Start

//Date: 12/03/2010
Enter RicePrice
Enter SugarPrice
#include<stdio.h>
#include<conio.h>
Display *** LIST OF
void main() ITEMS*** and go to new
{ line.
float x,y,Div; Display ITEM and spaces
and PRICE and go to new
clrscr(); line
Display Rice, spaces and
printf("Enter Two Values:--\n"); RicePrice and go to nw line.
scanf("%f %f",&x,&y); Display Sugar, spaces
and SugarPrice.
Div=x/y;

printf("x= %f y= %f Div= %f",x,y,Div);


getch();
}
End

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

a) Declare x and y as integer variables and z as a short integer variable.


Program:-- b) Assign two 6 digit numbers to x and y.
c) Assign the sum of x and y to z.
// The price of one kg of rice is Rs. 16.75 and one kg of sugar is Rs.15 d) Output the value of x, y and z.
// Write a program to get these values from the user and display the
// prices as follows: Comment on the output.
// *** LIST OF ITEMS***
// Item Price
// Rice Rs. 16.75
// Sugar Rs. 15.00 Algorithm:--

// Date: 12/03/2010 Algorithm to print the sum of to values.

Step 1: Enter the two integer values to variables x and y.


#include<stdio.h> Step 2: Compute the sum or x and y
#include<conio.h> Step 3: Store the result of Step 3 to short integer z.
Step 4: Display x, y and z.
void main()
{ Flowchart:-- START
float RicePrice,SugarPrice;

clrscr(); Enter x
Enter y
printf("Enter the price of Rice:\n");
scanf("%f",&RicePrice);
printf("Enter the price of Sugar:\n");
scanf("%f",&SugarPrice); Compute z=x+y

printf("***LIST OF ITEMS***\n");

printf("Item Price\n"); Display x, y


printf("Item Rs. %f\n",RicePrice); and z
printf("Rice Rs. %f\n",SugarPrice);

getch();
} END

Output:-- Program:-
Enter the price of Rice: 16.75 // Write a program to do the following:
Enter the price of Sugar: 15
//a) Declare x and y as integer variables and z as a short integer variable.
*** LIST OF ITEMS*** //b) Assign two 6 digit numbers to x and y.
Item Price //c) Assign the sum of x and y to z.
Rice Rs. 16.7500 //d) Output the value of x, y and z.
Sugar Rs. 15.0000
//Comment on the output.

//Date : 12/03/2010
2.7 Write a program to do the following:

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
#include<stdio.h> START
#include<conio.h>

void main() Enter x


{ Enter y
int x,y;
short int z;
Compute z=x+y
clrscr();

printf("Enter Two Values\n");


scanf("%d %d",&x,&y); Display x, y
and z
z=x+y;

printf("Values Are:--\n");
printf("x= %d , y= %d , z=%d",x,y,z); END

getch();
} Program:--

// Write a program to read two floating point numbers


Ouput:-- //using a scanf statement, assign their sum to an integer
//variable and then output the values of all the three variables.
Enter Two Values
123456 //Date : 12/03/2010
234567
Values Are:--
x= -7616 y= -27577 z= 30343 #include<stdio.h>
#include<conio.h>

void main()
{
2.8 Write a program to read two floating point numbers using a scanf statement, assign their
sum to an integer variable and then output the values of all the three variables. float x,y;
int z;

Algorithm:-- clrscr();
Algorithm to display the sum of two floating point numbers. printf("Enter Two Values\n");
scanf("%f %f",&x,&y);
Step 1: Enter the two floating point numbers to x and y.
Step 2: Compute the sum of x and y. z=x+y;
Step 3: Store the result of Step 2 to integer variable z.
Step 4: Display the values of x, y and z. printf("Values Are:--\n");
printf("x= %f , y= %f , z=%d",x,y,z);

getch();
}

Output:--

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Enter Two Values void main()


12.35 {
14.67
Values Are:-- typedef int NewData;
x= 12.350000 y= 14.670000 z= 27
NewData x;
2.9 Write a program to illustrate the use of typedef declaration in a program.
clrscr();
Algorithm:--
printf("Enter Value\n");
Algorithm to illustrate the use of typedef declaration. scanf("%d",&x);

Step 1: Declare a user define datatype NewData using typedef. printf("Value is:--\n");
Step 2: Declare a variable x using NewData. printf("x= %d",x);
Step 3: Enter value to x.
Step 4: Display the value of x. getch();
}

Flowchart:-- Output:--

START Enter value


2
Value is:--
X=2
Declare NewData
using typedef
Declare x using
NewData
2.10 Write a program to illustrate the use of symbolic constants in a real life application.

Enter
value x Algorithm:--

Algorithm to illustrate the use of symbolic constants.

Step 1: Declare symbolic constants PIE to value 3.14.


Display x
Step 2: Compute PIE*Rad*Rad
Step 3: Store the result of Step 2 to Area.
Step 4: Display Area.

END

Program:--
// Write a program to illustrate the use of typedef declaration in a program.

//Date : 12/03/2010

#include<stdio.h>
#include<conio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- getch();
}
START
Output:--

Enter Radius
Declare Symbolic 3
constant PIE to Area of Circle is:--
3.14 28.260000

Area=PIE*Rad*Rad

Display
Area

END

Program:--

// Write a program to illustrate the use of symbolic constants in a real


//life application.
//Date : 12/03/2010

#include<stdio.h>
#include<conio.h>

#define PIE 3.14

void main()
{

float Area;
int Rad;

clrscr();

printf("Enter Radius\n");
scanf("%d",&x);

Area=PIE*Rad*Rad;

printf("Area of Circle is:--\n");


printf("%f",Area);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

CHAPTER 3 12/03/2010 void main()


{
3.1 Given the values of the variables x, y and z, write a program to rotate their int x,y,z,Temp;
values such that x has the value of y, y has the value of z and z has the value of x.
clrscr();

Algorithm:-- printf("Enter Three Values\n");


scanf("%d%d%d",&x,&y,&z);
Step 1: Read x, y and z.
Step 2: Compute Temp=x. Temp=x;
Step 3: Compute x=y. x=y;
Step 4: Compute y=z. y=z;
Step 5: Compute z=Temp. z=Temp;
Step 6: Display x, y and z.
printf(" x= %d \n y= %d \n z= %d",x,y,z);

Flow Chart:-- getch();


}
Output
Start
Enter Values of x, y and z
123
Read x,y
and z x=2
y=3
z=1
Temp=x
x=y
y=z
z=temp 2.2 Write a program that reads a floating-point number and then displays the right
–most digit of the integral part of the number.

Display x
Display y
Display z
Algorithm:--

Step 1: Read x where x is float type.


Step 2: Compute y=x where y is integer type.
End Step 3: compute y %10 and store the result in z.
Step 4: Display z.

Program

// Given the values of the variables x, y and Z, write a program to rotate their values such
//that x has the value of y, y has the value of z and z has the value of x

//Date : 12/03.2010

#include<conio.h>
#include<stdio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flow Chart:-- 3.3 Write a program that reads a floating-point number and then displays the two
right –most digit of the integral part of the number.
Start
Algorithm:--

Step 1: Read x where x is float type.


Read x Step 2: Compute y=x where y is integer type.
Step 3: compute y %100 and store the result in z.
Step 4: Display z.

y=x Flow Chart:--


z= y%10
Start

Display z
Read x

End
y=x
Program z= y%100

// Write a program that reads a floating-point number and then displays the right –most
//digit of the integral part of the number.
Display z
// Date 12/03/2010

#include<stdio.h>
#include<conio.h> End

void main() Program


{
float x; // Write a program that reads a floating-point number and then displays the two right –
int y,z; //most digit of the integral part of the number.

clrscr(); // Date 12/03/2010

printf("Enter floating point number : x= "); #include<stdio.h>


scanf("%f",&x); #include<conio.h>

y=x; void main()


z=y%10; {
float x;
printf(" \nThe Right-most digit of the integral part of the number %f is %d",x,z); int y,z;
getch();
} clrscr();

Output printf("Enter floating point number : x= ");


scanf("%f",&x);
Enter floating point number : x= 172.34
The Right-most digit of the integral part of the number 172.34 is 3. y=x;
z=y%100;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

#include<conio.h>
printf(" \nThe two Right-most digit of the integral part of the number %f is
%d",x,z); void main()
getch(); {
}
int Len,Wid,Area,Peri;
Output
clrscr();
Enter floating point number : x= 172.34
The two Right-most digit of the integral part of the number 172.34 is 72 printf("Enter the length of the rectangle :\n");
scanf("%d",&Len);
3.4 Write a program that will obtain the length and width of a rectangle from the
user and compute its area and perimeter. printf("Enter width of the rectangle :\n");
scanf("%d",&Wid);

Algorithm-- Peri= 2*(Len+Wid);

Step 1: Read Len and Wid. Area= Len*Wid;


Step 2: Compute 2*(Len+Wid) and store the result in Peri.
Step 3: Compute Len*Wid and store the result in Area. printf("The perimeter of the rectangle is =%d \n",Peri);
Step 4. Display Peri,Area. printf("The area of the rectangle is =%d \n",Area);

Flow Chart-- getch();


}
Start
Output

Read Len Enter the length of the rectangle :20


and Wid Enter width of the rectangle : 40

The perimeter of the rectangle =120


Peri= 2*(Len+Wid) The area of the rectangle =800

Area= Len*Wid
3.5 Given an integer number, write a program that displays the number as follows:

First line: all digits


Display Peri Second line: all except first digit
Display Area Third line: all except first two digits
……
Last line : The last digit.
End
Algorithm
Program
Step 1: Read x (Only 4 Digit number).
//Write a program that will obtain the length and width of a rectangle from the user and Step 2: Compute x%1000 and store the result in a.
//compute its area and perimeter. Step 2: Compute a%100 and store the result in b.
Step 2: Compute b%10 and store the result in c.
// Date: 12/03/2010 Step5. Display x and go to new line.
Step6. Display a and go to new line.
#include<stdio.h> Step7. Display b and go to new line.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Step8. Display c. printf("Enter a four digit number: ");


scanf("%d",&x);

a=x%1000;
Flow Chart b=a%100;
c=b%10;
Start
printf("%d\n",x);
printf("%d\n",a);
printf("%d\n",b);
Read x printf("%d\n",c);

getch();
}
a=x%1000
b=a%100
c=b%10 Output

Enter a four digit number: 1234


Display x, go to new line 1234
Display a, go to new line 234
Display b, go to new line 23
Display c, go to new line 2

End

Program 3.6 The straight line method of computing the yearly depreciation of the value of an
item is given by:
//Given an integer number, write a program that displays the number as follows:
Depreciation = Purchase price- Salvage Value
//First line: all digits Years of Service
//Second line: all except first digit
//Third line: all except first two digits Write a Program to determine the salvage value of an item when the purchase price,
//.... years of service and the annual depreciation are given.
//Last line : The last digit.

// Date: 12/03/2010

#include<stdio.h>
#include<conio.h> Algorithm:--

void main() Step 1: Read Dep,Year_Ser, Pur_Price.


{ Step 2: Compute Pur_Price-(Dep*Year_Ser) and store in variable Sal_Price.
Step4. Display Sal_Price.
int x,a,b,c;

clrscr();

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flow Chart:-- Sal_Price = Pur_Price-(Dep*Year_Ser);

printf("The salvage value of an item = %f ",Sal_Price);


Start
getch();
}

Read Output:--
Dep,Year_Ser, Enter Deperaciation, Year of Service, Purchase price
Pur_Price. 23
2
342
The salvage value of an item = 296.000000

Sal_Price =
Pur_Price-(Dep*Year_Ser) 3.7 Write a program that will read a real no. from the keyboard and print the
output in one line:

Smallest integer not the given no. largest integer not


Display less than the number greater than the no.
Sal_Price
Algorithm:--

Step 1: Input float value to RealNo


Step 2: Compute SmallNo=RealNo where SmallNo is integer type.
End Step 3: Compute LargeNo=RealNo+1 where LargeNo is integer type.
Step 4: Display SmallNo, RealNo, LargeNo.

Program Flowchart:--

//The straight line method of computing the yearly depreciation of the value of an item is
//given by:
// Depreciation = (Purchase price- Salvage Value)/Years of Service START

//Write a Program to determine the salvage value of an item when the purchase price,
//years of service and the annual depreciation are given.*/
Read
RealNo
// Date 12/03/2010

#include<stdio.h> SmallNo=RealNo
#include<conio.h> LargeNo=RealNo+1

void main()
{
Display SmallNo,
RealNo, LargeNo
float Dep,Year_Ser,Pur_Price,Sal_Price;

clrscr();
END
printf("Enter Deperaciation, Year of Service, Purchase price\n");
scanf("%f%f%f",&Dep,&Year_Ser,&Pur_Price);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:

//Write a program that will read a real no. from the keyboard and print the
//output in one line: 3.8 The total distance traveled by a vehicle, a is the acceleration. Write a program to
calculate the distance travelled by at regular intervals of time ,given the values
//Smallest integer not the given no. largest integer not of u and a.the program should be flexible to the user select his own time interval
//less than the number greater than the no. and repeat the calculation for diff values of u,a.

//Date: 12/03/2010
Algorithm:--

#include<stdio.h> Step 1: Read u,a,t.


#include<conio.h> Step 2: Compute (u*t)+(a*(t*t))/2 and store the result in Dis.
Step 3: Display Dis.

void main() Flowchart:--


{

int SmallNo,LargeNo;
float RealNo; START

clrscr();

printf("Enter the real no."); Read u, a and t


scanf("%f",& RealNo);

SmallNo=RealNo;
LargeNo=RealNo; Compute
Dis=(u*t)+(a*(t*t))/2
printf("Smallest integer not ");
printf("The given no. ");
printf("Largest integer not \n");
Display Dis
printf("less than the number ");
printf(" greater than the no.\n");

printf("%d ", SmallNo);


END
printf("%f ", RealNo);
printf("%d ", LargeNo);

getch(); Program:--
}
//The total distance traveled by a vehicle, a is the acceleration. Write a program to
//calculate the distance travelled by at regular intervals of time ,given the values
Output:-- //of u and a.the program should be flexible to the user select his own time interval
//and repeat the calculation for diff values of u,a.
Enter a Real no.
25.32
//Date: 12/03/2010
Smallest integer not the given no. Largest integer not
less than the number greater than the no.
#include<stdio.h>
25 25.32 25
#include<conio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

void main() Flowchart:--


{

int u,t,a;
float Dis; START

clrscr();
Read Dr,Sc,Hc
printf("Enter the value of u,a and t\n");
scanf("%d %d %d",&u,&a,&t);

Dis=(u*t)+(a*(t*t))/2;
EOQ=sqrt((2*Dr*Sc)/Hc)
printf("The distance is : %f \n",Dis); TBO=sqrt((2*Sc)/(Dr*Hc))

getch();
}
Display EOQ
Display TBO
Output:--

Enter the value of u,a and t


234
The distance is : 36.000000 END

3.9 In inventory management, the economy order quantity for a single item is given Program:--
by
//In inventory management, the economy order quantity for a single item is given
EOQ = sqrt((2*dr*sc)/hc) //by

and the optimal time between orders //EOQ = sqrt((2*dr*sc)/hc)

TBO= sqrt((2*sc)/(dr*hc)) // and the optimal time between orders

Write a program to compute EOQ and TBO ,given demand rate ,setup costs,and //TBO= sqrt((2*sc)/(dr*hc))
the holding cost.
// Write a program to compute EOQ and TBO ,given demand rate ,setup costs,and
// the holding cost.

Algorithm:--
//Date: 12/03/2010
Step 1: Read Dr,Sc,Hc.
Step 2: Compute sqrt((2*Dr*Sc)/Hc)and store the result in EOQ
Step 3: Compute sqrt((2*Sc)/(Dr*Hc)) and store the result in TBO #include<stdio.h>
Step 4: Display EOQ, TBO #include<conio.h>
#include<math.h>

void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

{ Flowchart:--

float Dr,Sc,Hc;
float TBO,EOQ;
START
clrscr();

printf("Enter Demand Rate \n");


scanf("%f\n",&Dr); Read L,R,C
printf("Enter Setup Cost \n");
scanf("%f\n",&Sc); Temp1= (1/(L*C))
Temp2= ((R*R)/(4*C*C))
printf("Enter Holding Cost \n"); Freq= sqrt(Temp1-Temp2)
scanf("%f\n",&Hc);

EOQ=sqrt((2*Dr*Sc)/Hc);
Display Freq
TBO=sqrt((2*Sc)/(Dr*Hc));

printf("The Economic Order Quantity is : %f\n",EOQ);


printf("The time Between Order is : %f",TBO);
END
getch();
}

Output:--

Enter Demand Rate 23 Program:


Enter Setup Cost 24
Enter Holding Cost 45 //For a certain electrical circuit with an inductance L and resistance R,the
The Economic Oder Quantity is: 1.384437 // damped natural frequency is given by frequency is
The time Between Order is: 0.060193
//Frequency= sqrt((1/LC)-(R*R)/(4*C*C))

3.10 For a certain electrical circuit with an inductance L and resistance R,the // It is desired to study the variation of this frequency with c,write a program to
damped natural frequency is given by frequency is // calculate the frequency for diff values of c starting from .01 to.10(interval is
// .01).
Frequency= sqrt((1/LC)-(R*R)/(4*C*C))

It is desired to study the variation of this frequency with c,write a program to


calculate the frequency for diff values of c starting from .01 to.10(interval is //Date: 12/03/2010
.01).

Algorithm:-- #include<stdio.h>
#include<conio.h>
Step 1: Read L,R,C. #include<math.h>
Step 2:Compute (1/(L*C)) andc store the result in Temp1.
Step 3:Compute ((R*R)/(4*C*C)) and store the result in Temp2.
Step 4:Compute sqrt(Temp1-Temp2) and store the result in Freq. void main()
Step 5: Display Freq. {

double L,R,C;
double Freq,Temp1,Temp2;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
clrscr(); START

printf("Enter Inductance, Resistance, Capacitannce \n");


scanf("%lf %lf %lf",&L,&R,&C); Read Num &
Sum1=Sum2=Sum3=Sum4=0
Temp1= (1/(L*C))
Temp2= ((R*R)/(4*C*C))
Freq= sqrt(Temp1-Temp2) Sum1=Num%10
Num=Num/10
printf("The Frequency is : %lf\n",Freq);
Sum1= Num%10
getch(); Num=Num/10
}
Output:--

Enter Inductance, Resistance, Capacitannce Sum1= Num%10


234 Num=Num/10
The Frequency is :

Sum1= Num%10
Num=Num/10

3.11 Write a program to read a four digit integer and print the sum of its digits.
Hint: Use / and % operators. Display Sum

END

Algorithm:-- Program:--
Algorithm to find the sum of digits of a number.
//Write a program to read a four digit integer and print the sum of its digits.
Step 1: Read Num. // Hint: Use / and % operators.
Step 2: Store 0 to Sum,Sum1,Sum2,Sum3,Sum4.
Step 3: Compute Num %10 & store the result in Sum1.
Step 5: Compute Num/10 & store the result in Num. //Date: 12/03/2010
Step 3: Compute Num %10 & store the result in Sum2.
Step 5: Compute Num /10 & store the result in Num.
Step 3: Compute Num %10 & store the result in Sum3. #include<stdio.h>
Step 5: Compute Num /10 & store the result in Num. #include<conio.h>
Step 3: Compute Num %10 & store the result in Sum4.
Step 6: Compute Sum1+Sum2+Sum3+Sum4 & store the result in Sum. void main()
Step 7: Display Sum. {

int Num,Sum,Sum1,Sum2,Sum3,Sum4;

Sum1=Sum2=Sum3=Sum4=0;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
Sum=0;
START
clrscr();

printf("Enter a Four Digits Number\n",&Num); Display sizeof(int)


scanf("%d",&Num); Display sizeof(char)
Display sizeof(float)
Sum1=Num%10; Display sizeof(double)
Num=Num/10;

Sum2=Num%10;
Num=Num/10; END

Sum3=Num%10;
Num=Num/10;

Sum4=Num%10;
Num=Num/10;

Sum=Sum1+Sum2+Sum3+Sum4;
Program:--
printf("\nSum of Digits are :-- %d\n",Sum);
//Write a program to print the size of various data types in C.
getch();
}
Output:-- //Date: 12/03/2010
Enter a Four Digits Number
1234 #include<stdio.h>
Sum of Digits are :-- 10 #include<conio.h>

void main()
3.12 Write a program to print the size of various data types in C. {

clrscr();
Algorithm:--
printf("Size of Integer Data Type :-- %d \n",sizeof(int));
printf("Size of Character Data Type :-- %d \n",sizeof(char));
Algorithm to print the size of various data types in C. printf("Size of Float Data Type :-- %d \n",sizeof(float));
printf("Size of Double Data Type :-- %d \n",sizeof(double));
Step 1: Display the size of integer datatype using sizeof(int) function.
Step 2: Display the size of character datatype using sizeof(char) function. getch();
Step 3: Display the size of float datatype using sizeof(float) function. }
Step 4: Display the size of double datatype using sizeof(double) function.
Output:--

Size of Integer Data Type :-- 2


Size of Character Data Type :-- 1
Size of Float Data Type :-- 4
Size of Double Data Type :-- 8

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

3.13 Given three values, write a program to read three values from keyboard and print out the {
largest of them without using if statement.
int x,y,z;
Algorithm:--
clrscr();
Algorithm to find largest between three values.
printf("Enter Three Numbers:--\n");
Step 1: Input three values from keyboard & store in x, y and z. scanf("%d %d %d",&x,&y,&z);
Step 2: Check ((x>y)&(y>z)) then largest is x otherwise go to Step 3.
Step 3: Check ((y>x)&(x>z)) then largest is y otherwise go to Step 4. ((x>y)&&(x>z))?printf("Largest is x :-- %d",x):((y>x)&&(y>z))?printf("Largest is y :--
Step 4: Check ((z>y)&(y>x)) then largest is z. %d",y):printf("Largest is z :-- %d",z);
Step 5: End.
getch();
Flowchart:-- }
Output:--
START
Enter Three Numbers:--
345
Input x, Largest is z :-- 5
y&z

NO NO 3.14 Write a program to read two integer values m and n and to decide and print whether m is
(x>y)& (y>x)& (z>y)&
(z>x) multiple of n.
(x>z) (y>z)

YES YES YES


Display x Display y Display z Algorithm:--

Algorithm to find whether m is multiple of n.

END Step 1: Input two values.


Step 2: Store these values in m & n.
Step 3: Compute m%n and store the result in x.
Step 4: Check x==0
Step 5: If Step 4 is true then m is multiple of n.
Program:-- Step 6: If Step 4 is false then m is not multiple of n.

//Given three values, write a program to read three values


//from keyboard and print out the largest of them without using if statement.

//Date: 12/03/2010

#include<stdio.h>
#include<conio.h>

void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- getch();
START
}

Input m Output:--
&n
Enter Two Numbers:--
6
3
x= m%n m is multiple of n

x==0 m is multiple
of n 3.15 Write a program to read three values using scanf statement and print the following
results:
a) Sum of the values
b) Average of the three values
m is not c) Largest of three values
multiple of n d) Smallest of three values

Algorithm:--
END
Algorithm to find various results.

Step 1: Input three values.


Step 2: Store these values in x, y and z.
Program:-- Step 3: Compute x+y+z and store the result in Sum.
Step 4: Display Sum.
//Write a program to read two integer values m and n and Step 5: Compute (x+y+z)/3 and store the result in Ave.
//to decide and print whether m is multiple of n. Step 6: Display Ave.
Step 7: Check ((x>y)&(x>z)) then largest is x otherwise go to Step 3.
Step 8: Check ((y>x)&(y>z)) then largest is y otherwise go to Step 4.
//Date: 12/03/2010 Step 9: Check ((z>y)&(z>x)) then largest is z.
Step 10: Check ((x<y)&(x<z)) then smallest is x otherwise go to Step 3.
Step 11: Check ((y<x)&(y<z)) then smallest is y otherwise go to Step 4.
#include<stdio.h> Step 12: Check ((z<y)&(z<x)) then smallest is z.
#include<conio.h>

void main()
{

int m,n,x;

clrscr();

printf("Enter Two Numbers:--\n");


scanf("%d %d",&m,&n);

x=m%n;

(x==0)?printf("m is multiple of n\n"):printf("m is not multiple of n\n");

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
START A

Input x,
y&z

Sum=x+y+z
NO NO
(x<y)& (y<x)& (z<y)&
(x<z) (y<z) (z<x)
Display Sum
YES YES YES
Ave=(x+y+z)/3 Display x Display y Display z

Display Ave
END

Program:--

NO NO
(z>y)& //Write a program to read three values using scanf statement and print the following
(x>y)& (y>x)&
(z>x) // results:
(x>z) (y>z)
// a) Sum of the values
YES YES // b) Average of the three values
YES // c) Largest of three values
Display x Display y Display z // d) Smallest of three values

//Date: 12/03/2010
A
#include<stdio.h>
#include<conio.h>

void main()
{

int x,y,z,Sum;
float Ave;

clrscr();

printf("Enter Three Numbers:--\n");


scanf("%d %d %d",&x,&y,&z);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Sum=x+y+z;
printf("Sum= %d\n",Sum);

Ave=(x+y+z)/3;
printf("Average= %d\n",Ave);
Flowchart:-- START
((x>y)&&(x>z))?printf("Largest is x :-- %d\n",x):((y>x)&&(y>z))?printf("Largest is y :--
%d\n",y):printf("Largest is z :-- %d\n",z);

((x<y)&&(x<z))?printf("Smallest is x :-- %d\n",x):((y<x)&&(y<z))?printf("Smallest is y :-- Input number of calls of customer1 and


%d\n",y):printf("Smallest is z :-- %d\n",z); store in Cus1.
Input number of calls of customer2 and
store in Cus2.

getch();
}
YES NO
Output:-- Cus1<= Bill1=(250+(1.25*
Bill1=250 100 Cus1))
Enter Three Numbers:--
234
Sum= 9
Average= 3
Largest is z :-- 4
Smallest is x :-- 2

3.16 The cost of one type of mobile service is Rs. 250 plus Rs. 1.25 for each call made over and YES NO
Cus1<=
above 100 calls. Write a program to read customer codes and calls made and print the bill Bill1=250 100 Bill1=(250+(1.25*
for each customer. Cus1))

Algorithm:--

Algorithm to print the mobile bill of each customer.


Display Bill1.
Step 1: Input number of calls of customer1 and store in Cus1.
Display Bill2.
Step 2: Input number of calls of customer2 and store in Cus2.
Step 3: Check Cus1<=100 then store Bill1=250 otherwise compute (250+(1.25*Cus1)) & store
in Bill11.
Step 4: Check Cus2<=100 then store Bill2=250 otherwise compute (250+(1.25*Cus2)) & store
in Bill12. END
Step 5: Display Bill1.
Step 6: Display Bill2.

Program:--

//The cost of one type of mobile service is Rs. 250 plus Rs. 1.25
//for each call made over and above 100 calls. Write a program to
//read customer codes and calls made and print the bill for each customer.

//Date: 12/03/2010

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

CHAPTER 4 13/03/2010
#include<stdio.h> 4.1 Given the string ”WORDPROCESSING”, write a program to read the string
#include<conio.h> from the terminal and display the same in the following formats:
a. WORD PROCESSING.
void main() b. WORD
{ PROCESSING
c. W.P.
float Cus1,Cus2,Bill1,Bill2;

clrscr(); Algorithm:--

printf("Enter Numbers of Call of Customer 1:--\n"); Step 1. Store Str1=”WORD” and Str2=”PROCESSING”.
scanf("%f",&Cus1); Step 2. Display Str1, Space and Str2.
Step 3. Display Str1, then new line and Str2..
printf("Enter Numbers of Call of Customer 2:--\n"); Step 4. Display First character of Str1 and Str2.
scanf("%f",&Cus2);
Flowchart:--
Cus1<=100?Bill1=250:Bill1=(250+Cus1*1.25);
Cus2<=100?Bill2=250:Bill2=(250+Cus1*1.25);
START
printf("Mobile Bill of Customer 1:-- %f\n",Bill1);
printf("Mobile Bill of Customer 2:-- %f",Bill2);
Str1=”WORD”
getch();
Str2=”PROCESSING”.
}

Display Str1, Space and Str2.


Output:-- Display Str1, then new line and
Str2.
Enter Numbers of Call of Customer 1:-- Display First character of Str1
100 and Str2.
Enter Numbers of Call of Customer 2:--
Mobile Bill of Customer 1:--
250.000000
Mobile Bill of Customer 2:--
END
375.000000
Program:--

//Given the string”WORDPROCESSING”, write a program to read the string from //the
terminal and display the same in the following formats:
//WORD PROCESSING.
//WORD
//PROCESSING
//W.P.

//Date: 13/03/2010

#include<stdio.h>
#include<conio.h>
#include<math.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

void main() Flowchart:--

char Str1[10]="WORD"; START


char Str2[10]="PROCESSING";

clrscr();
Read x,y.
printf("%s ",Str1);
printf(" %s\n",Str2);
a=((x+y)/(x-y))
printf("%s\n",Str1); b=(x+y)/2
printf("%s\n",Str2); c=(x+y)*(x-y)
printf("%.1s.",Str1);
printf("%.1s.",Str2);
Display a,b,c
getch();
}
END
Output:--
Program:--
WORD PROCESSING
WORD //write a program to read values of x,y and print as
PROCESSING
W.P. //x+y/x-y x+y/2 (x+y)(x-y)

//Date: 13/03/2010

#include<stdio.h>
4.2 write a program to read values of x,y and print as #include<conio.h>
#include<math.h>
x+y/x-y x+y/2 (x+y)(x-y)
void main()

{
Algorithm:-- float x,y,a,b,c;

Step 1: Read x,y. clrscr();


Step 2: Compute a=((x+y)/(x-y))
Step 3: Compute b=(x+y)/2 printf(" Enter x\n");
Step 4: Compute c=(x+y)*(x-y) scanf("%f",&a);
Step 5: Display a,b,c.
printf("Enter y\n");
scanf("%f",&y);

a=((x+y)/(x-y));
b=(x+y)/2;
c=(x+y)*(x-y);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("%f %f %f",a,b,c);

getch();
}
Program :--
Output:--
//Write a program to read the following numbers, round them off to the nearest
//integer and print out the result as:
Enter x 5 //35.7 50.21 -23.73 -46.45
Enter y 3
4.000000 4.000000 16.000000 //Date: 13/03/2010

#include<stdio.h>
#include<conio.h>
4.3 Write a program to read the following numbers, round them off to the nearest #include<math.h>
integer and print out the result as:
35.7 50.21 -23.73 -46.45
void main()

Algorithm:-- {

Step 1: Read float value a,b,c. int x,y,z;


Step 2: Compute x=ceil(a), y=ceil(b), z=ceil(c). float a,b,c;
Step 3: Display x,y,z.
clrscr();
Flowchart:--
printf(" Enter three value a, b, c:--\n");
scanf(" %f %f %f",&a,&b,&c);

START x=ceil(a);
y=ceil(b);
z=ceil(c);
Read float value a,b,c
printf("Values Are:-\n") ;

printf("%d ",x);
x=ceil(a); y=ceil(b); z=ceil(c); printf("%d ",y);
printf("%d",z);

getch();
Display x,y,z }

}
Output :
END
Enter three value a, b, c:--

76.34 24.56 12.90

Values Are:-
76 25 13

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

4.4 Write a program that reads 4 floating point values in the range of 0.0 to Program :
20.0,and prints a horizontal bar chart to represent these values using the
character * as the fill char.forr the purpose of chart,the values may be rounded //Write a program that reads 4 floating point values in the range of 0.0 to
off to the nearest integer.for ex. // 20.0,and prints a horizontal bar chart to represent these values using the
// character * as the fill char.forr the purpose of chart,the values may be rounded
* * * * // off to the nearest integer.for ex.
* * * * 4.36
* * * * //* * * *
//* * * * 4.36
Algorithm:-- //* * * *

Step 1: Store a=1.95, b=2.24, c=3.40, d=4.65 //Date: 13/03/2010


Step 2: Compute w=ceil(a), x=floor(b), y=floor(c), z=ceil(d).
Step 3: Display * in w columns & 3 rows & w. #include<stdio.h>
Step 4: Display * in x columns & 3 rows & x. #include<conio.h>
Step 5: Display * in y columns & 3 rows & y. #include<math.h>
Step 6: Display * in z columns & 3 rows & z.
void main()
Flowchart:-- {

int w,x,y,z;
START float a,b,c,d;

clrscr();
Store a=1.95, b=2.24,
a=1.95;
c=3.40, d=4.65
b=2.24;
c=3.40;
w=ceil(a), d=4.65;
x=floor(b),
y=floor(c), w=ceil(a);
z=ceil(d) x=floor(b);
y=floor(c);
z=ceil(d);
Display * in w columns & 3 rows & w
printf("\n\n*\n*(%f)\n*",a);
printf("\n\n* *\n* *(%f)\n* *",b);
printf("\n\n* * *\n* * *(%f)\n* * *",c);
printf("\n\n* * * *\n* * * *(%f)\n* * * *",d);
Display * in w columns & 3 rows & w
getch();
}
Display * in w columns & 3 rows & w Output:--

*
Display * in w columns & 3 rows & w
* 1.950000
*

**
END * * 2.240000
**

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

*** Flowchart:--
* * * 3.400000
***

****
START
* * * * 4.650000
****
Read a,b

x=b%10
4.5 write a program to demo the process of multiplication. The program should ask y=b/10
the user to enter two digit integers and print the product to integers as shown t=x*a
below q=y*a
w=t+(q*10)

Display a, Newline, ‘*’ and b.


45 Display ---------------------- & go to newline
* 37 Display x, ‘*’, a, “is” and t & go to new line.
-------------------- Display y, ‘*’, a, “is” and q & go to new line.
7x45 is 315 Display ---------------------- & go to newline.
3x45 is 135 Display “Add Them” and w.
------------------
Add them 1665
END

Program:--

Algorithm:-- //write a program to demo the process of multiplication. The program should ask
// the user to enter two digit integers and print the product to integers as shown
Step 1: Read a,b; // below
Step 2: Compute x=b%10, y=b/10.
Step 3: Compute t=x*a, q=y*a and w=t+(q*10). // 45
Step 4: Display a, Newline, ‘*’ and b. // * 37
Step 5: Display ---------------------- & go to newline. // --------------------
Step 6: Display x, ‘*’, a, “is” and t & go to new line. // 7x45 is 315
Step 7: Display y, ‘*’, a, “is” and q & go to new line. // 3x45 is 135
Step 8: Display ---------------------- & go to newline. // --------------------
Step 9: Display “Add Them” and w. //Add them 1665

//Date: 13/03/2010

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

4.6 Write a program to read three integers form the keyboard using one scanf statement and
int a,b,x,y,t,q,w; output them on one line using:

clrscr(); a) Three printf statements.


b) Only one printf with conversion specifiers, and
printf("Enter value of a"); c) Only one printf without conversion specifiers.
scanf("%d",&a);
Algorithm:--
printf("Enter value of b");
scanf("%d",&b);
Step 1: Read x, y and z.
x=b%10; Step 2: Display x.
y=b/10; Step 3: Display y.
t=x*a; Step 4: Display z.
q=y*a; Step 5: Display x, y & z in one line with conversion specifiers.
w=t+(q*10); Step 6: Display x, y & z in one line without conversion specifiers.

printf("\n %d",a); Flowchart:--


printf("\n * %d\n",b);
printf(" -----\n"); START

printf("%d x %d is",x,a);
printf(" %d\n",t);
Display x.
printf("%d x %d is",y,a);
Display y.
Display z
printf(" %d\n",q);
printf(" ----------\n");

printf("ADD THEM %d\n",w);


Display x, y & z in one line with
printf(" ----------");
conversion specifiers.
getch();
}

Output :
Display x, y & z in one line without
Enter value of a 56 conversion specifiers.
Enter value of b 65

56
* 65
--------------- END
5 x 56 is 280
6 x 56 is 336
---------------- Program:--
ADD THEM 3640
----------------
//Write a program to read three integers form the keyboard using one scanf statement and
// output them on one line using:

//a) Three printf statements.


//b) Only one printf with conversion specifiers, and
//c) Only one printf without conversion specifiers.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--

//Date : 13/03/2010
START
#include<conio.h>
#include<stdio.h>
Num=10.45678
void main()
{
int x,y,z;

clrscr(); Display Num in


exponential format (correct
printf("Enter Three Values\n"); to two decimal places)
scanf("%d%d%d",&x,&y,&z);

printf(" x= %d",x);
printf(" y= %d",y); Display Num in exponential format
printf(" z= %d",z); (correct to four decimal places)
printf(" x= %d y= %d z= %d\n",x,y,z);

printf(" x= %d y= %d z= %d\n",x,y,z);

getch(); Display Num in exponential format


} (correct to eight decimal places)

Output:--

Enter Three Values


234 END
x=2 y=3 z=4
x=2 y=3 z=4
x=2 y=3 z=4
Program:--

/Write a program that prints the value 10.45678 in exponential format with the following
4.7 Write a program that prints the value 10.45678 in exponential format with the following // specification:
specification: // a) correct to two decimal places;
a) correct to two decimal places; // b) correct to four decimal places; and
b) correct to four decimal places; and // c) correct to eight decimal places.
c) correct to eight decimal places.
//Date : 13/03/2010

Algorithm:-- #include<conio.h>
#include<stdio.h>
Step 1: Store 10.45678 in Num.
Step 2: Display Num in exponential format (correct to two decimal places). void main()
Step 3: Display Num in exponential format (correct to four decimal places). {
Step 4: Display Num in exponential format (correct to eight decimal places). float Num=10.45678;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--

clrscr();
START
printf("exponential format with correct to two decimal places:-- %.2e\n",Num);
printf("exponential format with correct to four decimal places:-- %.4e\n",Num);
printf("exponential format with correct to eight decimal places:-- %.8e\n",Num); Num=345.6789
getch();
}

Display Num in fixed-point


format (correct to two
decimal places)
Output:--

exponential format with correct to two decimal places:-- 1.05e+01


exponential format with correct to four decimal places:-- 1.0457e+01 Display Num in fixed-point format
exponential format with correct to eight decimal places:-- 1.0456780e+01 (correct to five decimal places)

4.8 Write a program to print the value 345.6789 in fixed-point format with the following
specification: Display Num in fixed-point format
(correct to zero decimal places)

a) Correct to two decimal places;


b) Correct to five decimal places; and
c) Correct to zero decimal places. END

Program:--

//Write a program to print the value 345.6789 in fixed-point format with the following
// specification:
Algorithm:--
//a) Correct to two decimal places;
Step 1: Store 345.6789 in Num. //b) Correct to five decimal places; and
Step 2: Display Num in fixed-point format (correct to two decimal places). //c) Correct to zero decimal places.
Step 3: Display Num in fixed-point format (correct to five decimal places).
Step 4: Display Num in fixed-point format (correct to zero decimal places). //Date : 13/03/2010

#include<conio.h>
#include<stdio.h>

void main()
{
float Num=345.6789;

clrscr();

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--

printf("fixed-point format (correct to two decimal places):-- %.2f\n",Num);


START
printf("fixed-point format (correct to five decimal places):-- %.5f\n",Num);
printf("fixed-point format (correct to zero decimal places):-- %f\n",Num);

getch(); Str1=”ANIL”
} Str2=”KUMAR”
Str3=”GUPTA”

Output:-- Display Str1,first character of Str2 & Dot


and Str3.
fixed-point format (correct to two decimal places):-- 345.68
fixed-point format (correct to five decimal places):-- 345.67889
fixed-point format (correct to zero decimal places):--345.678894

Display First character of


Str1,Dot(.),first character of Str2 and
Str3
4.9 Write a program to read the name ANIL KUMAR GUPTA in three parts using the scanf
statement and to display the same in the following format using the printf statement.

a) ANIL K. GUPTA Display Str3,First character of


b) A.K. GUPTA Str1,Dot(.),first character of Str2 and
c) GUPTA A.K. Dot(.)

END
Algorithm:--

Program:--

//Write a program to read the name ANIL KUMAR GUPTA in three parts using the scanf
Step 1: Store ANIL in Str1. // statement and to display the same in the following format using the printf statement.
Step 2: Store KUMAR in Str2.
Step 3: Store GUPTA in Str3.
Step 4: Display Str1,first character of Str2 & Dot(.) and Str3. //a) ANIL K. GUPTA
Step 5: Display First character of Str1,Dot(.),first character of Str2 and Str3. //b) A.K. GUPTA
Step 6: Display Str3,First character of Str1,Dot(.),first character of Str2 and Dot(.). //c) GUPTA A.K.

//Date : 13/03/2010

#include<conio.h>
#include<stdio.h>

void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
START
{
char Str1[10]="ANIL";
char Str2[10]="KUMAR"; Read Name1, Code1 and Price1
char Str3[10]="GUPTA"; Read Name2, Code2 and Price2

clrscr();

printf("%s %.1s. %s\n",Str1,Str2,Str3); Display Name1 and Code1 left-justified and Price1 right-justified.
printf("%.1s. %.1s. %s\n",Str1,Str2,Str3); Go to new line
printf("%s %.1s.%.1s.\n",Str3,Str1,Str2); Display Name2 and Code2 left-justified and Price2 right-justified.

getch();
}
END

Program:--
Output:--
//Write a program to read and display the following table of data.
ANIL K. GUPTA
A.K. GUPTA //Name Code Price
GUPTA A.K. //Fan 67831 1234.50
//Motor 450 5786.70

//The name and code must be left-justified and price must be right-justified.

4.10 Write a program to read and display the following table of data.

Name Code Price


Fan 67831 1234.50 //Date : 13/03/2010
Motor 450 5786.70
#include<conio.h>
The name and code must be left-justified and price must be right-justified. #include<stdio.h>

void main()
{
char Name1[10],Name2[10];
int Code1,Code2;
Algorithm:-- float Price1,Price2;

Step 1: Read Name1, Code1 and Price1.


Step 2: Read Name2, Code2 and Price2. clrscr();
Step 3: Display Name1 and Code1 left-justified and Price1 right-justified.
Step 4: Go to new line printf("Enter Name, Code and Price\n");
Step 5: Display Name2 and Code2 left-justified and Price2 right-justified. scanf("%s%d%f",&Name1,&Code1,&Price1);

printf("Enter Name, Code and Price\n");


scanf("%s%d%f",&Name2,&Code2,&Price2);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("Name Code Price\n"); CHAPTER 5 13/03/2010


printf("%-7s %-8d %8.2f\n",Name1,Code1,Price1); 5.1 Write a program to determine whether a given number is “odd” or “even” and print
printf("%-7s %-8d %8.2f\n",Name2,Code2,Price2); the message

getch(); NUMBER IS EVEN


} OR
NUMBER IS ODD
Output:-- (a) Without using the else option.
(b) With else option.
Enter Name, Code and Price
Fan Algorithm:--
67831
1234.50 Without using the else option
Step 1: Read x.
Enter Name, Code and Price Step 2: Check x%2==0.
Motor Step 3: If true then go to step 4 and otherwise go to step 5.
450 Step 4: Display “The number is even” and exit.
5786.70 Step 5: Display “The number is odd”.

Name Code Price With else option


Fan 67831 1234.50 Step 1: Read x.
Motor 450 5786.70 Step 2: Check x%2==0.
Step 3: If true then go to step 4 and otherwise go to step 5.
Step 4: Display “The number is even”.
Step 5: Display “The number is odd”.

Flowchart:--

Start

Read x

NO YES
x%2
==0

Display “The Display “The


Number is Odd” Number is
Even”

End

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- //(a) Without using the else option.


//(b) With else option.
Without using the else option
// Date: March 13,2010
//Write a program to determine whether a given number is “odd” or “even” and print the
message
#include<stdio.h>
//NUMBER IS EVEN #include<conio.h>
//Or
//NUMBER IS ODD void main()
//(a) Without using the else option. {
//(b) With else option. int x;

clrscr();
// Date : 13/03/2010
printf("Enter an integer number: ");
#include<stdio.h> scanf("%d",&x);
#include<conio.h>
#include<stdlib.h> if(x%2==0)
printf("The number entered is even");
void main() else
{ printf("The number entered is odd");
int x;
getch();
clrscr();
}
printf("Enter an integer number: ");
scanf("%d",&x); Output:--

if(x%2==0) Enter an integer number: 5


{ The number entered is odd
printf("The number entered is even");
getch();
exit(0);
} 5.2 Write a program to find the number of and sum of all integers greater than 100
and less than 200 that are divisible by 7.
printf("The number entered is odd");
getch();
}

Output:-- Algorithm:--

Enter an integer number: 5 Step 1: Store 100 to Num & 0 to Sum.


The number entered is odd Step 2: if Num%7=0 then go to Step 3
Step 3: Compute Count=Count+1 & Sum=Sum+Num & Num=Num+1.
With else option Step 4: if Num<=200 then go to Step 2 otherwise go to Step 5.
//Write a program to determine whether a given number is “odd” or “even” and print the Step 5: Display Count & Sum.
message

//NUMBER IS EVEN
//Or
//NUMBER IS ODD

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- Sum=Count=0;
START
Loop:

if (Num%i==0)
NO {
if Sum=Sum+Num;
Num%7=0 Count=Count+1;
}

YES Num=Num+1;

Count=Count+1 & if(Num<=100)


Sum=Sum+Num & goto Loop;

printf("Count:-- %d\n",Count);
printf("Sum:-- %d",Sum);
Num=Num+1 }

Output:--

YES
if
Num<=200

5.3 A set of two linear equation two unknows x1 and x2 is given below:
NO ax1 + bx2 = m
cx1 + dx2 = n
Display
Count & The set has a unique solution
Sum
x1=(md-bn)/(ad-cb)
x2=(na-mc)/(ad-cb)

START
Program:--
Algorithm:--
//Write a program to find the number of and sum of all
//integers greater than 100 and less than 200 that are divisible by 7. Step 1: Read a,b,c,d,m and n.
// Date : 13/03/2010 Step 2: Compute a*d-c*b and store the result Dr.
Step 3: Check if Dr! =0.
#include<stdio.h> Step 4: If true then go to Step 5 and otherwise go to step 9.
#include<conio.h> Step 5: Compute (m*d-b*n)/(a*d-c*b) and store the result x1.
#include<stdlib.h> Step 6: Compute (n*a-m*c)/(a*d-c*b) and store the result x2.
Step 7: Display x1.
void main() Step 8: Display x2 and go to step 10.
{ Step 9: Display “The division is not possible”.
int Num,Sum,Count; Step 10: Stop.

clrscr();

Num=100;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
float x1,x2;

Start clrscr();

printf("Enter the value of a, b, c, d, m, n: ");


scanf("%d%d%d%d%d%d",&a,&b,&c,&d,&m,&n);
Read a, b,
c, d, m
Dr=(a*d-c*b);
and n
if(Dr!=0)
{
x1=(m*d-b*n)/dr;
No x2=(n*a-m*c)/dr;
Dr! =0 printf("\n The value of x1= %f \n The value of x2= %f",x1,x2);
}
else
Yes printf("The division is not possible and result is an abrupt value ");

getch();
x1= (m*d-b*n)/(a*d-c*b)
}
and
x2=(n*a-m*c)/(a*d-c*b) The division
is not 5.4 Given the list of marks ranging from 0 to 100,write a program to compute and
possible. print the number of students:
a) who have obtained more than 80 marks.
b) who have obtained more than 60 marks
Display x1 c) who have obtained more than 40 marks
Display x2 d) who have obtained 40 or less marks
e) in the range 81 to 100
f) in the range 61 to 80
g) in the range 41 to 60
h) in the range 0 to 40
End
The program should use minimum number of if statements.
Program:--
5.5 Admission to a professional course in subject to the following conditions:
//A set of two linear equation two unknows x1 and x2 is given below: a) Marks in mathematics >=60
b) Marks in Physics >=50
// ax1 + bx2 = m c) Marks in Chemistry >=40
// cx1 + dx2 = n d) Total in all three subjects >=200
// The set has a unique solution or
Total in mathematics and physics>=150.
// x1=(md-bn)/(ad-cb) Given the marks in the three subjects, write a program to process the applications
// x2=(na-mc)/(ad-cb) to the eligible candidates.

#include<stdio.h> Algorithm:--
#include<conio.h>
Step 1: Read Maths, Phy and Chem.
void main() Step 2: Compute Maths+Phy+Chem and store the result in Total
{ Step 3: Compute Maths+Phy and store the result Total_MP
Step 4: Check Maths>=60 && Phy>=50 && Chem>=40 && Total>=200
int a,b,c,d,m,n,Dr; Step 5: If Step 4 true then go to step 6 otherwise go to step 7.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Step 6: Display “The candidate is eligible for the course” and go to step 11.
Step 7: Check Total_MP>=150 Program:--
Step 8: If Step 7 true then go to step 9 otherwise go to step 10.
Step 9: Display “The candidate is eligible for the course” and go to step11 //Admission to a professional course in subject to the following conditions:
Step 10: Display “The candidate is not eligible for the course” and go to step 11. //a) Marks in mathematics >=60
Step 11: Stop. //b) Marks in Physics >=50
//c) Marks in Chemistry >=40
//d) Total in all three subjects >=200
//or
//Total in mathematics and physics>=150.
//Given the marks in the three subjects, write a program to process the applications to the
eligible candidates.
Flowchart:--
//Date: 13/03/2010
Start
#include<stdio.h>
#include<conio.h>

Read Maths, void main()


Phy and Chem {
int Maths,Phy,Chem,Total,Total_MP;

clrscr();

Total= printf("Enter the marks of maths :");


Maths+Phy+Chem scanf("%d",&Maths);
Total_MP = Maths+Phy
printf("Enter the marks of phy :");
scanf("%d",&Phy);

printf("Enter the marks of chem :");


scanf("%d",&Chem);

Yes Total=Maths+Phy+Chem;
Maths>=60 && Phy>=50
Total_MP=Phy+Maths;
&& Chem>=40 &&
Total>=200
if (Maths>=60 && Phy>=50 && Chem>=40 && Total>=200)
printf("The candidate is eligible for the admission");

No else
{
The candidate is The candidate is if(Total_MP>=150)
not eligible for No Yes eligible for the
Total_MP printf("The candidate is eligible for the admission");
the course course
>=150 else
` printf("The candidate is not eligible for the admission");
}

getch();

}
End

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

5.6 Write a program to print a two-dimensional Sqaure Root Table as shown below,
to provide the square root of any number from 0 to 9.9
Flowchart:-- START
Square Root table

Number 0.0 0.1 0.2……………………0.9 Display “NUMBER”


0.0
1.0
3.0 x y j=0.1

9.0
Display j
Algorithm:--

j<0.5

i=1

j= 0.1

sum=i+j
sq=sqrt(sum);

Display sq

j=j+0.1

j<0.5

i=i+1

i<5

END

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- if(j<0.5)
goto loop;
//Write a program to print a two-dimensional Sqaure Root Table as shown below,
// to provide the square root of any number from 0 to 9.9 i=i+1;
if(i<=4)
// Square Root table {
printf("\n");
//Number 0.0 0.1 0.2……………………0.9 goto loop1;
//0.0 }
//1.0 getch();
//3.0 x y }
//… Output:--
//9.0
5.9 Write a program that will read the value of x and evaluate the following function

Y= 1 for x>0
#include<stdio.h> 0 for x=0
#include<conio.h> -1 for x<0
#include<math.h>
void main() Using
(a) Nested if statements
{ (b) Else if statements
float sq,sum,i,j; (c) Conditional operators
clrscr();

printf("Number ");

j=0.1;
Algorithm
loop3:
printf(" %f",j);
j=j+0.1; Step 1: Read x.
if(j<0.5) Step 2: Check x>0, if true then go to step 3 otherwise go to step 5.
goto loop3; Step 3: Assign 1 to y, and go to step 4
Step 4: Display y and go to step 10.
printf("\n"); Step 5: Check if x==0, if true then go to step 6 otherwise go to step 8.
i=1; Step 6: Assign 0 to y and go to step 7.
Step 7: Display y and go to step 10.
loop1: Step 8: Assign -1 to y, go to step 9.
Step 9: Display y and go to step 10.
printf("%f",i); Step 10: End

j=0.1; Flowchart:--
loop: Start

sum=i+j;

sq=sqrt(sum); Enter X

printf(" %f",sq);

j=j+0.1; Yes
X>0
Y=1
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

clrscr();
No printf("Enter the value of X: ");
Yes scanf("%f",&x);
x==0 Display Y
Y==0 if(x>0)
{

No y=1;
printf("The value of y for the given value of x=%f is %d\n",x,y);
}
Display Y Y=(-1) else if(x==0)
{
y=0;
printf("The value of y for the given value of x=%f is
%d\n",x,y);
Display Y }
else
{
y=-1;
printf("The value of y for the given value of x=%f is %d\n",x,y);
}
End
getch();
}
Program:-- Output:--

Else if statements Enter the value of X: 3

//Write a program that will read the value of x and evaluate the following function The value of y for the given value of x=3 is =1

//Y= 1 for x>0 Nested if statements


// 0 for x=0
// -1 for x<0 //Write a program that will read the value of x and evaluate the following function

//Using //Y= 1 for x>0


//(a) Nested if statements // 0 for x=0
//(b) Else if statements // -1 for x<0
//(c) Conditional operators
//Using
//Date: 13/03/2010 //(a) Nested if statements
//(b) Else if statements
#include<stdio.h> //(c) Conditional operators
#include<conio.h>
//Date: 13/03/2010
void main()
{ #include<stdio.h>
#include<conio.h>
int y;
float x; void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

int y; // Date 13/03/2010


float x;

clrscr();
#include<stdio.h>
printf("Enter the value of X: "); #include<conio.h>
scanf("%f",&x);
void main()
if(x>0) {
{
int y;
y=1; float x;
printf("The value of y for the given value of x=%f is %d\n",x,y);
} clrscr();

else printf("Enter the value of X: ");


{ scanf("%f",&x);
if(x==0)
{ (x>0?(y=1):(x==0)?(y=0):(y=-1));
y=0;
printf("The value of y for the given value of x=%f is %d\n",x,y); printf("The value of y for the given value of x=%f is %d\n",x,y);
}
else getch();
{ }
y=-1;
printf("The value of y for the given value of x=%f is %d\n",x,y);
} Output:--
}
Enter the value of X: 3
getch();
} The value of y for the given value of x=3 is =1

Output:--

Enter the value of X: 3


5.10 Write a program to compute the real roots of a quadratic equation
The value of y for the given value of x=3 is =1 ax2 + bx+c=0

The roots are given by the equations:


Conditional operators
X1=(-b+sqrt(b*b-4*a*c))/(2*a)
//Write a program that will read the value of x and evaluate the following function
X2=(-b-sqrt(b*b-4*a*c))/(2*a)
//Y= 1 for x>0
// 0 for x=0 The program should request for the values of the constants a,b and c and print the
// -1 for x<0 values of x1 and x2. Use the following rules:
(a) No solution , if both a nd b are zero
//Using (b) There is only one root,if a=0 (x=-c/b)
//(a) Nested if statements (c) There is no real root if b*b-4ac is negative
//(b) Else if statements (d) Otherwise there are real roots.
//(c) Conditional operators

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Test your program with appropriate data. Yes


D<0

No
Algorithm:--
x1= (-b+sqrt(b*b-4*a*c))/(2*a)
Step 1: Read a, b and c x2= (-b-sqrt(b*b-4*a*c))/(2*a)
Step 2: Compute b*b-4*a*c and store the result in d.
Step 3: Check if a==0 && b==0, if true then go to step 4 otherwise go to step 5.
Step 4: Display “There is no solution of the quadratic equation" and go to step 13.
Step 5: Check if a==0, if true go to step 6 otherwise go to step 8.
Step 6: Compute x=-c/b and go to step 7. Display x1
Step 7: Display “There is only one root” and display x and go to step 13. Display x2
Step 8: Check if d<0, if true go to step 9 otherwise go to step 10.
Step 9: Display “Roots are imaginary” and go to step 13.
Step 10: Compute x1= (-b+sqrt(b*b-4*a*c))/(2*a) and go to step11
Step 11: Compute x2= (-b-sqrt(b*b-4*a*c))/(2*a)
Step 12: Display x1 and x2 END
Step13. Stop

Program:--

// ax2 + bx+c=0

// The roots are given by the equations:

Flowchart:-- //X1=(-b+sqrt(b*b-4*a*c))/(2*a)

Start //X2=(-b-sqrt(b*b-4*a*c))/(2*a)

//The program should request for the values of the constants a,b and c and print the values of x1
Read and x2. Use the following rules:
a,b,c //(a) No solution , if both a nd b are zero
//(b) There is only one root,if a=0 (x=-c/b)
//(c) There is no real root if b*b-4ac is negative
//(d) Otherwise there are real roots.
D=b*b-4*a*c
//Test your program with appropriate data.

Yes There is no // Date 13 March,2010


if a==0 solution of
&& b==0 the equation

#include<stdio.h>
No #include<conio.h>
#include<math.h>
Yes Calculate x=-
If c/b
a==0 void main()
{
float a,b,c,d;
No There is only float x1,x2,x;
The roots one root
are
imaginary
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
clrscr(); START

printf(" Enter the value of a: ");


scanf("%f",&a); Read Len,
printf("\n Enter the value of b: "); Hei, Hyp
scanf("%f",&b);
printf("\n Enter the value of c: ");
scanf("%f",&c);
Temp1=Hyp*Hyp;
d=(b*b)-(4*a*c); Temp2=Len*Len+Hei*Hei;

if(a==0 && b==0)


printf(" There is no solution of the quadratic equation");

else if(a==0) YES Temp1= NO


{ Temp2
x=-c/b;
printf(" There is only one root of the equation, x= %f",x);
}
else if(d<0)
{
printf("The roots are imaginary and as follows: \n"); Triangle is Right Angle Triangle is Not a Right
} Triangle Angle Triangle
else
{
x1= (-b+sqrt(d))/(2*a);
x2= (-b-sqrt(d))/(2*a);
printf("The roots are real"); END
printf("x1=%f \n x2=%f",x1,x2);
}

getch();
}
Program:--
Output:--
Enter the value of a: 1 //Write a program to read three integer values from the keyboard and display the
Enter the value of b: -3 //output stating that they are the sides of right-angled triangle.
Enter the value of c: 2
The roots are real //Date : 13/03/2010
x1=2 x2=1
#include<conio.h>
5.11 Write a program to read three integer values from the keyboard and display the #include<stdio.h>
output stating that they are the sides of right-angled triangle.
void main()
Algorithm:-- {

Step 1: Read Len, Hei, Hyp. float Len,Hei,Hyp;


Step 2: Compute Temp1=Hyp*Hyp , Temp2=Len*Len+Hei*Hei. float Temp1,Temp2;
Step 3: Check Temp1==Temp2 is true then go to Step 4 otherwise go to Step 5
Step 4: Display “Triangle is Right Angle Triangle”. clrscr();
Step 5: Display “Triangle is Not a Right Angle Triangle”.
printf("Enter Length Height and Hypotenes of Triangle--\n");

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

scanf("%f %f %f",&Len,&Hei,&Hyp);

Temp1=Hyp*Hyp;
Temp2=Len*Len+Hei*Hei;
Units>=0 NO Units>200 NO Units>300 NO
if(Temp1==Temp2) &&Units< &&Units< &&Units<
printf("Triangle is Right Angle Triangle\n"); =200 =300 =400
else
printf("Triangle is Not a Right Angle Triangle\n");

getch();
} YES YES YES

Output:--
Charge=100+(Units* Charge=100+(Units* Units>300&&Units< Charge=
Enter Length Height and Hypotenes of Triangle— 0.80) 0.90) =400 (100+units)+(100+U
234 nits)*15
Triangle is Not a Right Angle Triangle

5.12 An electricity board charges the following rates for the use of electricity: Display Name
Units Charge
For the first 200 units; 80 P per unit
For the next 100 units; 90 P per unit
Beyond 300 units; Rs. 1 per unit
END
All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more
than Rs. 400, then an additional surcharge of 15% of total amount is charged.
Write a program to read the names of users and number of units consumed and printout
the charges with names. Program:--

Algorithm:-- //An electricity board charges the following rates for the use of electricity:
// For the first 200 units; 80 P per unit
Step 1: Read Name & Units. // For the next 100 units; 90 P per unit
Step 2: Check Units>=0&&Units<=200 if true the go to Step 3 otherwise go to Step 4 // Beyond 300 units; Rs. 1 per unit
Step 3: Compute Charge=100+(Units*0.80) & go to Step 9 //All users are charged a minimum of Rs. 100 as meter charge. If the total amount is more than
Step 4: Check Units>200&&Units<=300 if true the go to Step 5 otherwise go to Step 6 Rs. 400,
Step 5: Compute Charge=100+(Units*0.90) & go to Step 9 //then an additional surcharge of 15% of total amount is charged.
Step 6: Check Units>300&&Units<=400if true the go to Step 7 otherwise go to Step 8 //Write a program to read the names of users and number of units consumed and printout the
Step 7: Compute Units>300&&Units<=400 & go to Step 9 charges with names.
Step 8: Compute Charge= (100+units)+(100+Units)*15 & go to Step 9
Step 9: Display Name Units Charge
//Date : 13/03/2010
Flowchart:--
#include<conio.h>
START #include<stdio.h>

void main()
{
Enter Name of User in
Name int Units;
Enter Total Units char Name[10];
Consumed in Units float Charge;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

clrscr();

printf("Enter Name of User:--\n");


scanf("%s",&Name);

printf("Enetr Total Units Consumed\n"); YES


scanf("%d",&Units);
Display i
if(Units>=0&&Units<=200)
Charge=100+(Units*0.80);
else if(Units>200&&Units<=300)
Charge=100+(Units*0.90);
Count=Count+1
else if(Units>300&&Units<=400) YES Sum=Sum+1
Charge=100+Units;
else
Charge=(100+units)+(100+Units)*15;
i=i+1

printf("Name Units Charge\n");


printf("%s %d %.2f",Name,Units,Charge);
i<=100
getch();
}
NO
Output:--
Enter Name of User:-- Ritesh Display Sum
Enetr Total Units Consumed 600 Display Count
Name Units Charge
Ritesh 600 805.00
END
5.13 Write a program to compute and display the sum of all integers that are divisible by 6
but not divisible by 4 and lie between 0 and 100. The program should also count and
display the number of such values. Program:--

Algorithm:-- //Write a program to compute and display the sum of all integers that are divisible by 6
// but not divisible by 4 and lie between 0 and 100. The program should also count and
Step 1: Store 0 to Sum, Count and i. // display the number of such values.
Step 2: if i%6==0 & i%4!=0 is true then Continue from Step3 otherwise go to Step 5.
Step 3: Display i
Step 4: Compute Count=Count+1 & Sum=Sum+1. //Date : 13/03/2010
Step 5: Compute i=i+1
Step 6: if i<=100 then go to Step 2. #include<conio.h>
Step 7: Display Sum & Count. #include<stdio.h>

Flowchart:-- void main()


START {

int Sum,i,Count;
Sum=Count=i=0
clrscr();

Sum=Count=0;
i=0;
i%6=0 NO
&i%4!=0
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Loop:

if((i%6==0)&&(i%4!=0))
{
If NO
printf("%d \n",i);
Count=Count+1; Temp=0
Sum=Sum+i;
}
YES
i=i+1; YES

if(i<=100) Count=Count+1 i=i+1


goto Loop;

printf("Sum of Numbers is %d\n",Sum);


printf("Count of Numbers is %d\n",Count);
If
getch(); i<=Num
}

Output:-- NO
6 18 30 42 54 66 78 90
Sum of Numbers is 384 If YES
Prime
Count of Numbers is 8 Count=1

5.14 Write an interactive program that could read a positive integer number and decide END
whether the number is prime number and display the output accordingly. NO
Not Prime
Modify the program to count all the prime numbers that lie between 100 and 200.

Algorithm:-- Program:--

Step 1: Read Num. //Write an interactive program that could read a positive integer number and decide
Step 2: Store 2 to i & 0 to Count. //whether the number is prime number and display the output accordingly.
Step 3: Compute Num%i & store the result in Temp.
Step 4: if Temp==0 then Count=Count+1
Step 5: if i<=Num then goto step 3 //Date : 13/03/2010
Step 6: if Count==1 then Display Number is Prime
Step 7: Otherwise Display Number is not Prime. #include<conio.h>
#include<stdio.h>

void main()
Flowchart:-- {
START
int Num,i,Count,Temp;

i=2 clrscr();
Count=0
Count=0;
i=2;

Temp=Num%i printf("Enter A Number :--\n");

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

scanf("%d",&Num);
START
Loop:

Temp=Num%i;
Read x,
if(Temp==0)
T
Cpunt=Count+1;

i=i+1;

if(i<=Num) NO NO
goto Loop; If T =s or If T=c or If T =t or
S C T Other
if(Count==1) T
printf("Number %d is Prime",Num);
else YES YES
printf("Number %d is Not Prime",Num); YES

Val=sin(x) Val =cos(x) Val =tan(x)


getch();
}

Output:--
Display Wrong Input
Enter A Number :-- Val
6
Number 6 is Prime
END

5.15 Write a program to read a double-type value x that represent angle in radians and a
character-type variable T that represents the type of trigonometric function and display
the value of

a) Sin(x), if s or S is assigned to T,
b) Cos(x), if c or C is assigned to T, and
c) Tan(x), if t or T is assigned to T.

Using (i) if……….else statement and (ii) switch statement.

Algorithm:-- Using switch statement

Step 1: Read x, T. START


Step 2: Choice T is s or S then Val=sin(x)
Step 3: Choice T is c or C then Val=cos(x)
Step 4: Choice T is t or T then Val=tan(x)
Step 5: Display Val.

Flowchart:-- Read x,
Choice
Using if………..else

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("\nc or C for Cos(x)");


printf("\nt or T for Tan(x)");
printf("\nEnter Choice\n");
T=s or S
T Val=sin(x) T=getch();

if((T=='s')||(T=='S'))
Val=sin(x);
T=c or C Display else if((T=='c')||(T=='C'))
Val =cos(x)
Val Val=cos(x);
else if((T=='t')||(T=='T'))
Val=tan(x);
else
T=t or T printf("\nWrong Input\n");
Val =tan(x)
END
printf("Value:--- %lf",Val);

Other T getch();
Wrong Input }

Output:--
Program:--
Enter Angle:--
Using if………..else 90
s or S for Sin(x)
//Write a program to read a double-type value x that represent angle in radians and a c or C for Cos(x)
// character-type variable T that represents the type of trigonometric function and display t or T for Tan(x)
// the value of
Enter Choice
//a) Sin(x), if s or S is assigned to T, s
//b) Cos(x), if c or C is assigned to T, and
//c) Tan(x), if t or T is assigned to T. Value:--- 1.000000

//Using (i) if……….else statement


Using Switch Statement

//Date : 13/03/2010 //Write a program to read a double-type value x that represent angle in radians and a
// character-type variable T that represents the type of trigonometric function and display
#include<conio.h> // the value of
#include<stdio.h>
//a) Sin(x), if s or S is assigned to T,
void main() //b) Cos(x), if c or C is assigned to T, and
{ //c) Tan(x), if t or T is assigned to T.
double x,Val;
char T; //Using (ii) switch statement
Val=0;

clrscr(); //Date : 13/03/2010

printf("Enter Angle:--\n"); #include<conio.h>


scanf("%lf",&x); #include<stdio.h>

printf("\ns or S for Sin(x)"); void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

{
double x,Val; CHAPTER 6 15/03/2010
char T;
clrscr(); 6.1 Given a number, write a program using while loop to reverse the digits of the number.
Val=0;
printf("Enter Angle:--\n"); Algorithm:--
scanf("%lf",&x);
Step 1: Read Num.
printf("\ns or S for Sin(x) \ns or S for Cos(x) \ns or S for Tan(x)\nEnter Choice "); Step 2: Store Temp=Num & RevNum=0.
T=getch(); Step 3: Repeat Step 4 to Step 5 while Temp! =0 do otherwise go to Step 6
Step 4: Compute Dig=Temp%10 & Temp=Temp/10.
switch(T) Step 5: Compute RevNum=(RevNum*10)+Temp.
{ Step 6: Display RevNum.

case 's': Flowchart:--


case 'S': Val=sin(x); break; START
case 'c':
case 'C': Val=cos(x); break;
case 't': Read Num
case 'T': Val=tan(x); break;

default:printf("\nWrong Input\n");
Temp=Num &
} RevNum=0

printf("Value:--- %lf\n",Val);

getch(); NO
} Temp
! =0

Enter Angle:-- YES


90
s or S for Sin(x) Dig=Temp%10, Temp=Temp/10
c or C for Cos(x) RevNum= (RevNum*10) + Dig
t or T for Tan(x)

Enter Choice
s Display
RevNum
Value:--- 1.000000

END
Program:--

//Given a number, write a program using while loop to reverse the digits of the number.
// Date : 15/03/2010

#include<stdio.h>
#include<conio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

void main()
{
long int Num,Temp,RevNum,Dig; Flowchart:-- START

clrscr();
Read m
printf("Enter any Number:--\n");
scanf("%ld",&Num);
Fact=1
N=m
Temp=Num;
RevNum=0;
NO
while(Temp!=0) n>0
{
Dig=Temp%10; YES
Temp=Temp/10; Fact=Fact*n
RevNum=(RevNum*10)+Dig; n=n-1
}

printf("Rverse of Number %ld is %ld\n",Num,RevNum); Display Fact


getch();
END
}

Output:-- Program:--

Enter any Number:-- //The factorial of an integer m is the product of consecutive integers from 1 to m. That is,
12345 // Factorial m = m! = m*(m-1)*…………1
Rverse of Number 12345 is 54321
// Date : 15/03/2010

#include<stdio.h>
5.2 The factorial of an integer m is the product of consecutive integers from 1 to m. That is, #include<conio.h>
Factorial m = m! = m*(m-1)*…………1

void main()
{
long int m,n,Fact;

Algorithm:-- clrscr();

Step 1: Read m. printf("Enter any Number:--\n");


Step 2: Store 1 to Fact & n=m. scanf("%ld",&m);
Step 3: Repeat Step 4 while n > 0 otherwise go to Step 5
Step 4: Compute Fact=Fact*n & n=n-1.
Step 5: Display Fact. n=m;
Fact=0;

while(n>0)
{
Fact=Fact*n;
n=n-1;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

}
Program:--
printf("Factorial of Number %ld is %ld\n",m,Fact); //Write a program to compute the sum of the digits of a given number.
getch(); // Date : 15/03/2010

} #include<stdio.h>
#include<conio.h>
Output:--

Enter any Number:-- void main()


4 {
Factorial of Number 4 is 24 long int Num,Temp,Sum,Dig;

6.3 Write a program to compute the sum of the digits of a given number. clrscr();

Algorithm:-- printf("Enter any Number:--\n");


scanf("%ld",&Num);
Step 1: Read Num.
Step 2: Store Temp=Num & Sum=0.
Step 3: Repeat Step 4 to Step 5 while Temp! =0 do otherwise go to Step 6 Temp=Num;
Step 4: Compute Dig=Temp%10 & Temp=Temp/10. Sum=0;
Step 5: Compute Sum=Sum+Temp.
Step 6: Display Sum. while(Temp!=0)
{
Flowchart:-- Dig=Temp%10;
START Temp=Temp/10;
Sum=Sum+Dig;
}
Read Num
printf("Sum of Number %ld is %ld\n",Num,Sum);
getch();

Temp=Num & }
Sum=0
Output:--

Enter any Number:--


NO 12345
Temp Sum of Number 12345 is 15
! =0
6.4 The numbers in the sequence
YES 1 1 2 3 5 8 13 21 …………………….
are called Fibonacci numbers. Write a program using do ……….. while loop to calculate
Dig=Temp%10, Temp=Temp/10 and print the first m Fibonacci numbers.
Sum= Sum + Dig

Algorithm:--
Display Step 1: Read m.
Sum Step 2: Store i=1, Fib1=0 & Fib2=1.
Step 3: Check m>1 then Display Fib2 otherwise go to Step 4
Step 4: Do Step 5 to Step 7
END Step 5: Compute Fib=Fib1+Fib2, Fib1=Fib2, Fib2=Fib, i=i+1.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Step 6: Display Fib. {


Step 7: Check i<=m-1 if true then go to Step 5 otherwise go to Step 8. int m,i,Fib1,Fib2,Fib;
Step 8: End.
clrscr();
Flowchart:--
START printf("Enter Length of Series:--\n");
scanf("%d",&m);

Read Fib1=0;
m Fib2=i=1;

printf("Fibonacci Numbers-->\n");
i=1
Fib1=0 if(m>1)
Fib2=1 {
printf("%d ", Fib2);
}

NO Fib=Fib1+Fib2 do
m>1 Fib1=Fib2 {
Fib2=Fib Fib=Fib1+Fib2;
i=i+1 Fib1=Fib2;
YES Fib2=Fib;
i=i+1;
Display printf("%d ",Fib);
Display Fib
Fib2 YES }while(i<=m-1);

getch();

}
i<=m-1
Output:--

Enter Length of Series:--


NO
5
END Fibonacci Numbers-->
11235

Program:-- 6.5 Rewrite the program of the Example using the for statement.

//The numbers in the sequence A program to evaluate the equation


// 1 1 2 3 5 8 13 21 ……………………. y = xn
//are called Fibonacci numbers. Write a program using do ……….. while loop to when n is a non-negative integer.
//calculate and print the first m Fibonacci numbers.
Algorithm:--
// Date : 15/03/2010
Step 1: Read x, n.
#include<stdio.h> Step 2: Store 1.0 to y.
#include<conio.h> Step 3: For Count=1 to n repeat Step
Step 4: Compute y=y*x & Count=Count+1.
Step 5: Display x, n, y.
void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

clrscr();
Flowchart:--
START printf("Enter The Value of x and n:--\n");
scanf("%f %n",&x,&n);

y=1.0;
Read x, n
for(Count=1;Count<=n;Count++)
{
y=1.0 y=y*x;
Count=1 }

printf("\nx = %f; n = %d; x to power n = %f\n",x,n,y);

NO getch();
Count
<=n }

YES Output:--

y=y*x Enter The Value of x and n:-- 2.5 4


Count=Count+1 nx = 2.500000; n = 4; x to power n = 39.062500

Display x,
n, y
6.6 Write a program to evaluate the following investment equation

V=P (1+r) n
END And print the tables which would give the values of various combination of the following
values of P, r and n.

Program:-- P: 1000, 2000, 3000, ……………….10000


r: 0.10, 0.11, 0,12,…………………..0.20
//Rewrite the program of the Example using the for statement. n: 1,2,3…………….10

// A program to evaluate the equation


// y = xn
// when n is a non-negative integer.

Algorithm:--
// Date : 15/03/2010
Step 1: Read P.
#include<stdio.h> Step 2: For r=0.1 to 0.15 repeat Step 3 to Step 4
#include<conio.h> Step 3: For n=1 to 2 repeat Step Step 4
Step 4: Compute V=P (1+r) n
Step 5: Display r, n, V
void main()
{
int Count,n;
float x,y;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
START
// Date : 15/03/2010

Read #include<stdio.h>
P #include<conio.h>

void main()
r=0.1, n=1 {
int P,n;
float V,r,temp;

NO clrscr();
r<=0.15
printf("Enter Principal Amount:--\n");
scanf("%d",&P);
YES
printf("For P:-- %d\n",P);
NO
n<=2
for(r=0.1;r<=0.15;r+=0.01)
{
printf("For Rate %f\n",r);
YES
printf("n V");
V=P (1+r) n for(n=1;n<=5;n++)
n = n+1 {
printf("%d ",n);

Display r, n, temp=pow((1+r),n);
V V=P*temp;
printf("%f",V);
}
}
r=r+0.1
printf("\nx = %f; n = %d; x to power n = %f\n",x,n,y);

END getch();

}
Program:--
Output:--
//Write a program to evaluate the following investment equation

// V=P(1+r)n
//And print the tables which would give the values of various combination of the following values
of P, r and n.

// P: 1000, 2000, 3000, ……………….10000


// r: 0.10, 0.11, 0,12,…………………..0.20
// n: 1,2,3…………….10

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

6.7 Write a program to print the following outputs using for loops
b) START
a) 1 b) *
22 **
333 *** i=1
4444 **** j=1
55555 *****

Algorithm:-- NO
i<=5
a)
Step 1: for i=1 to 5 repeat Step 2 to Step 4
Step 2: for j=1 to 5 repeat Step 3
Step 3: Display i YES
Step 4: go to newline
NO
j<=5
b)
Step 1: for i=1 to 5 repeat Step 2 to Step 4
Step 2: for j=1 to 5 repeat Step 3
Step 3: Display * YES
Step 4: go to newline
Display *
Flowchart:--

a) START go to newline

i=1
j=1 END

Program:-
NO
i<=5 a)
// Write a program to print the following output using for loops :
//1
//2 2
YES //3 3 3
//4 4 4 4
NO
j<=5 //5 5 5 5 5

// Date: 15/03/2010
YES #include<conio.h>
#include<stdio.h>
Display i
void main()
{
go to newline
int i,j;

clrscr();
END

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

for(i=1;i<=5;i++) Output:--
{
for(j=1;j<=i;j++) *
{ **
printf("%d",i); ***
} ****
printf("\n"); *****
}
getch(); 6.8 Write a program to read the age of 100 persons and count the number of persons in the
} age group 50 to 60.

Output:-- Algorithm:--
1
22
333 Step 1: for i=1 to 10 repeat Step 2 to Step 4
4444 Step 2: Read age
55555 Step 3: Check age>=50 && age<=60 then go Step 4 otherwise go Step 1
Step 4: Compute c=c+1
b) Step 5: Display c.
//Write a program to print the following output using for loops :
//* Flowchart:--
//* * START
//* * *
//* * * *
//* * * * *
c=0
i=1
// Date: 15/03/2010

#include<conio.h>
#include<stdio.h> NO
i<=10
void main()
{
YES
int i,j,k;
Read age
clrscr();

for(i=5;i>=1;i--)
{
for(k=5;k>i;k--) age>=50 NO
printf(" "); &&
for(j=1;j<=i;j++) age<=60
{
printf("*");
}
printf("\n"); YES
}
getch(); c=c+1
}

Display C

END
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- printf(" Y-----> \n");


//Write a program to read the age of 10 persons and count the numbers of printf("0---------------------------------------\n");
//persons in teh group 50 to 60 / for(x=0;x<5;x=x+0.25)
{
// Date: 15/03/2010 y1=(int) (50*exp(-a*x)+0.5);
y2=(int) (50*exp(-a*x*x/2)+0.5);
#include<conio.h> if(y1==y2)
#include<stdio.h> {
void main() if(x==2.5)
{ printf("X |");
else
int i,age,c=0; printf("|");
for(i=1;i<=y1-1;++i)
clrscr(); printf(" ");
printf("#\n");
for(i=1;i<=10;i++) }
{ else
printf("Enter the age of the person%d:",i); {
scanf("%d",&age); if(y1>y2)
{
if (age>=50 && age<=60) if(x==2.5)
c=c+1; printf("X |");
else
} printf(" |");
for(i=1;i<y2-1;i++)
printf("The number of persons in the age group 50 to 60 are : %d",c); printf(" ");
printf("*");
getch(); for(i=1;i<=(y1-y2-1);++i)
printf("-");
} printf("0\n");
Output:-- continue;
}
else
6.9 Rewrite the program of case study 6.4 using else……if constructs instead of continue {
Statement. if(x==2.5)
printf("X |");
Program:-- else printf(" |");
//Rewrite the program of case study 6.4 using else……if constructs instead of continue for(i=1;i<=(y1-1);++i)
//Statement. printf(" ");
printf("0");
for(i=1;i<=(y2-y1-1);++i)
// Date: March15,2010 printf("-");
printf("*\n");
#include<conio.h> }
#include<stdio.h> }
#include<math.h> printf(" |\n");
}
void main() }
{

int i;
float a,x,y1,y2;
a=0.4;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

6.10 Write a program to print a table of values of the function


y = exp (-x) E D
for varying from 0.0 to 10.0 in steps of 10.0. A

Algorithm:--
sum=i+j, Ex=exp(sum)
Step 1: Display ‘x’.
Step 2: For j=0.1 to 0.5 repeat Step 3
Step 3: Display j & go to newline. Display Ex
Step 4: For i=1 to 5 repeat Step 5 to Step 9
Step 5: Display i
Step 6: For j=0.1 to 0.5 repeat Step 7 to Step 8
Step 7: Compute sum=i+j, Ex=exp(sum). j=j+0.1
Step 8: Display Ex.
Step 9: Go to newline.
Flowchart:-- C Go to newline
START

i=i+1
Display ‘x’

j=0.1 END B

Program:--
j<=
0.5 //Write a program to print a table of values of the function
// y = exp (-x)
// for varying from 0.0 to 10.0 in steps of 10.0.
Display j & go to newline
// Date : 15/03/2010

j=j+0.1 #include<stdio.h>
#include<conio.h>
#include<math.h>
i=1
void main()

E i<=5 {
float Ex,sum,i,j;

clrscr();
Display i
printf("X");

j=0.1 for(j=0.1;j<=0.5;j+=0.1)
printf(" %f",j);

printf("\n");
j<=
D
0.5
for(i=1;i<=5;i++)
{
C A B

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("%f",i); Program:--

for(j=0.1;j<=0.5;j+=0.1) //Write a program that will read a positive integer and determine and print its binary equivalent.
{ // Date : 15/03/2010
sum=i+j;
Ex=exp(sum); #include<stdio.h>
printf(" %f",Ex); #include<conio.h>
}
printf("\n");
} void main()
{
getch(); int Num,Dig,Bin[10],i,Temp,Count;
}
6.11 Write a program that will read a positive integer and determine and print its binary clrscr();
equivalent.
printf("Enter any Number:--\n");
Algorithm:-- scanf("%d",&Num);

Step 1: Read Num.


Step 2: Store Temp=Num, Count=0. Temp=Num;
Step 3: Repeat Step while Temp!=0
Step 4: Compute Dig=Temp%2, Temp=Temp/2, Bin[Count]=Dig, Count=Count+1. Count=0;
Step 5: Display Bin.
Flowchart:-- while(Temp!=0)
START {
Dig=Temp%2;
Temp=Temp/2;
Read Num Bin[Count]=Dig;
Count++;
}

Temp=Num & printf("Binary Number of Integer Number %d is \n",Num);


Cont=0
for(i=(Count-1);i>=0;i--)
printf("%d",Bin[i]);

NO
Temp getch();
! =0 }

YES
Output:--
Dig=Temp%2, Temp=Temp/2,
Bin[Count]=Dig, Count=Count+1 Enter any Number:--
5
Binary Number of Integer Number 5 is 101

Display
Bin

END

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

6.12 Write a program using for and if statement to display the capital letter S in a grid of 15 j=1;
rows & 18 columns as shown below.
//first
****************** for(i=1;i<=3;i++)
****************** for(j=1;j<=18;j++)
****************** {
**** printf("*");
**** if(j==18)
**** printf("\n");
****************** }
******************
****************** //second
**** for(i=1;i<=3;i++)
**** for(j=1;j<=4;j++)
**** {
****************** printf("*");
****************** if(j==4)
****************** printf("\n");
}
Algorithm:--
//3rd
Step 1: Store 1 to i, j & k
Step 2: Display S using for & if for(i=1;i<=3;i++)
for(j=1;j<=18;j++)
Flowchart:-- {
START printf("*");
if(j==18)
printf("\n");
i=j=k=1 }

//4th
Display S
using for & if for(i=1;i<=3;i++)
{
for(k=1;k<=14;k++)
printf(" ");
END for(j=15;j<=18;j++)
{
printf("*");
Program:-- if(j==18)
printf("\n");
// Write a program using for and if statement to display the S. }
//15/03/2010 }

for(i=1;i<=3;i++)
#include<stdio.h> for(j=1;j<=18;j++)
#include<conio.h> {
void main() printf("*");
{ if(j==18)
int i,j,k; printf("\n");
}
clrscr();

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

getch(); Program:--

} // Write a program to compute the value of Euler’s number that is used as the base of natural
Output:-- logarithms.
****************** //Use the following formula.
******************
****************** // e= 1+ 1/1! +1 /2! + 1/3+……………. 1/n!
****
**** //15/03/2010
****
******************
****************** #include<stdio.h>
****************** #include<conio.h>
**** void main()
**** {
**** float i,n;
****************** float e1,e2;
******************
****************** clrscr();

6.13 Write a program to compute the value of Euler’s number that is used as the base of printf("Enter No.");
natural logarithms. Use the following formula. scanf("%f",&n);

e= 1+ 1/1! +1 /2! + 1/3+……………. 1/n! e2=1;


Algorithm:-- e1=1;

Step 1: Read n. for(i=1;i<=n;i++)


Step 2: Store 1 to e1 & e2. {
Step 3: For i=1 to n repeat Step 4 to Step 6 e1=e1+((float)1/fact(i));
Step 4: Compute e1=e1+(1/i!)
Step 5: Check (e1-e2)<0.00001 then break otherwise go to Step 5 if((e1-e2)<0.00001)
Step 6: Compute e2=e1. break;
Step 7: Display e1. e2=e1;

Flowchart:-- START }

printf("The value of e is : %f",e1);


e1=1 & e2=1
i=1 getch();
}
NO
i<=n 6.14 Write programs to evaluate the following functions to 0.0001% accuracy.
YES a) sinx = x – x3/3! + x5/5! – x7/7! + .............
b) cosx= 1 – x2/2! + x4/4! – x6/6! + ………..
e1=e1+(1/i!) c) SUM= 1 + (½)2 + (1/3)3 +(1/4)4 + ………

(e1-e2) < 6.16 Write a program to print a square of size 5 by using the character S as shown below
0.00001 a) S S S S S b) S S S S S
SSSSS S S
YES SSSSS S S
NO
SSSSS S S
e1=e2
SSSSS SSSSS

Display e1 END
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:--
Algorithm:--
a) //Write a program to print a square of size 5 by using the character S as shown below
a)
//a) SSSSS
Step 1: For i=1 to 5 repeat Step 2 to Step 4 // SSSSS
Step 2: For j=1 to 5 repeat Step 3 // SSSSS
Step 3: Dispay S // SSSSS
Step 4: go to newline // SSSSS

b)
Step 1: For i=1 to 5 repeat Step 2 #include<stdio.h>
Step 2: Dispay S #include<conio.h>
Step 3: For j=1 to 3 repeat Step 4 #include<math.h>
Step 4: Dispay S S
Step 5: For i=1 to 5 repeat Step 2 void main()
Step 6: Dispay S {
int j,i;
Flowchart:--
a) clrscr();
START for (i=1;i<=5;i++)
{
i=1 for(j=1;j<=5;j++)
printf("S");

NO printf("\n");
i<=5 }
getch();
}
YES
b) //Write a program to print a square of size 5 by using the character S as shown below
j=1
//S S S S S
//S S
NO //S S
j<=5 //S S
//S S S S S
YES #include<stdio.h>
#include<conio.h>
Dispay S

void main()
go to newline {
int j,i,k;

clrscr();
END
for (i=1;i<=5;i++)
printf("S");

for(j=2;j<=4;j++)

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

{
printf("\nS S"); // Date : 15/03/2010
}
#include<stdio.h>
printf("\n"); #include<conio.h>
#include<math.h>
for (i=1;i<=5;i++)
printf("S"); void main()

getch(); {
} float y;
int x,i;
6.17 Write a program to graph the function
y = sin(x) clrscr();
in the interval 0 to 180 degrees in steps of 15 degrees.
printf("X Sin(X)\n");

Algorithm:-- for(i=0;i<=180;i+=15)
{
Step 1: For x=0 to 180 repeat Step 2 to Step 3 y=sin(x);
Step 2: Compute y = sin(x) printf("%d %f\n",x,y);
Step 3: Display x, y. }

Flowchart:-- getch();
START }

x=0
6.18 Write a program to print all integers that are not divisible by either 2 or 3 and lie
between 1 and 100. Program should also account the number of such integers and print
the result.
NO
x<=180

YES

y=sin(x) Algorithm:--

Step 1: For i=1 to 100 repeat Step 2 to Step 4


Step 2: Check i%2!=0 && i%3!=0 then go to Step 3 otherwise go to Step 1
Step 3: Compute Count=Count+1
Display x, y Step 4: Display i & Count.

END

Program:--

//Write a program to graph the function


// y = sin(x)
// in the interval 0 to 180 degrees in steps of 15 degrees.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

{
Flowchart:-- if(i%2!=0 && i%3!=0)
START {
Count=Count+1;
printf("%d",i);
i=1 }

printf("%d\n",Count);
}

i%2!=0 NO getch();
&& }
i%3!=0

YES

Count=Count+1

Display i &
Count

i = i+1

END

Program:--

//Write a program to print all integers that are not divisible by either 2 or 3 and lie
// between 1 and 100. Program should also account the number of such integers and print
// the result.

// Date : 15/03/2010

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()

{
int i,Count;

clrscr();

Count=0;

for(i=1;i<=100;i++)

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

CHAPTER 7 16/03/2010
Flowchart:--

START

7.1 Write a program for fitting a straight line through a set of points (xi,
yi),i=1,2,3….n. The straight line equation is: n=10

Y =mx+c
i=0
and the values of m and c are given by:

m=((n ∑(xi,yi))- (∑xi)(∑yi) )/( n(∑xi2)-(∑xi)2)


No
c=1/n(∑yi -m(∑xi))
i<10
All summations are from 1 to n.
Yes
i=0

Read v1
No
Algorithm:-
i<10
Step 1: Store n=10 Yes
Step 2: For i=1 to i=10, Enter the values of x[i]=v1. i=i+1
Step 3: For i=1 to i=10, Enter the values of y[i]=v2.
Step 4: Assign 0 to total_x,total_y,total_xy,total_x2. Read v2
Step 5: For i=0 to i=10,repeat step 6.
Step 6: Compute total_x=total_x+x[i]
total_y=total_y+y[i]
total_xy=total_xy+(x[i]*y[i]) i=i+1
total_x2=total_x2+(x[i]*x[i])

Step 7: Compute temp=total_x*total_y total_x=total_y=total_xy=


templ=total_x+total_x
total_x2=0
m=((n*total_xy)-(temp))/((n*total_x2)-temp1)
c=((total_y)-(m*total_x))/n

Step 8: Display y=mx+c.


Step 9: Stop temp=total_x*total_y i=0
templ=total_x+total_x
m=((n*total_xy)-(temp))/((n*total_x2)-temp1)
c=((total_y)-(m*total_x))/n No
i<10

Yes
Display
Y=mx+c total_x=total_x+x[i]
total_y=total_y+y[i]
total_xy=total_xy+(x[i]*y[i])
total_x2=total_x2+(x[i]*x[i])

END

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

c=((total_y)-(m*total_x))/n;
Program
/* Write a program for fitting a straight line through a set of points (xi, yi),i=1,2,3….n. printf(" \nThe equation of the straight line is: ");
The straight line equation is: printf(" Y=%fX+%f",m,c);
Y =mx+c
and the values of m and c are given by: getch();
m=((n ∑(xi,yi))- (∑xi)(∑yi) )/( n(∑xi2)-(∑xi)2) }
c=1/n(∑yi -m(∑xi))
All summations are from 1 to n. */ Output
Enter the values for x:
// Date March 16,2010 1 2 3 4 5 6 7 8 9 10
Enter the values for y:
#include<stdio.h> 1 2 3 4 5 6 7 8 9 10
#include<conio.h> The equation of the straight line is:
Y=1.00000X+0.000000
void main()
7.2 The daily maximum temperature recorded in 10 cities during the month of January
{ (for all 31 days) have been tabulated as follows:
int i,n=10,v1,v2,x[10],y[10];
int total_x,total_y,total_xy,total_x2; City
float m,c,temp,temp1; 1 2 3 4 5 6 ……………………………10
Day
clrscr(); 1
2
printf("Enter the values for x: "); 3
.
for(i=0;i<10;i++) .
{ .
scanf("%d",&v1); .
x[i]=v1; 31
}
Write a program to read the table elements into a two-dimensional array temperature, and
printf("Enter the values for y: "); to find the city and day corresponding to
a) the highest temperature
for(i=0;i<10;i++) b) the lowest temperature
{
scanf("%d",&v2); Algorithm:--
y[i]=v2;
} Step 1: Read Array Temp.
Step 2: Display Temp
total_x=total_y=total_xy=total_x2=0; Step 3: Store MinTemp=MaxTemp=Temp [0][0],City1=0,City2=0.
Step 4: For i=0 to 2 repeat Step 5 to Step
for(i=0;i<10;i++) Step 5: For j=0 to 2 repeat Step 6 to Step
{ Step 6: Check MaxTemp<Temp[i][j] go to Step 7
total_x=total_x+x[i]; Step 7: Compute MaxTemp=Temp[i][j],City1=j+1
total_y=total_y+y[i]; Step 8: Check MinTemp>Temp[i][j] go to Step 9
total_xy=total_xy+(x[i]*y[i]); Step 9: Compute MinTemp=Temp[i][j], City2=j+1
total_x2=total_x2+(x[i]*x[i]); Step 10: Display MaxTemp & MinTemp
}

temp= total_x*total_y;
temp1=total_x*total_x;
m=((n*total_xy)-(temp))/((n*total_x2)-temp1);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- Program:--
A
//Write a program to read the table elements into a two-dimensional array temperature, and to
find the city and day corresponding to
i=0 //a) the highest temperature
//b) the lowest temperature

// Date : 16/03/2010
i<2
#include<stdio.h>
#include<conio.h>
START j=0
void main()
{
Read Array int Temp[2][2];
Temp j<2 int i,j,City1,City2,MaxTemp,MinTemp;

clrscr();

Display Temp printf("Enter temperature:--\n\n");


MaxTemp<
Temp[i][j] for(i=0;i<2;i++)
{
MinTemp= printf("For City %d ->\n",i);
MaxTemp=Temp [0][0]
&City1=0,City2=0 MaxTemp=Temp[i][j],
City1=j+1 for(j=0;j<2;j++)
{
printf("For Day %d ->",j);
scanf("%d",&Temp[i][j]);
A
}
MinTemp>
}
Temp[i][j]
clrscr();

printf("Temperature Matix :--- \n");


MinTemp=Temp[i][j],
City2=j+1 printf(" City \n ");

for(i=0;i<2;i++)
Display printf("%d ",i+1);
MaxTemp &
MinTemp printf("\n Day\n");

for(i=0;i<2;i++)
{
END printf(" %d ",i+1);

for(j=0;j<2;j++)
{
printf(" %d",Temp[i][j]);
}
printf("\n");
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

7.3 An election is contested by 5 candidates. The candidate are numbered are 1 to 5


MinTemp=MaxTemp=Temp[0][0]; and the voting is done by marking the candidate number on the ballot paper. Write
City1=0; a program to read the ballots and count the votes cast for each candidate using an
City2=0; array variable count. In case, a number, read is outside the range 1 to 5,the ballot
should be considered as a ‘spoilt ballot’ and the program should also count the
for(i=0;i<2;i++) number of spoilt ballots.
{
for(j=0;j<2;j++)
{
if(MaxTemp<Temp[i][j])
{
MaxTemp=Temp[i][j];
City1=j+1;
} Algorithm:--

if(MinTemp>Temp[i][j]) Step1. Initialize c1,c2, c3,c4,c5,count,count_sp with 0.


{ Step2. For i=0 to i=5, repeat step3.
MinTemp=Temp[i][j]; Step3. Enter the value of v and store in vote[i]
City2=j+1; Step4. For i=0 to i=5, repeat from step5 till step14 and for i>5,go to step15.
} Step5. Check if vote[i]==1,if true, go to step6,if false go to step7.
} Step6.Calculate c1=c1+1
} Step7. Check if vote[i]==2,if true, go to step8,if false go to step9.
Step8.Calculate c2=c2+2
printf("\n\nHighest Temperature of City %d is %d\n",City1,MaxTemp); Step9.Check if vote[i]==3,if true, go to step10,if false go to step11.
printf("Lowest Temperature of City %d is %d\n",City2,MinTemp); Step10. Calculate c3=c3+2
Step 11.Check if vote[i]==4,if true, go to step12,if false go to step13.
getch(); Step12.Calculate c4=c4+2
} Step13.Check if vote[i]==5,if true, go to step14,if false go to step15.
Step14.Calculate c5=c5+2
Step15. Display c1,c2,c3,c4 and c5.
Step16. For i=0 to i=5, repeat from step 17 to step, and i>5,go to step 20
Step17.Check if vote[i]<5,if true go to step18 and if false, go to step 19.
Step18.count=count+1 and go to step 20.
Step19.count_sp=count_sp+1 and go to step 20.
Step20. Display count and count_sp
Step21. Stop.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:--
Flowchart:--
/* An election is contested by 5 candidates.

The candidate are numbered are 1 to 5 and the voting is done by marking the candidate
number on the ballot paper. Write a program to read the ballots and count the votes cast for
Start each candidate using an array variable count. In case, a number, read is outside the range 1
to 5,the ballot should be considered as a ‘spoilt ballot’ and the program should also count
the number of spoilt ballots. */
c1=c2=c3=c4=c5=cou
// Date March 16,2010
nt=count_sp=0
c1,c2,c3,
c4,c5 #include<stdio.h>
#include<conio.h>
i=1
void main()

i=1 Vote[i]== c1=c1+ {


i<=5 i<=5
1 1 int i,vote[5],c1=0,c2=0,c3=0,c4=0,c5=0,count=0,count_sp=0,v;

clrscr();
Read
v c2=c2+ printf("Enter your votes for 5 candidates:");
Vote[i]==
2 1 for(i=1;i<=5;i++)
Vote[i]= {
v scanf("%d",&v);
vote[i]=v;
}
i=i+
1 Vote[i]== c3=c3+
3 1 for(i=1;i<=5;i++)
{
if(vote[i]==1)
c1=c1+1;
else
Vote[i]== c4=c4+ {
4 1 if(vote[i]==2)
c2=c2+1;
else
{
Vote[i]== c5=c5+ if(vote[i]==3)
i=i+ c3=c3+1;
1 5 1
else
{
if(vote[i]==4)
Count c4=c4+1;
count=count Vote[i i<=5 i=1
Count_s else
+1 ]<=5 if(vote[i]==5)
p
c5=c5+1;
}
i=i+1 }
END count_sp=count_sp+
}
1
}
printf(" votes to candidate1=%d",c1);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf(" \nvotes to candidate2=%d",c2); Step1. Declare


printf("\n votes to candidate3=%d",c3); Step2. For i=0 to i<10, Enter sub1[i]
printf(" \nvotes to candidate4=%d",c4); Step3. For i=0 to i<10, Enter sub2[i]
printf(" \nvotes to candidate5=%d",c5); Step4. For i=0 to i<10, Enter sub3[i]

for(i=1;i<=5;i++) START
{
if(vote[i]<=5)
count=count+1;
else i=0
count_sp=count_sp+1;
}
N
printf(" The number of valid votes is:%d",count); i<10 i=0
printf(" \nThe number of spoilt votes is:%d",count_sp);
Y
getch(); N
} Enter i=0
i<10
sub1[i]
Y
Output
Enter your votes for 5 candidates: N Total_sub1=total_sub2
Enter i<10
1 sub2[i] =total_sub3=0
3 Y
1
8 Enter
2 sub3[i] i=0
Votes to Candidate 1: 2
Votes to Candidate 2: 1
Votes to Candidate 3:1 N
Votes to Candidate 4:0
Votes to Candidate 5:0 i=0 i<10
The number of valid votes is: 4 Y
The number of spoilt votes is: 1
max1=sub1[0] Y total_sub1=total_sub1+sub1[
5.1 The annual examination results of 10 students are tabulated as follows: max2=sub2[0] i]
i<10
max3=sub3[0] total_sub1=total_sub1+sub1[
i=0
max=total[0] i]
Roll No. Subject1 Subject2 Subject3 N
roll=roll1=roll2=roll3 total_sub1=total_sub1+sub1[
. =0
. Student i]
. [i+1] has
.____________________________________________________________________ total[i]
Y N
Write a program to read the data and determine the following: i<10
(a) Total marks obtained by each student.
N
(b) The highest marks in each subject and the Roll No. of the student who
secured it. Y
max1<sub1[i max1=sub1[i
(c) The student who obtained the highest total marks. ] ] N max1,roll1
roll1=i+1 max2,roll2
Algorithm:--
max3,roll3
max,roll

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

scanf("%d",&sub1[i]);

printf("Enter the marks for subject2 of all the students: ");

for(i=0;i<MAX;i++)
Y scanf("%d",&sub2[i]);
max2<sub2[i max1=sub1[i
] ]
roll1=i+1 printf("Enter the marks for subject3 of all the students: ");
Y
max3<sub3[i max1=sub1[i for(i=0;i<MAX;i++)
] STOP
] N scanf("%d",&sub3[i]);
roll1=i+1
Y total_sub1=total_sub2=total_sub3=0;
max<total[i] max1=sub1[i
] N for(i=0;i<MAX;i++)
roll1=i+1
{
total_sub1=total_sub1+sub1[i];
total_sub2=total_sub2+sub2[i];
Program:-- total_sub3=total_sub3+sub3[i];
total[i]=sub1[i]+sub2[i]+sub3[i];
/* The annual examination results of 10 students are tabulated as follows: }

Roll No. Subject1 Subject2 Subject3


. for(i=0;i<MAX;i++)
. {
. printf("The total marks obtained by the student%d is =%d\n",i+1,total[i]);
. }

Write a program to read the data and determine the following: max1=sub1[0];
(a)Total marks obtained by each student. max2=sub2[0];
(b) The highest marks in each subject and the Roll No. of the student who secured it. max3=sub3[0];
(c) The student who obtained the highest total marks. max=total[0];
*/ roll1=0;
roll2=0;
// Date March 16,2010 roll3=0;
roll=0;
#include<stdio.h> for (i=0;i<MAX;i++)
#include<conio.h> {
#define MAX 10 if(max1<sub1[i])
{
void main() max1=sub1[i];
roll1=i+1;
{ }
int i,roll,m1,m2,m3,sub1[MAX],sub2[MAX],sub3[MAX]; if(max2<sub2[i])
int total_sub1,total_sub2,total_sub3,total[MAX]; {
int max,max1,max2,max3,roll1,roll2,roll3; max2=sub2[i];
roll2=i+1;
clrscr(); }

printf("Enter the marks for subject1 of all the students: "); if(max3<sub3[i])
{
for(i=0;i<MAX;i++) max3=sub3[i];

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

roll3=i+1; Flowchart:--
}
Start
if(max<total[i])
{
max=total[i]; Read m,n arrays a, & b
roll=i+1;
}
} ax=bx=cx=0
mn=m+n

printf("\nThe highest marks in subject1 is %d and the roll number is %d",max1,roll1); NO


printf("\nThe highest marks in subject2 is %d and the roll number is %d",max2,roll2); ax<n && bx<m
printf("\nThe highest marks in subject3 is %d and the roll number is %d",max3,roll3);
printf("\n The highest total marks is %d and the roll number is %d ",max,roll); YES NO
a[ax]<b[bx]
getch();
YES
}
c[cx]=a[ax]
ax=ax+1
7.6 Given are one dimensional arrays A and B which are sorted in ascending
order. Write a program to merge them into a single sorted array C that contains c[cx]=b[bx],
every item form array A and B, in ascending order. bx=bx+1

cx=cx+1

NO
Algorithm:-- ax==n
YES
Step 1: Read m, n, Array a & Array b.
Step 2: Store 0 to ax, bx and cx. bx<m
Step 3: Compute mn=m+n YES
Step 4: Repeat Step 5 to Step 8 while ax<n && bx<m otherwise go to Step 9 c[cx]=b[bx],
Step 5: Check a[ax]<b[bx] then go to Step 6 otherwise go to Step 7 bx=bx+1, cx=cx+1
Step 6: Compute c[cx]=a[ax], ax=ax+1
Step 7: Compute c[cx]=b[bx], bx=bx+1 NO
Step 8: Compute cx=cx+1 ax<n
Step 9: Check ax==n then go to Step 10 otherwise go to Step 12
Step 10: Repeat Step 11 while bx<m YES
Step 11: Compute c[cx]=b[bx], bx=bx+1, cx=cx+1. c[cx]=a[ax],
Step 12: Repeat Step 13 while ax<n ax=ax+1, cx=cx+1
Step 13: Compute c[cx]=a[ax], ax=ax+1, cx=cx+1.
Step 14: Display Sorted Array c.
Display C

End

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- if(ax==n)
{
// Given are one dimensional arrays A and B which are sorted in ascending while(bx<m)
// order. Write a program to merge them into a single sorted array C that contains {
// every item form array A and B, in ascending order. c[cx]=b[bx];
bx++;
cx++;
}
//Date: 16/03/2010 }
else
#include<stdio.h> {
#include<conio.h> while(ax<n)
{
#define MAX 50 c[cx]=a[ax];
ax++;
void main() cx++;
{ }
}
int a[MAX],b[MAX],c[MAX]; //sorted array
int ax,bx,cx,n,m,mn; printf("the sorted array is : \n");
for(i=0;i<mn;i++)
clrscr(); printf("%d ",c[i]);

ax=bx=cx=0; getch();
}
printf("Enter no. of elements of array : ");
scanf("%d %d",&n,&m);

printf("Enter elements of first array :\n");


for(i=0;i<n;i++) 7.7 Write a program that will read the values of elements of A and B and produce the
scanf("%d",&a[i]); product matrix C.

printf("Enter elements of Second array :");


for(i=0;i<m;i++)
scanf("%d",&b[i]); Algorithm:--

mn=m+n; Step 1: Read row, col, Array a and Array b.


Step 2: Display Array a and b.
while(ax<n && bx<m) Step 3: For i=0 to row repeat Step 4 to Step 7
{ Step 4: For j=0 to col repeat Step 5 to Step 7
if(a[ax]<b[bx]) Step 5: Store 0 to c[i][j]
{ Step 6: For k=0 to col repeat Step 7
c[cx]=a[ax]; Step 7: Compute c[i][j]=c[i][j]+(a[i][k]*b[k][j])
ax++; Step 8: Display c.
}
else
{
c[cx]=b[bx];
bx++;
}
cx++;
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- Program:--
START
// Write a program that will read the values of elements of A and B and produce the
// product matrix C.

Read row, col, Array


a and Array b //Date: 16/03/2010

#include<stdio.h>
#include<conio.h>
Display Array a and b
#define MAX 10

void main()
i=0 {

int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX];
int i,j,k,row,col;
i<row
clrscr();

printf("Enter row of matrix");


j=0 scanf("%d",&row);

printf("Enter column of matrix");


j<col scanf("%d",&col);

printf("Enter first matrix\n");

for(i=0;i<row;i++)
c[i][j] = 0 for(j=0;j<col;j++)
scanf("%d",&a[i][j]);

k=0 printf("\nEnter second matrix \n");

for(i=0;i<row;i++)
k<col for(j=0;j<col;j++)
scanf("%d",&b[i][j]);

printf("\nFirst matrix is : \n");

for(i=0;i<row;i++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]) {
for(j=0;j<col;j++)
printf("%d ",a[i][j]);
printf("\n");
Display Array a and b }

printf("\nSecond matrix is\n");

END for(i=0;i<row;i++)
{
for(j=0;j<col;j++)

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("%d ",b[i][j]);
printf("\n"); Flowchart:--
} START

for(i=0;i<row;i++)
for(j=0;j<col;j++)
{ c[i][j]=0; Store Upper left
for(k=0;k<col;k++) triangle with +1s
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
Store Lower right
printf("\nMultiplication is\n");
Triangle with -1s
for(i=0;i<row;i++)
{
for(j=0;j<col;j++) Store Right to left
printf("%d ",c[i][j]); diagonal with zeros
printf("\n");
}

getch(); Display A
}

END

Program:--

7.8 Write a program that fills a five-by-five as follows: //Write a program that fills a five-by-five as follows:
 Upper left triangle with +1s //• Upper left triangle with +1s
 Lower right triangle with -1s //• Lower right triangle with -1s
 Right to left diagonal with zeros //• Right to left diagonal with zeros
Display the contents of the matrix using not more than two printf statements. //Display the contents of the matrix using not more than two printf statements.

// Date : 16/03/2010

#include<stdio.h>
#include<conio.h>
Algorithm:--
void main()
Step 1: Store Upper left triangle with +1s {
Step 2: Store Lower right triangle with -1s int A[5][5];
Step 3: Store Right to left diagonal with zeros int a,i,k,j;
Step 4: Display A
clrscr();

a=3;

for(i=0;i<=3;i++)
{
for(j=0;j<=a;j++)
{
A[i][j]=+1;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

}
a--; Flowchart:-- START
}

j=4; Read Array A

for(i=0;i<=4;i++)
{ k=0
A[i][j]=0;
j--;
} NO
k<=9
a=4;
YES
for(i=1;i<=4;i++)
{ Small=A[k] & Loc=k
for(j=4;j>=a;j--)
{
A[i][j]=-1; i=0
}
a--;
} NO
i<=9

printf("Array is:--\n\n");
YES
for(i=0;i<=4;i++)
{ Small
for(j=0;j<=4;j++) >A[i]
printf("%d ",A[i][j]); YES
printf("\n");
} Small=A[i] & Loc=i

getch(); A[Loc]=A[k], A[k]=Small


}

Display A

7.9 Write a program to implement selection sort.


END

Algorithm:--

Step 1: Read Array A. Program:--


Step 2: For k=0 to 9 repeat Step 3 to Step 8.
Step 3: Compute Small=A[k] & Loc=k. //Write a program to implement selection sort.
Step 4: For i=0 to 9 repeat Step 5 to Step 7 otherwise go to Step 7. // Date : 16/03/2010
Step 5: Check Small>A[i] then go to Step 6 otherwise go to Step 4.
Step 6: Compute Small=A[i] & Loc=i. #include<stdio.h>
Step 7: Compute A[Loc]=A[k], A[k]=Small. #include<conio.h>
Step 8: Display Sorted Array A.

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

void main() Flowchart:--


{ START
int A[10];
int i,k,Small,Loc;
Beg=0, End=9 NO
clrscr(); Mid= (Beg+End)/2 Beg>End

printf("Enter Elements of Array:---\n"); YES


Read a Sorted Array
for(i=0;i<=9;i++) Str & an Item Display “Item
scanf("%d",&A[i]); Not Found”

for(k=0;k<=9;k++)
{ Display “Item
Small=A[k]; Item!=Str[ NO Found”
Loc=k; Mid] &&
(Beg<=End)
for(i=k;i<=9;i++) END
if(Small>A[i])
YES
{
Small=A[i]; NO
Loc=i; Item<Str[ Beg=Mid+1
} Mid]

A[Loc]=A[k]; YES
A[k]=Small;
} End=Mid-1 Mid=(Beg+End)/2

printf("Sorted Array is:--\n\n");

for(i=0;i<=9;i++) Program:--
printf("%d ",A[i]);
//Write a program to implement Binary Search algorithm.
// Date : 16/03/2010
getch();
} #include<stdio.h>
#include<conio.h>
7.10 Write a program to implement Binary Search algorithm.
void main()
Algorithm:-- {
int Str[10];
Step 1: Store 0 to Beg & 9 to End. int i,Beg,End,Mid,Item;
Step 2: Compute Mid=(Beg+End)/2.
Step 3: Read a Sorted Array Str & an Item to Search. clrscr();
Step 4: Repeat Step 5 to Step 8 while Item!=Str[Mid])&&(Beg<=End) otherwise go to Step 9
Step 5: Check Item<Str[Mid] then go to Step 6 otherwise go to Step 7 Beg=0;
Step 6: Compute End=Mid-1 End=9;
Step 7: Compute Beg=Mid+1
Step 8: Compute Mid=(Beg+End)/2 Mid=(Beg+End)/2;
Step 9: Check Beg>End go to Step 10 otherwise go to Step 11
Step 10: Display “Item Not Found” printf("Enetr Any Sorted Array:--\n");
Step 11: Display “Item Found”
for(i=0;i<10;i++)

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

scanf("%d",&Str[i]); Program:--

printf("Enter Item Which U want to Search:--\n"); //Write a program that will compute the length of a given character string.
scanf("%d",&Item); // Date : 16/03/2010

while((Item!=Str[Mid])&&(Beg<=End)) #include<stdio.h>
{ #include<conio.h>
if(Item<Str[Mid])
End=Mid-1; void main()
else {
Beg=Mid+1; char Str[50];
int i,Len;
Mid=(Beg+End)/2;
} clrscr();

if(Beg>End) Len=0;
printf("Item Not Found\n");
else printf("Enter a String:---\n");
printf("%d Found At Index %d\n",Item,Mid); scanf("%[^\n]s",&Str);

getch(); for(i=0;Str[i]!='\0';i++)
} Len=Len+1;
7.11 Write a program that will compute the length of a given character string.
printf("Length of String is %d",Len);
Algorithm:-- getch();

Step 1: Read a string Str. }


Step 2: For i=0 to End of String repeat Step 3 to Step
Step 3: Compute Len=Len+1
Step 4: Display Len.
Flowchart:-- START
7.12 Write a program that will count the number occurrences of a specified character in a
given line of text.

Read a string Str

i=0 Algorithm:--

Step 1: Read a string Str & a Character CheckChar


NO Step 2: Length of String is Len
End of Step 3: For i=0 to Len repeat Step 4 to Step 5
String Step 4: Check CheckChar==Str[i] go to Step 5 otherwise go to Step 3
Step 5: Count=Count+1
YES Step 6: Display Count
Len = Len+1
i = i+1

Display Len

END
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
START clrscr();

Count=0;

printf("Enter a String:---\n");
Read a string Str scanf("%[^\n]s",&Str);
& a Character
CheckChar Len=strlen(Str);

fflush(stdin);

Length of String printf("Enter a charatcer:--\n");


is Len & i=0 scanf("%c",&CheckChar);

for(i=0;i<=Len;i++)
NO if(CheckChar==Str[i])
i<=Len Count=Count+1;

YES printf("Number of occurences of %c is %d",CheckChar,Count);


getch();
CheckChar
}
==Str[i]

YES

Count=Count+1
i=i+1
7.13 Write a program to read a matrix of size m*n and print its transpose.

Display
Count
Algorithm:--

Step 1: Read Row, Col & Matrix A


END Step 2: Display A
Step 3: For i=0 to Col repeat Step 4 to Step 5 otherwise go to Step 6
Step 4: For j=0 to Row repeat Step 5 otherwise go to Step 3
Program:-- Step 5: Compute C[i][j]=A[j][i]
Step 6: Display C
//Write a program that will count the number occurrences of a specified character in a
// given line of text.

// Date : 16/03/2010

#include<stdio.h>
#include<conio.h>

void main()
{
char Str[50],CheckChar;
int i,Count,Len;

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:-- #define MAX 10


START
void main()
{
int A[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
Read Row, Col
& Matrix A clrscr();

printf("Enter Number of Rows:--\n");


scanf("%d",&Row);
Display A
printf("Enter Number of Column:--\n");
scanf("%d",&Col);
i=0
printf("Enter Matrix:---\n");
NO for(i=0;i<Row;i++)
i<Col for(j=0;j<Col;j++)
scanf("%d",&A[i][j]);
YES

j=0 clrscr();

printf("Matrix:---\n");
NO
j<Row for(i=0;i<Row;i++)
{
YES for(j=0;j<Col;j++)
C[i][j]=A[j][i] printf("%d ",A[i][j]);
printf("\n");
j=j+1
}

for(i=0;i<Col;i++)
i=i+1 for(j=0;j<Row;j++)
C[i][j]=A[j][i];

printf("Transpose of Matrix:---\n");
Display C
for(i=0;i<Col;i++)
{
END for(j=0;j<Row;j++)
printf("%d ",C[i][j]);
printf("\n");
}
Program:--
getch();
//Write a program to read a matrix of size m*n and print its transpose.
}
// Date : 16/03/2010

#include<stdio.h>
#include<conio.h>

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

7.14 Every book published by international publishers should carry an International Program:--
Standard Book Number (ISBN). It is a 10 character 4 part number as shown below.
//Write a program that reads a given ISBN number and check whether it represents a valid ISBN.
0-07-041183-2
The first part denotes the region, the second represents publisher, the third identifies the // Date : 16/03/2010
book and the fourth is the check digit. The check digit is computed as follows:
#include<stdio.h>
Sum= (1*first digit) + (2*second digit) + (3*third digit)+…………….+ (9*ninth digit) #include<conio.h>

Check digit is the remainder when Sum is divided by 11. Write a program that reads a #define MAX 10
given ISBN number and check whether it represents a valid ISBN.
void main()
Algorithm:-- {
Step 1: Read Array ISBN. int ISBN[11];
Step 2: Compute Sum=Sum+(i*ISBN[i]) for i=0 to 9 int i,j,Sum,CheckDig;
Step 3: Compute CheckDig=Sum%11
Step 4: Check CheckDig=ISBN[10] then go to Step 5 Otherwise go to Step 6 clrscr();
Step 5: Display “Valid ISBN”
Step 6: Display “Invalid ISBN” Sum=0;

Flowchart:-- printf("Enter ISBN Number:---\n");


START
for(i=1;i<=10;i++)
scanf("%d",&ISBN[i]);
Read Array for(i=1;i<=9;i++)
ISBN [11] Sum=Sum+(i*ISBN[i]);

CheckDig=Sum%11;
i=0
if(CheckDig==ISBN[10])
printf("\nValid ISBN\n");
NO else
i<=9 printf("\nInvalid ISBN\n");

YES getch();

Sum=Sum+(i*ISBN[i]) }
i=i+1

7.15 Write a program to read two matrices A and B and print the following:
CheckDig=Sum%11
a) A + B and
b) A – B.
NO Algorithm:--
CheckDig=
Invalid ISBN
=ISBN[10]
Step 1: Read Row, Col, Array A & B.
Step 2: Display Array A & B.
YES
Step 3: Compute Addition of Array A & B & Store in Array C.
Valid ISBN END Step 4: Display Array C

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
START printf("Enter Number of Column:--\n");
scanf("%d",&Col);

printf("Enter First Matrix:---\n");


Read
Row,Col
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
Read Array A
scanf("%d",&A[i][j]);
&B
printf("Enter Second Matrix:---\n");

Display Array A & B for(i=0;i<Row;i++)


for(j=0;j<Col;j++)
scanf("%d",&B[i][j]);

Compute Addition of clrscr();


Array A & B & Store in
Array C printf("First Matrix:---\n");

for(i=0;i<Row;i++)
{
Display Array C for(j=0;j<Col;j++)
printf("%d ",A[i][j]);
printf("\n");
}
END
printf("Second Matrix:---\n");

for(i=0;i<Row;i++)
{
Program:-- for(j=0;j<Col;j++)
//Write a program to read two matrices A and B and print the following: printf("%d ",B[i][j]);
printf("\n");
//a) A + B and }
//b) A – B.
for(i=0;i<Row;i++)
// Date : 16/03/2010 for(j=0;j<Col;j++)
C[i][j]=A[i][j]+B[i][j];
#include<stdio.h>
#include<conio.h> printf("Addition of Matrix:---\n");

#define MAX 10 for(i=0;i<Row;i++)


{
void main() for(j=0;j<Col;j++)
{ printf("%d ",C[i][j]);
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX]; printf("\n");
int Row,Col,i,j; }

clrscr(); for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
printf("Enter Number of Rows:--\n"); C[i][j]=A[i][j]-B[i][j];
scanf("%d",&Row);

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("Subtration of Matrix:---\n"); CHAPTER 8 18/03/2010


8.3 Write a program to extract a portion of a character string and print the extracted string.
for(i=0;i<Row;i++) Assume that m characters are extracted, starting with the nth character.
{
for(j=0;j<Col;j++) Algorithm:--
printf("%d ",C[i][j]);
printf("\n"); Step 1: Read a String Str.
} Step 2: Read Number of Characters Which We Want to Extract Say m.
Step 3: Read Beginnig Index from Which We Want to Extract Say n.
getch(); Step 4: For i=n-1 to m+n-1 repeat Step 5.
Step 5: Display Str[i].
}
Flowchart:-- START

Read String
Str, m & n

i=n-1

NO
i<m+n-1

YES

Display
Str[i]

END

Program:--

//Write a program to extract a portion of a character string and print the extracted string.
//Assume that m characters are extracted, starting with the nth character.
//Date: 18/03/2010

#include<stdio.h>
#include<conio.h>

#define MAX 50

void main()

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

{ Say Veh[i], St_Mon[i], En_Mon[i] & Price[i].


char Str1[MAX]; Step 3: Display Veh[i], St_Mon[i], En_Mon & Price[i] for i=0 to n-1.
int i,m,n,j; Step 4: Read Type of Vehicle Say Vehicle.
Step 5: Read Starting & Ending Month Say StMon & EnMon.
clrscr(); Step 6: For i=0 to n-1 repeat Step 7
Step 7: Check (strcmp(Veh[i],Vehicle)==0) & St_Mon[i]>=StMon & En_Mon[i]<=EnMon then
printf("Enter A String:--\n"); go to Step 8 otherwise go to Step 9
scanf("%[^\n]s",Str1); Step 8: Display Vehicle Type Vehicle is Sold During Period StMon / EnMon
Step 9: Display Vehicle Type Vehicle is Not Sold During Period StMon / EnMon
printf("\nEnter Number of Characters Which U Wnat to Extract-->\n");
scanf("%d",&m); Flowchart:--
A
START
printf("\nEnter Beginnig Index from Which U Want to Extract-->\n");
scanf("%d",&n);
i=0
printf("\nExtracted String is:--\n\n");
Read n, Veh[i], St_Mon[i],
for(i=n-1;i<m+n-1;i++) En_Mon[i] & Price[i] for
{ i=0 to n-1
printf("%c",Str1[i]); i<=n-1 NO
}

getch();
} Display Veh[i], YES
St_Mon[i], En_Mon &
Output:-- Price[i] for i=0 to n-1
NO (strcmp(Veh[i],Vehicle)
Enter A String:-- ==0) &
Ritesh Kumar Jain St_Mon[i]>=StMon &
Enter Number of Characters Which U Wnat to Extract--> En_Mon[i]<=EnMon
6 Read Vehicle,
Enter Beginnig Index from Which U Want to Extract--> StMon & EnMon
4
Extracted String is:-- YES
esh Ku
Display Vehicle
8.7 A Maruti car dealer maintains a reecord of sales of various vehicles in the following Type Vehicle is Sold
A During Period StMon /
form:
EnMon
Vehicle Type Month of sales Price
MARUTI-800 02/01 210000
MARUTI-DX 07/01 265000
GYPSY 04/02 315750 Display Vehicle
MARUTI-VAN 08/02 240000 Type Vehicle is Not
Sold During Period
Write a program to read this data into a table of strings and output the details of a StMon / EnMon
particular vehicle sold during a specified period. The program should request the user to
input the vehicle type and the period (starting month, ending month).

Algorithm:--
END
Step 1: Read Entries We Want to Enter Say n
Step 2: Read Vehcle Type, Starting Month, Ending Month & Price for i=1 to n-1

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:--
printf("Enter The Type of Vehicle\n");
//A Maruti car dealer maintains a reecord of sales of various vehicles in the following scanf("%s",Vehicle);
// form:
printf("Enter the Starting & Ending Month\n");
// Vehicle Type Month of sales Price scanf("%d %d",&StMon,&EnMon);
// MARUTI-800 02/01 210000
// MARUTI-DX 07/01 265000 for(i=0;i<n;i++)
// GYPSY 04/02 315750 {
// MARUTI-VAN 08/02 240000 if((strcmp(Veh[i],Vehicle)==0))
{
// Write a program to read this data into a table of strings and output the details of a if(St_Mon[i]>=StMon)
// particular vehicle sold during a specified period. The program should request the user to {
// input the vehicle type and the period (starting month, ending month). if(En_Mon[i]<=EnMon)
{
//Date: 18/03/2010 printf("Vehicle Type %s is Sold During Period
0%d/0%d\n",Vehicle,StMon,EnMon);
#include<stdio.h> getch();
#include<conio.h> exit(0);
}
#define MAX 10 }
}
void main() }
{
printf("Vehicle Type %s Not Sold During Period 0%d/0%d\n",Vehicle,StMon,EnMon);
char Veh[MAX][MAX]={""};
char Vehicle[MAX]; getch();
int St_Mon[MAX],En_Mon[MAX],StMon,EnMon; }
long int Price[MAX];
int n,i; Output:--

clrscr(); How Entries U Want to Enter 4


Enter Vehcle Type,Starting Month, Ending Month & Price:--
printf("How Entries U Want to Enter\n"); MARUTI-800 02/01 210000
scanf("%d",&n); MARUTI-DX 07/01 265000
GYPSY 04/02 315750
printf("Enter Vehcle Type,Starting Month, Ending Month & Price:--\n"); MARUTI-VAN 08/02 240000

for(i=0;i<n;i++)
{ Vehicle Type Month of sales Price
scanf("%s",Veh[i]); MARUTI-800 02/01 210000
scanf("%d",&St_Mon[i]); MARUTI-DX 07/01 265000
scanf("%d",&En_Mon[i]); GYPSY 04/02 315750
scanf("%ld",&Price[i]); MARUTI-VAN 08/02 240000
}
Enter The Type of Vehicle MARUTI-800
clrscr(); Enter the Starting & Ending Month 02 01

printf("Vehicle Type Month of Sales Price\n"); Vehicle Type MARUTI-800 is Sold During Period 02/01
for(i=0;i<n;i++)
{
printf("%s 0%d / 0%d %ld\n",Veh[i],St_Mon[i],En_Mon[i],Price[i]);
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

8.9 Write a program that reads the cost of an item in the form RRRR.PP (where RRRR Program:--
denotes Rupees and PP denotes Paise) and converts the value to a string of words that
express the numeric value in words. For example, if we input 125.75 the ouput should be //Write a program that reads the cost of an item in the form RRRR.PP (where RRRR
“ONE HUNDRED TWENTY FIVE AND PAISE SEVENTY FIVE”. // denotes Rupees and PP denotes Paise) and converts the value to a string of words that
// express the numeric value in words. For example, if we input 125.75 the ouput should be
Algorithm:-- // “ONE HUNDRED TWENTY FIVE AND PAISE SEVENTY FIVE”.

Step 1: Read Cost. //Date: 18/03/2010


Step 2: Compute Rup = Cost, Pai = (Cost - Rup)*100 and i=Rup/100.
Step 3: Check i=1 Display “ONE HUNDRED”………….i=9 Display “NINE HUNDRED” #include<stdio.h>
Step 4: Compute i=Rup%100, R=i/10, Re=(float)i/10. #include<conio.h>
Step 5: Check R=1 Display “TEN”………….i=9 Display “NINETY”
Step 6: Compute R=(Re-R)*10. void main()
Step 7: Check R=1 Display “ONE”………..R=9 Display “NINE” & Display “AND PAISE” {
Step 8: Compute i=Pai/10, Re=(float)Pai/10, R=(Re-i)*10
Step 9: Check i=1 Display “TEN”………….i=9 Display “NINETY” float Cost,Pai,Re,j;
Step 10: Check R=1 Display “ONE”………..R=9 Display “NINE” int Rup,i,R;

Flowchart:-- clrscr();

START ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE printf("\nEnter Cost of an ITEM-->\n");
HUNDRED HU HU HUN HU HUN HUN HUN HUN
scanf("%f",&Cost);
1 2 3 4 5 6 7 8 9

Rup = Cost;
Rup = Cost, Pai = Pai = (Cost - Rup)*100;
(Cost - Rup)*100 i
and i=Rup/100 i=Rup/100;
TEN TWENT THIRTY FOURT FIFTY SIXTY SEVEN EIGHTY NINETY
Y Y TY switch(i)
{
1 2 3 4 5 6 7 8 9
i=Rup%100, case 1: printf("ONE HUNDRED "); break;
R=i/10, case 2: printf("TWO HUNDRED "); break;
Re=(float)i/10 R case 3: printf("THREE HUNDRED "); break;
case 4: printf("FOUR HUNDRED "); break;
case 5: printf("FIVE HUNDRED "); break;
ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE
case 6: printf("SIX HUNDRED "); break;
case 7: printf("SEVEN HUNDRED "); break;
1 2 3 4 5 6 7 8 9 case 8: printf("EIGHT HUNDRED "); break;
case 9: printf("NINE HUNDRED "); break;
R=(Re-R)*10 }
R
AND PAISE i=Rup%100;
R=i/10;
TEN TWENT THIRTY FOURT FIFTY SIXTY SEVEN EIGHTY NINETY
Y Y TY Re=(float)i/10;
1 2 3 4 5 6 7 8 9
i=Rup%100, switch(R)
R=i/10, {
i case 1: printf("TEN"); break;
Re=(float)i/10
case 2: printf("TWENTY "); break;
case 3: printf("THIRTY "); break;
ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE case 4: printf("FOURTY "); break;
case 5: printf("FIFTY "); break;
1 2 3 4 5 6 7 8 9
case 6: printf("SIXTY "); break;

R
END
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

case 7: printf("SEVENTY "); break;


case 8: printf("EIGHTY "); break; Output:--
case 9: printf("NINETY "); break;
} Enter Cost of an ITEM-->
125.25
R=(Re-R)*10;
ONE HUNDRED TWENTY FIVE AND PAISE TWENTY FIVE
switch(R)
{ 8.10 Develop a program that will read and store the details of a list of students in the format
case 1: printf("ONE"); break;
case 2: printf("TWO "); break; Roll No. Name Marks Obtained
case 3: printf("THREE "); break; …………. ……… ………………..
case 4: printf("FOUR "); break; …………. ……… ………………..
case 5: printf("FIVE "); break;
case 6: printf("SIX "); break; And produce the following output lists:
case 7: printf("SEVEN "); break; a) Alphabetical list of names, roll numbers and marks obtained.
case 8: printf("EIGHT "); break; b) List sorted on roll numbers.
case 9: printf("NINE "); break; c) List sorted on marks (rank-wise list)
}
Algorithm:--
printf("AND PAISE ");
Step 1: Read n
i=Pai/10; Step 2: Read Roll_No[i], Stu_Name[i] & Marks[i] for i=0 to n-1.
Re=(float)Pai/10; Step 3: Display Roll_No[i], Stu_Name[i], Marks[i] for i=0 to n-1.
R=(Re-i)*10; Step 4: Sort list using Bubble sort according to Alphabetical list.
Step 5: Sort list using Bubble sort according to Roll numbers.
switch(i) Step 6: Sort list using Bubble sort according to Marks.
{ A
case 1: printf("TEN"); break; Flowchart:--
case 2: printf("TWENTY "); break;
case 3: printf("THIRTY "); break; START i<n
case 4: printf("FOURTY "); break;
case 5: printf("FIFTY "); break;
case 6: printf("SIXTY "); break; j=0
case 7: printf("SEVENTY "); break; Read Roll_No[i],
case 8: printf("EIGHTY "); break; Stu_Name[i] &
Marks[i] for i=0 j<n
case 9: printf("NINETY "); break; V
} to n-1
strcmp(Stu_Name[j],St
switch(R) u_Name[j+1])>0
{
case 1: printf("ONE"); break; Display Roll_No[i],
case 2: printf("TWO "); break; Stu_Name[i], strcpy(Temp,Stu_Name[j]); strcpy(Stu_Name[j],Stu_Name[j+1]);
case 3: printf("THREE "); break; Marks[i] for i=0 to n- strcpy(Stu_Name[j+1],Temp);Temp1=Roll_No[j];
case 4: printf("FOUR "); break; 1 Roll_No[j]=Roll_No[j+1]; Roll_No[j+1]=Temp1; Temp2=Marks[j];
Marks[j]=Marks[j+1]; Marks[j+1]=Temp2;
case 5: printf("FIVE "); break;
case 6: printf("SIX "); break;
case 7: printf("SEVEN "); break; i=0
j=j+1
case 8: printf("EIGHT "); break;
case 9: printf("NINE "); break;
}
getch(); A i=i+1
}
Display Roll_No[i],
B Stu_Name[i], Marks[i]
Downloaded by Pragnya shri Prakhya ([email protected])
for i=0 to n-1
Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

i=0

B
i<n

i=0 j=0

j<n
i<n

j=0 Marks[j] <Marks [j+1]

j<n
strcpy(Temp,Stu_Name[j]); strcpy(Stu_Name[j],Stu_Name[j+1]);
strcpy(Stu_Name[j+1],Temp);Temp1=Roll_No[j];
Roll_No[j]>Roll_No[j+1] Roll_No[j]=Roll_No[j+1]; Roll_No[j+1]=Temp1; Temp2=Marks[j];
Marks[j]=Marks[j+1]; Marks[j+1]=Temp2;

strcpy(Temp,Stu_Name[j]); strcpy(Stu_Name[j],Stu_Name[j+1]); j=j+1


strcpy(Stu_Name[j+1],Temp);Temp1=Roll_No[j];
Roll_No[j]=Roll_No[j+1]; Roll_No[j+1]=Temp1; Temp2=Marks[j];
Marks[j]=Marks[j+1]; Marks[j+1]=Temp2;
i=i+1

j=j+1 Display Roll_No[i],


Stu_Name[i], Marks[i]
for i=0 to n-1
i=i+1

Display Roll_No[i], END


Stu_Name[i], Marks[i]
for i=0 to n-1

Program:--
//Date: 18/03/2010
C
#include<stdio.h>
#include<conio.h>

#define MAX 50

void main()
{
char Stu_Name[MAX][MAX]={""};
//char Stu_Name1[MAX][MAX]={""};

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

char Temp[MAX]=""; printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]);


int Roll_No[MAX],Marks[MAX],n,i,In[MAX],Roll_No1[MAX],Marks1[MAX]; }
int Temp1,Temp2;
int j; for(i=0;i<n;i++)
{
clrscr(); for(j=0;j<n-i-1;j++)
{
printf("How Many Student Name U Want to Enter\n\n"); if(Roll_No[j]>Roll_No[j+1])
scanf("%d",&n); {
strcpy(Temp,Stu_Name[j]);
printf("Enter Roll No. & Students Name & Total Marks:--\n"); strcpy(Stu_Name[j],Stu_Name[j+1]);
strcpy(Stu_Name[j+1],Temp);
for(i=0;i<n;i++)
{ Temp1=Roll_No[j];
scanf("%d",&Roll_No[i]); Roll_No[j]=Roll_No[j+1];
scanf("%s",Stu_Name[i]); Roll_No[j+1]=Temp1;
scanf("%d",&Marks[i]);
} Temp2=Marks[j];
Marks[j]=Marks[j+1];
clrscr(); Marks[j+1]=Temp2;
}
printf("Roll No Name Marks\n"); }
for(i=0;i<n;i++) }
{
printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]); printf("\nAccording to Marks:--\n");
} printf("Roll No Name Marks\n");

for(i=0;i<n;i++) for(i=0;i<n;i++)
{ {
for(j=0;j<n-i-1;j++) printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]);
{ }
if(strcmp(Stu_Name[j],Stu_Name[j+1])>0)
{ for(i=0;i<n;i++)
strcpy(Temp,Stu_Name[j]); {
strcpy(Stu_Name[j],Stu_Name[j+1]); for(j=0;j<n-i-1;j++)
strcpy(Stu_Name[j+1],Temp); {
if(Marks[j]<Marks[j+1])
Temp1=Roll_No[j]; {
Roll_No[j]=Roll_No[j+1]; strcpy(Temp,Stu_Name[j]);
Roll_No[j+1]=Temp1; strcpy(Stu_Name[j],Stu_Name[j+1]);
strcpy(Stu_Name[j+1],Temp);
Temp2=Marks[j];
Marks[j]=Marks[j+1]; Temp1=Roll_No[j];
Marks[j+1]=Temp2; Roll_No[j]=Roll_No[j+1];
} Roll_No[j+1]=Temp1;
}
} Temp2=Marks[j];
Marks[j]=Marks[j+1];
printf("\nAccording to Student Names:--\n"); Marks[j+1]=Temp2;
printf("Roll No Name Marks\n"); }
}
for(i=0;i<n;i++) }
{

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

printf("\nAccording to Roll No:--\n"); Program:--


printf("Roll No Name Marks\n");
//Write a program to read to strings and compare them using the function strcmp() and
for(i=0;i<n;i++) // print a mesaage that the first string is equal, less or greater than the second one.
{
printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]);
} //Date: 18/03/2010

getch(); #include<stdio.h>
} #include<conio.h>

#define MAX 50
8.11 Write a program to read to strings and compare them using the function strcmp() and
print a mesaage that the first string is equal, less or greater than the second one. void main()
{
Algorithm:-- char Str1[MAX],Str2[MAX];

Step 1: Read two strings Say Str1 & Str2. clrscr();


Step 2: Check strcmp(Str1,Str2)=0 true then Display “Both strings are equal” otherwise go to
Step 3. printf("Enter First String:--\n");
Step 3: Check strcmp(Str1,Str2)<0 then Display “First string is less than Second” otherwise go to scanf("%[^\n]s",Str1);
Step 4.
Step 4: Display “First string is greater than Second”. fflush(stdin);

Flowchart:-- printf("Enter Second String:--\n");


START scanf("%[^\n]s",Str2);

if(strcmp(Str1,Str2)==0)
Read two printf("\nBoth Strings are Equal\n");
strings Say Str1 else if(strcmp(Str1,Str2)<0)
& Str2 printf("\nFirst String is Less Than\n");
else
printf("\nFirst String is Greater Than\n");
getch();
YES }
strcmp(Str1, Display “Both
Str2)=0 strings are equal”
Output:--

NO Enter First String:--


Ritesh
YES Enter Second String:--
Display “First
strcmp(Str1, Jain
string is less than
Str2)<0 First String is Greater Than
Second”
8.12 Write a program to read a line of text from the keyboard and print out the number of
occurrences of a given substring using the function strstr().
NO
Algorithm:--
Display “First
string is less than Step 1: Read text Str1 & substring Str2, Count=0.
Second” Step 2: Compute Len=strlen(Str1), Len1=strlen(Str2).
END Step 3: Copy Str1 to Str3.
Step 4: For i=0 to Len repeat Step 5 to Step 6 otherwise go to Step 8

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Step 5: Check (Str3=strstr(Str3,Str2))!=NULL then go to Step 6 otherwise go to Step 7 Program:--


Step 6: Compute i=i+Len1, Count=Count+1.
Step 7: Copy Str3+Len1 to Str3. //Write a program to read a line of text from the keyboard and print out the number of
Step 8: Display Count. // occurrences of a given substring using the function strstr().

Flowchart:--
START //Date: 18/03/2010

#include<stdio.h>
#include<conio.h>
Read text Str1
substring Str2 #define MAX 50
Count=0
void main()
{
Len=strlen(Str1), char *Str1,*Str2,*Str3;
Len1=strlen(Str2) int i,Len,Len1,Count;
Copy Str1 to Str3
i=0 clrscr();

Count=0;
NO
i<Len printf("Enter Text:--\n");
scanf("%[^\n]s",Str1);

YES fflush(stdin);

printf("Enter Substring:--\n");
(Str3=strstr(Str3, NO
scanf("%[^\n]s",Str2);
Str2))!=NULL
Len=strlen(Str1);
Len1=strlen(Str2);
YES
strcpy(Str3,Str1);
i=i+Len1, Count=Count+1
for(i=0;i<Len;i++)
{
Copy Str3+Len1 to Str3, i=i+1 if((Str3=strstr(Str3,Str2))!=NULL)
{
i=i+Len1;
Count=Count+1;
}
Display Count
strcpy(Str3,(Str3+Len1));
}

printf("\n\nNumber of occurences is:-- %d",Count);


END getch();
}

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

8.13 Write a program that will copy m consecutive characters from a string s1 beginning at
position n into another string s2. Program:--

Algorithm:-- //Write a program that will copy m consecutive characters from a string s1 beginning at
// position n into another string s2.
Step 1: Read String Str1 & Value of m (Number of Characters Which U Wnat to Copy) &
n (Beginnig Index from Which U Want to Copy).
Step 2: Compute i=n-1 & j=0. //Date: 18/03/2010
Step 3: For i=n-1 to m+n repeat Step 4.
Step 4: Compute Str2[j]=Str1[i]. #include<stdio.h>
Step 5: Compute Str2[m]='\0'. #include<conio.h>
Step 6: Display Str2.
#define MAX 50
Flowchart:--
START void main()
{
char Str1[MAX],Str2[MAX];
Read String int i,m,n,j;
Str1, m & n
clrscr();

printf("Enter A String:--\n");
i=n-1 & j=0 scanf("%[^\n]s",Str1);

printf("\nEnter Number of Characters Which U Wnat to Copy-->\n");


scanf("%d",&m);
NO
i<m+n
printf("\nEnter Beginnig Index from Which U Want to Copy-->\n");
scanf("%d",&n);
YES
for(i=n-1,j=0;i<m+n;i++,j++)
Str2[j]=Str1[i] {
Str2[j]=Str1[i];
}

i=i+1, j=j+1 Str2[m]='\0';

printf("\n\nCopied String is--> %s \n\n",Str2);


getch();
Str2[m]='\0' }

Output:--

Enter A String:--
Display Str2 Ritesh Kumar Jain
Enter Number of Characters Which U Wnat to Copy-->
4
Enter Beginnig Index from Which U Want to Copy-->
END 8
Copied String is-->
esh K

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

8.14 Write a program to create a directory of students with roll numbers.The program
should display the roll number for a specified name and vice-versa. Program:--
// Write a program to create a directory of students with roll numbers.The program should
Algorithm:-- //display the roll number for a specified name and vice-versa.

Step 1: Read n (Number of Student Name U Want to Enter). #include<stdio.h>


Step 2: Read Roll_No[i] & Stu_Name[i] for i=0 to n. #include<conio.h>
Step 3: Read Roll (Student Roll No which U want to Search).
Step 4: For i=0 to n repeat Step 5 to Step 6 #define MAX 50
Step 5: Check Roll==Roll_No[i] then go to Step 6 void main()
Step 6: Compute Index=i {
Step 7: Display Index. char Stu_Name[MAX][MAX],Name[MAX];
Step 8: Read Name (Student Name which U want to Search). int Roll_No[MAX],n,i,Roll,Index;
Step 9: For i=0 to n repeat Step 5 to Step 6
Step 10: Check strcmp(Stu_Name[i],Name)==0 then go to Step 6 clrscr();
Step 11: Compute Index=i
Step 12: Display Index. printf("How Many Student Name U Want to Enter\n\n");
scanf("%d",&n);
Flowchart:--
printf("Enter Roll No. & Students Name:--\n");
START for(i=0;i<n;i++)
{
A scanf("%d",&Roll_No[i]);
scanf("%s",Stu_Name[i]);
Read n,
}
Roll_No[i] &
Stu_Name[i] for Read Name printf("\nEnter Student Roll No which U want to Search:--\n");
i=0 to n
scanf("%d",&Roll);

for(i=0;i<n;i++)
i=0
Read Roll {
if(Roll==Roll_No[i])
{
NO Index=i;
i=0 i<n }
}
YES
NO printf("\nName of Student is --> %s whose Roll No is:--%d",Stu_Name[Index],Roll);
i<n strcmp(Stu_Nam
e[i],Name)==0
printf("\n\nEnter Student Name which U want to Search:--\n");
scanf("%s",Name);
YES NO
YES
for(i=0;i<n;i++)
Roll==Roll_ Index=i {
No[i] if(strcmp(Stu_Name[i],Name)==0)
NO {
YES Display Index Index=i;
}
Index=i
}
END
Display Index printf("\n\nRoll No of is:-- %d Student Whose Name is:--
%s\n",Roll_No[Index],Stu_Name[Index]);

A
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

getch(); Flowchart:-- START


}

Output:--
Str= “123456789”
How Many Student Name U Want to Enter i=0
3
Enter Roll No. & Students Name:-- NO
2 Ritesh i<5
7 Amit
YES
12 Pooja
Enter Student Roll No which U want to Search:-- k=4
7
Name of Student is --> Amit whose Roll No is:--7 NO
Enter Student Name which U want to Search:-- k>i
Pooja YES
Roll No of is:-- 12 Student Whose Name is:-- Pooja
Display “ ” (Space)

8.15 Given a string k = k-1

char str[ ] =”123456789”;


l=i
Write a program that displays the following: j=0
1
232 NO
34543
j <= i
4567654
567898765 YES

Display Str[l]

Algorithm:-- l=l+1
j=j+1
Step 1: Store “123456789” to Str.
Step 2: For i=0 to 5 repeat Step 3 to Step to Step 13
Step 3: For k=4 to i repeat Step 4 l=l–2
Step 4: Display “ ” (Space). k=0
Step 5: Store i to l.
Step 6: For j=0 to i repeat Step 7 to Step 8 NO
Step 7: Display Str[l] k<i
Step 8: Compute l=l+1
Step 9: Compute l=l-2 YES
Step 10: For k=0 to i repeat Step 11 to Step 12
Step 11: Display Str[l] Display Str[l]
Step 12: l=l-1.
Step 13: Display “\n”
k=k+1
l=l-1

i = i+1

Downloaded by Pragnya shri Prakhya ([email protected]) END shri Prakhya ([email protected])


Downloaded by Pragnya
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- CHAPTER 9 19/03/2010


9.3 Use recursive function calls to evaluate
//Given a string
// char str[ ] =”123456789”; F(x) = x – x3/3! + x5/5! – x7/7! + ………………….
//Write a program that displays the following:
// 1 Algorithm:--
// 232
// 3 4 5 4 3 Step 1: Read a Number No.
// 4 5 6 7 6 5 4 Step 2: Call function f = Fx(No).
//5 6 7 8 9 8 7 6 5 Step 3: Display f.

Fx(No)
#include<stdio.h> Step 1: Store 1 to i & j.
#include<conio.h> Step 2: For i<=5 repeat Step 3 to Step 6
Step 3: Check j%2!=0 then go to Step 4 otherwise go to Step 5
void main() Step 4: Compute Temp1 = pow(x,i), Temp2 = Fact(i), Result += (float) Temp1/Temp2.
{ Step 5: Compute Temp1 = pow(x,i), Temp2 = Fact(i), Result -= (float) Temp1/Temp2.
char Str[]="123456789"; Step 6: Compute j=j+1 & i= i+2
int i,j,k,l; Step 7: Return Result.

clrscr(); Fact(n)
Step 1: Check n==0 then return 0 otherwise go to Step 2.
for(i=0;i<5;i++) Step 2: return n*Fact(n-1).
{
for(k=4;k>i;k--) Flowchart:--
{ Fx C
printf(" ");
} START

i=j=1 NO
for(j=0,l=i;j<=i;j++,l++) n==0
{ Read a
printf("%c",Str[l]); Number No YES
} NO
l=l-2; i<=5 Return 0
Fx
for(k=0;k<i;k++,l--) f = Fx(No) D
printf("%c",Str[l]); YES
B
NO Return n*Fact(n-1)
Display f j%2!
printf("\n"); =0
} YES
getch();
Temp1 = pow(x,i);
END Temp2 = Fact(i);
} Result+= (float) Temp1/Temp2;
D C
Output:--
Temp1 = pow(x,i);
1 Temp2 = Fact(i);
232 Result-= (float) Temp1/Temp2;
34543
4567654
567898765 i=i+2, j=j+1

B Return Result

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

scanf("%d",&No);
Program:--
F = Fx(No);
//Use recursive function calls to evaluate
printf("Result--> %f",F);
// F(x) = x – x3/3! + x5/5! – x7/7! + ………………….
getch();
}
//19/03/2010
9.6 Write a function that will round a floating-point number to an indicated decimal place.
#include<stdio.h> For example the number 17.457 would yield the value 17.46 when it is rounded off to
#include<conio.h> two decimal places.
#include<math.h>
Algorithm:--
int Fact(int n)
{ Flowchart:--
if(n==0)
return 1; Program:--
else
return (n*Fact(n-1));
} 9.7 Write a function that returns 1 if its argument is a prime number and returns zero
otherwise.
float Fx(int x)
{ Algorithm:--
float Result=0;
int i,j,Temp1,Temp2; Step 1: Read n.
Step 2: Call C=Prime (n).
for(i=1,j=1;i<=5;i+=2,j++) Step 3: Check C==1 then go to Step 4 otherwise go to Step 5
{ Step 4: Display “Number is Prime”
if(j%2!=0) Step 5: Display “Number is not Prime”
{
Temp1 = pow(x,i); Prime(x)
Temp2 = Fact(i); Step 1: For i=2 to x/2 repeat Step 2
Result += (float) Temp1/Temp2; Step 2: Check x%i == 0 then return 0.
} Step 3: return 1.
else P
{ Flowchart:--
Temp1 = pow(x,i); START
Temp2 = Fact(i); i=2
P
Result -= (float) Temp1/Temp2;
} C=Prime (n)
} R NO
return Result; i<=x/2
} NO
C=1
YES
void main()
YES
{ x%i==0
int No; Display “Number is Prime”
float F;

clrscr(); Return 0
Display “Number is not Prime” R
printf("Enter a Number-->\n");
Return 1
END
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- Algorithm:--

//Write a function that returns 1 if its argument is a prime number and returns zero Step 1: Call Read().
// otherwise. Step 2: Display 1. Area 2. Perimeter 3. Exit
Step 3: Read Ch (1 or 2 or 3)
Step 4: Check Ch
//19/03/2010 Step 5: Ch==1 then call Area().
Step 6: Ch==2 then call Peri().
int prime(int x) Step 7: Ch==3 then Exit.
{
int i; Read()
Step 1: Read a, b & c.
for(i=2;i<=x/2;i++)
{ Area()
if(x%i==0) Step 1: Compute S = (a+b+c)/2.
return 0; Step 2: Compute Area = Sqrt((s-a)(s-b)(s-c))
} Step 3: Display Area.
return 1;
} Peri()
Step 1: Compute P = a+b+c
void main() Step 2: Display P.
{
int n,c; Flowchart:--
A
clrscr(); START
C
printf("Enter a Number-->\n");
scanf("%d",&n); Read a, b & c
Read() A
P = a+b+c
c = prime(n);

if(c==1) 1. Area D
printf("\nNumber %d is Prime",n); 2. Perimeter D F
else 3. Exit
printf("\nNumber %d is Not Prime",n);

getch(); Read Ch B
}

Output:--
Compute S = (a+b+c)/2.
Ch Step 2: Compute Area = Sqrt((s-a)(s-b)(s-c))
Enter a Number-->
5 Ch=1
Number 5 is Prime Ch=2
E
9.10 Develop a modular interactive program using functions that reads the values of three
sides of a triangle and displays either its area or its perimeter as per the request of the Area() Peri() EXIT
user. Given the three sides a, b and c.

Perimeter = a+b+c C
B
Area = Sqrt((s-a)(s-b)(s-c))
Where s = (a+b+c)/2
F
E

END
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:-- printf("1. Area \n2. Perimeter \n3. Exit\n");


printf("Enter UR Choice\n");
//Develop a modular interactive program using functions that reads the values of three scanf("%d",&ch);
//sides of a triangle and displays either its area or its perimeter as per the request of the
//user. Given the three sides a, b and c. switch(ch)
{
//Perimeter = a+b+c case 1:
//Area = Sqrt((s-a)(s-b)(s-c)) Area();
//Where s = (a+b+c)/2 break;
case 2:
Peri();
#include<math.h> break;
default:
int a,b,c; exit(0);
}
void Read() getch();
{ }
printf("Enter three sides of Triangle-->\n"); }
scanf("%d %d %d",&a,&b,&c);
} Output:--

void Area() Enter three sides of Triangle-->


{ 234
double S,Area,Temp; 1. Area
2. Perimeter
S=(double) (a+b+c)/2; 3. Exit
Enter UR Choice 2
Area=sqrt((S-a)*(S-b)*(S-c)); Perimeter of Triangle:-->
9
printf("Area of Triangle:--> %lf",Area);
}

void Peri() 9.11 Write a function that can be called to find the largest element of an m by n matrix.
{
int P; Algorithm:--

P=a+b+c; Step 1: Read m(Number of Rows) & n(Number of Columns).


Step 2: Read Elements of matrix A[i][j] for i=0 to m-1 & j=0 to n-1.
printf("Perimeter of Triangle:--> %d",P); Step 3: Display matrix A.
} Step 4: Call L= Largest(A,m,n).
Step 5: Display L.
void main()
{ Largest(a,m,n)
int ch; Step 1: Store a[0][0] to Large.
Step 2: For i=0 to m repeat Step 3 to Step 5
clrscr(); Step 3: For j=0 to n repeat Step 4 to Step 5
Step 4: Check Large<a[i][j] then go to Step 5
Read(); Step 5: Compute Large=a[i][j].
Step 6: return Large.
while(1)
{
clrscr();

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

int Largest(int a[][MAX],int m,int n)


Flowchart:-- {
int Large,i,j;
A
START Large=a[0][0];

for(i=0;i<m;i++)
Large=a[0][0] {
Read i=0 for(j=0;j<n;j++)
m(Number of Rows) & j=0 {
n(Number of Columns). if(Large<a[i][j])
Read Elements of matrix Large=a[i][j];
A[i][j] for i=0 to m-1 & j=0 NO }
to n-1 i<m }

YES return Large;


}
Display matrix A NO
j<n
void main()
{
A YES int A[MAX][MAX];
L= Largest(A,m,n) NO int L,m,n,i,j;
Large<a[i][j]
B clrscr();

Display matrix A YES printf("Enter Number of Rows\n");


scanf("%d",&m);
Large=a[i][j]
printf("Enter Number of Columns\n");
scanf("%d",&n);
END
j=j+1 printf("Enter Elements of Matrix:--\n");

for(i=0;i<m;i++)
{
i=i+1 for(j=0;j<n;j++)
{
scanf("%d",&A[i][j]);
}
return Large }

clrscr();
B printf("Matrix is:--\n");
Program:--
for(i=0;i<m;i++)
//Write a function that can be called to find the largest element of an m by n matrix. {
// Date : 19/03/2010 for(j=0;j<n;j++)
{
#include<stdio.h> printf("%d ",A[i][j]);
#include<conio.h> }
#define MAX 10 }

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Flowchart:--
L = Largest(A,m,n); A

printf("\nLargest Element :-- %d",L);


START i=0
getch();
}
Read m & n NO
Output:-- Read m1 & n1 i<m

Enter Number of Rows YES


2
Enter Number of Columns m==n1 j=0
3
Enter Elements of Matrix:--
267 NO
825 Read First Matrix A j<n
Matrix is:-- Read Second Matrix B
267
YES
825
Largest Element:-- c[i][ j]=0
8 Display A & B

9.12 Write a function that can be called to compute the product of two matrices of size m
by n and n by m. The main function provides the values for m and n and two k=0
Call A
matrices.
Multiplication(a,b,m,n1)
NO
Algorithm:--
k<m
Step 1: Read m & n (Number of Rows & Columns First Matrix). Display “Multiplication is
Step 2: Read m1 & n1 (Number of Rows & Columns of Second Matrix). not applicable”. YES
Step 3: Check m==n1 if true than go to Step 4 otherwise go to Step 8.
c[i][j]=c[i][j]+(a[i][k]*b[k][j])
Step 4: Read First Matrix A.
Step 5: Read Second Matrix B. B
END
Step 6: Display A & B.
k=k+1
Step 7: Call Multiplication(a,b,m,n1).
Step 8: Display “Multiplication is not applicable”.

Multiplication(a,b,m,n1) j=j+1
Step 1: For i=0 to m repeat Step 2 to Step 5
Step 2: For j=0 to n1 repeat Step 3 to Step 5
Step 3: Compute c[i][j]=0. i=i+1
Step 4: For k=0 to m repeat Step 5
Step 5: Compute c[i][j]=c[i][j]+(a[i][k]*b[k][j]).
Step 6: Display Matrix c.
B Display Matrix c

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335

Program:--
if(m==n1)
//Write a function that can be called to compute the product of two matrices of size m {
//by n and n by m. The main function provides the values for m and n and two printf("Enter Elements of First Matrix:--\n");
//matrices.
for(i=0;i<m;i++)
// Date : 19/03/2010 {
for(j=0;j<n;j++)
#include<stdio.h> {
#include<conio.h> scanf("%d",&A[i][j]);
#define MAX 10 }
}
void Multiplication(int a[][MAX],int b[][MAX],int m,int n1)
{ printf("Enter Elements of Second Matrix:--\n");
int c[MAX][MAX],i,j,k;
for(i=0;i<m1;i++)
for(i=0;i<m;i++) {
{ for(j=0;j<n1;j++)
for(j=0;j<n1;j++) {
{ scanf("%d",&B[i][j]);
c[i][j]=0; }
for(k=0;k<m;k++) }
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
clrscr();
}
} printf("First Matrix is:--\n");

printf("Multiplication of Matrices is:--\n"); for(i=0;i<m;i++)


{
for(i=0;i<m;i++) for(j=0;j<n;j++)
{ {
for(j=0;j<n1;j++) printf("%d ",A[i][j]);
{ }
printf("%d ",c[i][j]); printf("\n");
} }
printf("\n");
} printf("Second Matrix is:--\n");

for(i=0;i<m1;i++)
} {
for(j=0;j<n1;j++)
void main() {
{ printf("%d ",B[i][j]);
int A[MAX][MAX],B[MAX][MAX]; }
int L,m,n,m1,n1,i,j; printf("\n");
}
clrscr();
Multiplication(A,B,m);
printf("Enter Number of Rows & Columns First Matrix\n"); }
scanf("%d %d",&m,&n); else
printf("Multiplication is not applicable\n");
printf("Enter Number of Rows & Columns of Second Matrix\n");
scanf("%d %d",&m1,&n1); getch();

Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335

Output:--

Enter Number of Rows & Columns First Matrix


23
Enter Number of Rows & Columns of Second Matrix
32
Enter Elements of First Matrix:--
111
111
Enter Elements of Second Matrix:--
11
11
11
First Matrix is:--
111
111
Second Matrix is:--
11
11
11
Multiplication of Matrices is:--
22
22

Downloaded by Pragnya shri Prakhya ([email protected])

You might also like