Progrmming in Ansi C Structured Programming Language by e Balagurusamy Solutions With Flowchart Program
Progrmming in Ansi C Structured Programming Language by e Balagurusamy Solutions With Flowchart Program
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.
START
1.2 Modify the above program to provide border lines to the address.
Algorithm: -
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: -
#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
#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:-- }
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:--
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
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)
Algorithm:--
END
Algorithm to compute the value of x.
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
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
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;
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>
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:--
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:--
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.
//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>
clrscr(); START
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;
START Algorithm:--
//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=
Program:--
Flowchart:--
START
//Write a program to display the equation of a line in the form
//ax+by=c
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
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:--
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:--
// 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
n=6; #include<stdio.h>
#include<conio.h>
sum=1+1/2+1/3+1/4+1/5+1/n;
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:--
Paise=Price*100
Algorithm:--
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()
{
clrscr();
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.
Program:--
//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:--
//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;
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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");
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>
printf("Values Are:--\n");
printf("x= %d , y= %d , z=%d",x,y,z); END
getch();
} Program:--
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
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:--
Enter
value x Algorithm:--
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:--
#include<stdio.h>
#include<conio.h>
void main()
{
float Area;
int Rad;
clrscr();
printf("Enter Radius\n");
scanf("%d",&x);
Area=PIE*Rad*Rad;
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
Display x
Display y
Display z
Algorithm:--
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:--
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
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);
Area= Len*Wid
3.5 Given an integer number, write a program that displays the number as follows:
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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
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:--
clrscr();
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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:
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:--
int SmallNo,LargeNo;
float RealNo; START
clrscr();
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");
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
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:--
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
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();
EOQ=sqrt((2*Dr*Sc)/Hc);
Display Freq
TBO=sqrt((2*Sc)/(Dr*Hc));
Output:--
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))
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
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();
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:--
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)
//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.
void main()
{
int m,n,x;
clrscr();
x=m%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();
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);
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:--
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”.
}
//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
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;
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:-- {
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:--
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:-- //* * * *
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)
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:
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");
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:
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;
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);
Output:--
/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();
}
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)
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:--
getch(); Str1=”ANIL”
} Str2=”KUMAR”
Str3=”GUPTA”
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.
void main()
{
char Name1[10],Name2[10];
int Code1,Code2;
Algorithm:-- float Price1,Price2;
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
Flowchart:--
Start
Read x
NO YES
x%2
==0
End
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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:--
Output:-- Algorithm:--
//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;
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();
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>
clrscr();
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
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:--
//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
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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();
Output:--
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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
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.
#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
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:-- {
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();
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>
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
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;
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
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.
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
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
//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;
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.
default:printf("\nWrong Input\n");
Temp=Num &
} RevNum=0
printf("Value:--- %lf\n",Val);
getch(); NO
} Temp
! =0
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
}
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();
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:--
6.3 Write a program to compute the sum of the digits of a given number. clrscr();
Temp=Num & }
Sum=0
Output:--
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
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:--
Program:-- 6.5 Rewrite the program of the Example using the for statement.
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 }
NO getch();
Count
<=n }
YES Output:--
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.
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.
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
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
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);
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);
Flowchart:-- START }
(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:--
END
Program:--
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:
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])
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();
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
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()
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
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]);
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: }
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
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);
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.
#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();
for(i=0;i<row;i++)
c[i][j] = 0 for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
for(i=0;i<row;i++)
k<col for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
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 }
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
}
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
Display A
Algorithm:--
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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
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();
i=0 Algorithm:--
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);
for(i=0;i<=Len;i++)
NO if(CheckChar==Str[i])
i<=Len Count=Count+1;
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:--
// 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
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;
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);
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");
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
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
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:--
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”.
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
i=0
B
i<n
i=0 j=0
j<n
i<n
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;
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
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
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];
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:--
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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));
}
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);
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.
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
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)
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
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
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:--
Downloaded by Pragnya shri Prakhya ([email protected]) Downloaded by Pragnya shri Prakhya ([email protected])
lOMoARcPSD|21254335 lOMoARcPSD|21254335
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 }
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
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");
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:--