Unit 4 - Exercise Solution
Unit 4 - Exercise Solution
Unit 4
CHAPTER 7
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:
Y =mx+c
Algorithm:-
START
n=10
i=0
No
i<10
Yes
i=0
Read v1
No
i<10
i=i+1 Yes
Read v2
i=i+1
total_x=total_y=total_xy=
total_x2=0
temp=total_x*total_y i=0
templ=total_x+total_x
m=((n*total_xy)-(temp))/((n*total_x2)-temp1)
c=((total_y)-(m*total_x))/n No
i<10
Yes
Display
Y=mx+c total_x=total_x+x[i]
total_y=total_y+y[i]
total_xy=total_xy+(x[i]*y[i])
total_x2=total_x2+(x[i]*x[i])
END
Program
/* 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:
Y =mx+c
and the values of m and c are given by:
m=((n ∑(xi,yi))- (∑xi)(∑yi) )/( n(∑xi2)-(∑xi)2)
c=1/n(∑yi -m(∑xi))
All summations are from 1 to n. */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,n=10,v1,v2,x[10],y[10];
int total_x,total_y,total_xy,total_x2;
float m,c,temp,temp1;
clrscr();
for(i=0;i<10;i++)
{
scanf("%d",&v1);
x[i]=v1;
}
for(i=0;i<10;i++)
{
scanf("%d",&v2);
y[i]=v2;
}
total_x=total_y=total_xy=total_x2=0;
for(i=0;i<10;i++)
{
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]);
}
temp= total_x*total_y;
temp1=total_x*total_x;
m=((n*total_xy)-(temp))/((n*total_x2)-temp1);
c=((total_y)-(m*total_x))/n;
getch();
}
Output
Enter the values for x:
1 2 3 4 5 6 7 8 9 10
Enter the values for y:
1 2 3 4 5 6 7 8 9 10
The equation of the straight line is:
Y=1.00000X+0.000000
7.2 The daily maximum temperature recorded in 10 cities during the month of January
(for all 31 days) have been tabulated as follows:
City
1 2 3 4 5 6 ……………………………10
Day
1
2
3
.
.
.
.
31
Write a program to read the table elements into a two-dimensional array temperature, and
to find the city and day corresponding to
a) the highest temperature
b) the lowest temperature
Algorithm:--
i=0
i<2
START j=0
Read Array
Temp j<2
Display Temp
MaxTemp<
Temp[i][j]
MinTemp=
MaxTemp=Temp [0][0]
&City1=0,City2=0 MaxTemp=Temp[i][j],
City1=j+1
A
MinTemp>
Temp[i][j]
MinTemp=Temp[i][j],
City2=j+1
Display
MaxTemp &
MinTemp
END
Program:--
//Write a program to read the table elements into a two-dimensional array temperature, and to
find the city and day corresponding to
//a) the highest temperature
//b) the lowest temperature
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
int Temp[2][2];
int i,j,City1,City2,MaxTemp,MinTemp;
clrscr();
printf("Enter temperature:--\n\n");
for(i=0;i<2;i++)
{
printf("For City %d ->\n",i);
for(j=0;j<2;j++)
{
printf("For Day %d ->",j);
scanf("%d",&Temp[i][j]);
}
}
clrscr();
for(i=0;i<2;i++)
printf("%d ",i+1);
printf("\n Day\n");
for(i=0;i<2;i++)
{
printf(" %d ",i+1);
for(j=0;j<2;j++)
{
printf(" %d",Temp[i][j]);
}
printf("\n");
}
MinTemp=MaxTemp=Temp[0][0];
City1=0;
City2=0;
for(i=0;i<2;i++)
{
for(j=0;j<2;j++)
{
if(MaxTemp<Temp[i][j])
{
MaxTemp=Temp[i][j];
City1=j+1;
}
if(MinTemp>Temp[i][j])
{
MinTemp=Temp[i][j];
City2=j+1;
}
}
}
getch();
}
7.3 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 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.
Algorithm:--
Start
c1=c2=c3=c4=c5=cou
nt=count_sp=0
c1,c2,c3,
c4,c5
i=1
Read
v c2=c2+
Vote[i]==
2 1
Vote[i]=
v
i=i+
Vote[i]== c3=c3+
1
3 1
Vote[i]== c4=c4+
4 1
i=i+1
END count_sp=count_sp+
1
Program:--
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
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. */
#include<stdio.h>
#include<conio.h>
void main()
{
int i,vote[5],c1=0,c2=0,c3=0,c4=0,c5=0,count=0,count_sp=0,v;
clrscr();
for(i=1;i<=5;i++)
{
if(vote[i]==1)
c1=c1+1;
else
{
if(vote[i]==2)
c2=c2+1;
else
{
if(vote[i]==3)
c3=c3+1;
else
{
if(vote[i]==4)
c4=c4+1;
else
if(vote[i]==5)
c5=c5+1;
}
}
}
}
printf(" votes to candidate1=%d",c1);
printf(" \nvotes to candidate2=%d",c2);
printf("\n votes to candidate3=%d",c3);
printf(" \nvotes to candidate4=%d",c4);
printf(" \nvotes to candidate5=%d",c5);
for(i=1;i<=5;i++)
{
if(vote[i]<=5)
count=count+1;
else
count_sp=count_sp+1;
}
Output
Enter your votes for 5 candidates:
1
3
1
8
2
Votes to Candidate 1: 2
Votes to Candidate 2: 1
Votes to Candidate 3:1
Votes to Candidate 4:0
Votes to Candidate 5:0
The number of valid votes is: 4
The number of spoilt votes is: 1
(b) The highest marks in each subject and the Roll No. of the student who
secured it.
Algorithm:--
Step1. Declare
Step2. For i=0 to i<10, Enter sub1[i]
Step3. For i=0 to i<10, Enter sub2[i]
Step4. For i=0 to i<10, Enter sub3[i]
START
i=0
N
i<10 i=0
Y
N
Enter i<10 i=0
sub1[i]
Y
N Total_sub1=total_sub2
Enter i<10
sub2[i] =total_sub3=0
Y
Enter
sub3[i] i=0
N
i=0 i<10
Y
max1=sub1[0] Y total_sub1=total_sub1+sub1[
max2=sub2[0] i]
i<10
max3=sub3[0] total_sub1=total_sub1+sub1[
i=0
max=total[0] i]
roll=roll1=roll2=roll3 N
total_sub1=total_sub1+sub1[
=0 i]
Student
[i+1] has
total[i]
Y N
i<10
N
Y
max1<sub1[i max1=sub1[i
] ] N max1,roll1
roll1=i+1 max2,roll2
max3,roll3
max,roll
Y
max2<sub2[i max1=sub1[i
] ]
roll1=i+1
Y
max3<sub3[i max1=sub1[i
] STOP
] N
roll1=i+1
Y
max<total[i] max1=sub1[i
] N
roll1=i+1
Program:--
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int i,roll,m1,m2,m3,sub1[MAX],sub2[MAX],sub3[MAX];
int total_sub1,total_sub2,total_sub3,total[MAX];
int max,max1,max2,max3,roll1,roll2,roll3;
clrscr();
for(i=0;i<MAX;i++)
scanf("%d",&sub1[i]);
for(i=0;i<MAX;i++)
scanf("%d",&sub2[i]);
for(i=0;i<MAX;i++)
scanf("%d",&sub3[i]);
total_sub1=total_sub2=total_sub3=0;
for(i=0;i<MAX;i++)
{
total_sub1=total_sub1+sub1[i];
total_sub2=total_sub2+sub2[i];
total_sub3=total_sub3+sub3[i];
total[i]=sub1[i]+sub2[i]+sub3[i];
}
for(i=0;i<MAX;i++)
{
printf("The total marks obtained by the student%d is =%d\n",i+1,total[i]);
}
max1=sub1[0];
max2=sub2[0];
max3=sub3[0];
max=total[0];
roll1=0;
roll2=0;
roll3=0;
roll=0;
for (i=0;i<MAX;i++)
{
if(max1<sub1[i])
{
max1=sub1[i];
roll1=i+1;
}
if(max2<sub2[i])
{
max2=sub2[i];
roll2=i+1;
}
if(max3<sub3[i])
{
max3=sub3[i];
roll3=i+1;
}
if(max<total[i])
{
max=total[i];
roll=i+1;
}
}
getch();
7.6 Given are one dimensional arrays A and B which are sorted in ascending
order. Write a program to merge them into a single sorted array C that contains
every item form array A and B, in ascending order.
Algorithm:--
ax=bx=cx=0
mn=m+n
NO
ax<n && bx<m
YES NO
a[ax]<b[bx]
YES
c[cx]=a[ax]
ax=ax+1
c[cx]=b[bx],
bx=bx+1
cx=cx+1
NO
ax==n
YES
bx<m
YES
c[cx]=b[bx],
bx=bx+1, cx=cx+1
NO
ax<n
YES
c[cx]=a[ax],
ax=ax+1, cx=cx+1
Display C
End
Program:--
// Given are one dimensional arrays A and B which are sorted in ascending
// order. Write a program to merge them into a single sorted array C that contains
// every item form array A and B, in ascending order.
//Date: 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
int a[MAX],b[MAX],c[MAX];
int ax,bx,cx,n,m,mn;
clrscr();
ax=bx=cx=0;
mn=m+n;
getch();
}
7.7 Write a program that will read the values of elements of A and B and produce the
product matrix C.
Algorithm:--
i=0
i<row
j=0
j<col
c[i][j] = 0
k=0
k<col
c[i][j]=c[i][j]+(a[i][k]*b[k][j])
END
Program:--
// Write a program that will read the values of elements of A and B and produce the
// product matrix C.
//Date: 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int a[MAX][MAX],b[MAX][MAX],c[MAX][MAX];
int i,j,k,row,col;
clrscr();
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&a[i][j]);
for(i=0;i<row;i++)
for(j=0;j<col;j++)
scanf("%d",&b[i][j]);
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",a[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",b[i][j]);
printf("\n");
}
for(i=0;i<row;i++)
for(j=0;j<col;j++)
{ c[i][j]=0;
for(k=0;k<col;k++)
c[i][j]=c[i][j]+(a[i][k]*b[k][j]);
}
printf("\nMultiplication is\n");
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf("%d ",c[i][j]);
printf("\n");
}
getch();
}
Algorithm:--
Display A
END
Program:--
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
int A[5][5];
int a,i,k,j;
clrscr();
a=3;
for(i=0;i<=3;i++)
{
for(j=0;j<=a;j++)
{
A[i][j]=+1;
}
a--;
}
j=4;
for(i=0;i<=4;i++)
{
A[i][j]=0;
j--;
}
a=4;
for(i=1;i<=4;i++)
{
for(j=4;j>=a;j--)
{
A[i][j]=-1;
}
a--;
}
printf("Array is:--\n\n");
for(i=0;i<=4;i++)
{
for(j=0;j<=4;j++)
printf("%d ",A[i][j]);
printf("\n");
}
getch();
}
Algorithm:--
Read Array A
k=0
NO
k<=9
YES
i=0
NO
i<=9
YES
Small
>A[i]
YES
A[Loc]=A[k], A[k]=Small
Display A
END
Program:--
#include<stdio.h>
#include<conio.h>
void main()
{
int A[10];
int i,k,Small,Loc;
clrscr();
for(i=0;i<=9;i++)
scanf("%d",&A[i]);
for(k=0;k<=9;k++)
{
Small=A[k];
Loc=k;
for(i=k;i<=9;i++)
if(Small>A[i])
{
Small=A[i];
Loc=i;
}
A[Loc]=A[k];
A[k]=Small;
}
for(i=0;i<=9;i++)
printf("%d ",A[i]);
getch();
}
Algorithm:--
Beg=0, End=9 NO
Mid= (Beg+End)/2 Beg>End
YES
Read a Sorted Array
Str & an Item Display “Item
Not Found”
Display “Item
Item!=Str[ NO Found”
Mid] &&
(Beg<=End)
END
YES
NO
Item<Str[ Beg=Mid+1
Mid]
YES
End=Mid-1 Mid=(Beg+End)/2
Program:--
#include<stdio.h>
#include<conio.h>
void main()
{
int Str[10];
int i,Beg,End,Mid,Item;
clrscr();
Beg=0;
End=9;
Mid=(Beg+End)/2;
for(i=0;i<10;i++)
scanf("%d",&Str[i]);
while((Item!=Str[Mid])&&(Beg<=End))
{
if(Item<Str[Mid])
End=Mid-1;
else
Beg=Mid+1;
Mid=(Beg+End)/2;
}
if(Beg>End)
printf("Item Not Found\n");
else
printf("%d Found At Index %d\n",Item,Mid);
getch();
}
7.11 Write a program that will compute the length of a given character string.
Algorithm:--
i=0
NO
End of
String
YES
Len = Len+1
i = i+1
Display Len
END
Program:--
//Write a program that will compute the length of a given character string.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
char Str[50];
int i,Len;
clrscr();
Len=0;
printf("Enter a String:---\n");
scanf("%[^\n]s",&Str);
for(i=0;Str[i]!='\0';i++)
Len=Len+1;
7.12 Write a program that will count the number occurrences of a specified character in a
given line of text.
Algorithm:--
Length of String
is Len & i=0
NO
i<=Len
YES
CheckChar
==Str[i]
YES
Count=Count+1
i=i+1
Display
Count
END
Program:--
//Write a program that will count the number occurrences of a specified character in a
// given line of text.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
char Str[50],CheckChar;
int i,Count,Len;
clrscr();
Count=0;
printf("Enter a String:---\n");
scanf("%[^\n]s",&Str);
Len=strlen(Str);
fflush(stdin);
printf("Enter a charatcer:--\n");
scanf("%c",&CheckChar);
for(i=0;i<=Len;i++)
if(CheckChar==Str[i])
Count=Count+1;
7.13 Write a program to read a matrix of size m*n and print its transpose.
Algorithm:--
Display A
i=0
NO
i<Col
YES
j=0
NO
j<Row
YES
C[i][j]=A[j][i]
j=j+1
i=i+1
Display C
END
Program:--
//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>
#define MAX 10
void main()
{
int A[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
clrscr();
printf("Enter Matrix:---\n");
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
scanf("%d",&A[i][j]);
clrscr();
printf("Matrix:---\n");
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf("%d ",A[i][j]);
printf("\n");
}
for(i=0;i<Col;i++)
for(j=0;j<Row;j++)
C[i][j]=A[j][i];
printf("Transpose of Matrix:---\n");
for(i=0;i<Col;i++)
{
for(j=0;j<Row;j++)
printf("%d ",C[i][j]);
printf("\n");
}
getch();
}
7.14 Every book published by international publishers should carry an International
Standard Book Number (ISBN). It is a 10 character 4 part number as shown below.
0-07-041183-2
The first part denotes the region, the second represents publisher, the third identifies the
book and the fourth is the check digit. The check digit is computed as follows:
Check digit is the remainder when Sum is divided by 11. Write a program that reads a
given ISBN number and check whether it represents a valid ISBN.
Algorithm:--
Step 1: Read Array ISBN.
Step 2: Compute Sum=Sum+(i*ISBN[i]) for i=0 to 9
Step 3: Compute CheckDig=Sum%11
Step 4: Check CheckDig=ISBN[10] then go to Step 5 Otherwise go to Step 6
Step 5: Display “Valid ISBN”
Step 6: Display “Invalid ISBN”
Flowchart:--
START
Read Array
ISBN [11]
i=0
NO
i<=9
YES
Sum=Sum+(i*ISBN[i])
i=i+1
CheckDig=Sum%11
NO
CheckDig=
Invalid ISBN
=ISBN[10]
YES
//Write a program that reads a given ISBN number and check whether it represents a valid ISBN.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int ISBN[11];
int i,j,Sum,CheckDig;
clrscr();
Sum=0;
for(i=1;i<=10;i++)
scanf("%d",&ISBN[i]);
for(i=1;i<=9;i++)
Sum=Sum+(i*ISBN[i]);
CheckDig=Sum%11;
if(CheckDig==ISBN[10])
printf("\nValid ISBN\n");
else
printf("\nInvalid ISBN\n");
getch();
7.15 Write a program to read two matrices A and B and print the following:
a) A + B and
b) A – B.
Algorithm:--
Read
Row,Col
Read Array A
&B
Compute Addition of
Array A & B & Store in
Array C
Display Array C
END
Program:--
//Write a program to read two matrices A and B and print the following:
//a) A + B and
//b) A – B.
// Date : 16/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
int A[MAX][MAX],B[MAX][MAX],C[MAX][MAX];
int Row,Col,i,j;
clrscr();
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
scanf("%d",&A[i][j]);
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
scanf("%d",&B[i][j]);
clrscr();
printf("First Matrix:---\n");
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf("%d ",A[i][j]);
printf("\n");
}
printf("Second Matrix:---\n");
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf("%d ",B[i][j]);
printf("\n");
}
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
C[i][j]=A[i][j]+B[i][j];
printf("Addition of Matrix:---\n");
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf("%d ",C[i][j]);
printf("\n");
}
for(i=0;i<Row;i++)
for(j=0;j<Col;j++)
C[i][j]=A[i][j]-B[i][j];
printf("Subtration of Matrix:---\n");
for(i=0;i<Row;i++)
{
for(j=0;j<Col;j++)
printf("%d ",C[i][j]);
printf("\n");
}
getch();
}
CHAPTER 8 18/03/2010
8.3 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.
Algorithm:--
Flowchart:-- START
Read String
Str, m & n
i=n-1
NO
i<m+n-1
YES
Display
Str[i]
END
Program:--
//Write a program to extract a portion of a character string and print the extracted string.
//Assume that m characters are extracted, starting with the nth character.
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Str1[MAX];
int i,m,n,j;
clrscr();
printf("Enter A String:--\n");
scanf("%[^\n]s",Str1);
for(i=n-1;i<m+n-1;i++)
{
printf("%c",Str1[i]);
}
getch();
}
Output:--
Enter A String:--
Ritesh Kumar Jain
Enter Number of Characters Which U Wnat to Extract-->
6
Enter Beginnig Index from Which U Want to Extract-->
4
Extracted String is:--
esh Ku
8.7 A Maruti car dealer maintains a reecord of sales of various vehicles in the following
form:
Write a program to read this data into a table of strings and output the details of a
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:--
Flowchart:--
A
START
i=0
Display Vehicle
Type Vehicle is Sold
A During Period StMon /
EnMon
Display Vehicle
Type Vehicle is Not
Sold During Period
StMon / EnMon
END
Program:--
//A Maruti car dealer maintains a reecord of sales of various vehicles in the following
// form:
// Write a program to read this data into a table of strings and output the details of a
// 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).
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 10
void main()
{
char Veh[MAX][MAX]={""};
char Vehicle[MAX];
int St_Mon[MAX],En_Mon[MAX],StMon,EnMon;
long int Price[MAX];
int n,i;
clrscr();
for(i=0;i<n;i++)
{
scanf("%s",Veh[i]);
scanf("%d",&St_Mon[i]);
scanf("%d",&En_Mon[i]);
scanf("%ld",&Price[i]);
}
clrscr();
for(i=0;i<n;i++)
{
if((strcmp(Veh[i],Vehicle)==0))
{
if(St_Mon[i]>=StMon)
{
if(En_Mon[i]<=EnMon)
{
printf("Vehicle Type %s is Sold During Period
0%d/0%d\n",Vehicle,StMon,EnMon);
getch();
exit(0);
}
}
}
}
getch();
}
Output:--
Algorithm:--
Flowchart:--
START ONE
HUNDRED
TWO
HU
THREE
HU
FOUR
HUN
FIVE
HU
SIX
HUN
SEVEN
HUN
EIGHT
HUN
NINE
HUN
1 2 3 4 5 6 7 8 9
1 2 3 4 5 6 7 8 9
i=Rup%100,
R=i/10,
Re=(float)i/10 R
1 2 3 4 5 6 7 8 9
R=(Re-R)*10 R
AND PAISE
1 2 3 4 5 6 7 8 9
i=Rup%100,
R=i/10,
i
Re=(float)i/10
1 2 3 4 5 6 7 8 9
R
END
Program:--
//Write a program that reads the cost of an item in the form RRRR.PP (where RRRR
// 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
// “ONE HUNDRED TWENTY FIVE AND PAISE SEVENTY FIVE”.
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
void main()
{
float Cost,Pai,Re,j;
int Rup,i,R;
clrscr();
Rup = Cost;
Pai = (Cost - Rup)*100;
i=Rup/100;
switch(i)
{
case 1: printf("ONE HUNDRED "); break;
case 2: printf("TWO HUNDRED "); break;
case 3: printf("THREE HUNDRED "); break;
case 4: printf("FOUR HUNDRED "); break;
case 5: printf("FIVE HUNDRED "); break;
case 6: printf("SIX HUNDRED "); break;
case 7: printf("SEVEN HUNDRED "); break;
case 8: printf("EIGHT HUNDRED "); break;
case 9: printf("NINE HUNDRED "); break;
}
i=Rup%100;
R=i/10;
Re=(float)i/10;
switch(R)
{
case 1: printf("TEN"); break;
case 2: printf("TWENTY "); break;
case 3: printf("THIRTY "); break;
case 4: printf("FOURTY "); break;
case 5: printf("FIFTY "); break;
case 6: printf("SIXTY "); break;
case 7: printf("SEVENTY "); break;
case 8: printf("EIGHTY "); break;
case 9: printf("NINETY "); break;
}
R=(Re-R)*10;
switch(R)
{
case 1: printf("ONE"); break;
case 2: printf("TWO "); break;
case 3: printf("THREE "); break;
case 4: printf("FOUR "); break;
case 5: printf("FIVE "); break;
case 6: printf("SIX "); break;
case 7: printf("SEVEN "); break;
case 8: printf("EIGHT "); break;
case 9: printf("NINE "); break;
}
i=Pai/10;
Re=(float)Pai/10;
R=(Re-i)*10;
switch(i)
{
case 1: printf("TEN"); break;
case 2: printf("TWENTY "); break;
case 3: printf("THIRTY "); break;
case 4: printf("FOURTY "); break;
case 5: printf("FIFTY "); break;
case 6: printf("SIXTY "); break;
case 7: printf("SEVENTY "); break;
case 8: printf("EIGHTY "); break;
case 9: printf("NINETY "); break;
}
switch(R)
{
case 1: printf("ONE"); break;
case 2: printf("TWO "); break;
case 3: printf("THREE "); break;
case 4: printf("FOUR "); break;
case 5: printf("FIVE "); break;
case 6: printf("SIX "); break;
case 7: printf("SEVEN "); break;
case 8: printf("EIGHT "); break;
case 9: printf("NINE "); break;
}
getch();
}
Output:--
8.10 Develop a program that will read and store the details of a list of students in the format
Algorithm:--
Step 1: Read n
Step 2: Read Roll_No[i], Stu_Name[i] & Marks[i] for i=0 to n-1.
Step 3: Display Roll_No[i], Stu_Name[i], Marks[i] for i=0 to n-1.
Step 4: Sort list using Bubble sort according to Alphabetical list.
Step 5: Sort list using Bubble sort according to Roll numbers.
Step 6: Sort list using Bubble sort according to Marks.
A
Flowchart:--
START i<n
j=0
Read Roll_No[i],
Stu_Name[i] &
Marks[i] for i=0 j<n
V
to n-1
strcmp(Stu_Name[j],St
u_Name[j+1])>0
Display Roll_No[i],
Stu_Name[i], strcpy(Temp,Stu_Name[j]); strcpy(Stu_Name[j],Stu_Name[j+1]);
Marks[i] for i=0 to n- strcpy(Stu_Name[j+1],Temp);Temp1=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;
i=0
j=j+1
A i=i+1
Display Roll_No[i],
B Stu_Name[i], Marks[i]
for i=0 to n-1
B
i=0
i<n
j=0
j<n
Roll_No[j]>Roll_No[j+1]
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+1]=Temp1; Temp2=Marks[j];
Marks[j]=Marks[j+1]; Marks[j+1]=Temp2;
j=j+1
i=i+1
Display Roll_No[i],
Stu_Name[i], Marks[i]
for i=0 to n-1
C
C
i=0
i<n
j=0
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+1]=Temp1; Temp2=Marks[j];
Marks[j]=Marks[j+1]; Marks[j+1]=Temp2;
j=j+1
i=i+1
Display Roll_No[i],
Stu_Name[i], Marks[i]
for i=0 to n-1
END
Program:--
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Stu_Name[MAX][MAX]={""};
//char Stu_Name1[MAX][MAX]={""};
char Temp[MAX]="";
int Roll_No[MAX],Marks[MAX],n,i,In[MAX],Roll_No1[MAX],Marks1[MAX];
int Temp1,Temp2;
int j;
clrscr();
for(i=0;i<n;i++)
{
scanf("%d",&Roll_No[i]);
scanf("%s",Stu_Name[i]);
scanf("%d",&Marks[i]);
}
clrscr();
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(strcmp(Stu_Name[j],Stu_Name[j+1])>0)
{
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+1]=Temp1;
Temp2=Marks[j];
Marks[j]=Marks[j+1];
Marks[j+1]=Temp2;
}
}
}
for(i=0;i<n;i++)
{
printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(Roll_No[j]>Roll_No[j+1])
{
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+1]=Temp1;
Temp2=Marks[j];
Marks[j]=Marks[j+1];
Marks[j+1]=Temp2;
}
}
}
printf("\nAccording to Marks:--\n");
printf("Roll No Name Marks\n");
for(i=0;i<n;i++)
{
printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]);
}
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(Marks[j]<Marks[j+1])
{
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+1]=Temp1;
Temp2=Marks[j];
Marks[j]=Marks[j+1];
Marks[j+1]=Temp2;
}
}
}
printf("\nAccording to Roll No:--\n");
printf("Roll No Name Marks\n");
for(i=0;i<n;i++)
{
printf("%d %s %d\n",Roll_No[i],Stu_Name[i],Marks[i]);
}
getch();
}
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.
Algorithm:--
Flowchart:--
START
Read two
strings Say Str1
& Str2
YES
strcmp(Str1, Display “Both
Str2)=0 strings are equal”
NO
NO
Display “First
string is less than
Second”
END
Program:--
//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.
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Str1[MAX],Str2[MAX];
clrscr();
fflush(stdin);
if(strcmp(Str1,Str2)==0)
printf("\nBoth Strings are Equal\n");
else if(strcmp(Str1,Str2)<0)
printf("\nFirst String is Less Than\n");
else
printf("\nFirst String is Greater Than\n");
getch();
}
Output:--
8.12 Write a program to read a line of text from the keyboard and print out the number of
occurrences of a given substring using the function strstr().
Algorithm:--
Flowchart:--
START
Len=strlen(Str1),
Len1=strlen(Str2)
Copy Str1 to Str3
i=0
NO
i<Len
YES
(Str3=strstr(Str3, NO
Str2))!=NULL
YES
i=i+Len1, Count=Count+1
Display Count
END
Program:--
//Write a program to read a line of text from the keyboard and print out the number of
// occurrences of a given substring using the function strstr().
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char *Str1,*Str2,*Str3;
int i,Len,Len1,Count;
clrscr();
Count=0;
printf("Enter Text:--\n");
scanf("%[^\n]s",Str1);
fflush(stdin);
printf("Enter Substring:--\n");
scanf("%[^\n]s",Str2);
Len=strlen(Str1);
Len1=strlen(Str2);
strcpy(Str3,Str1);
for(i=0;i<Len;i++)
{
if((Str3=strstr(Str3,Str2))!=NULL)
{
i=i+Len1;
Count=Count+1;
}
strcpy(Str3,(Str3+Len1));
}
Algorithm:--
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.
Step 3: For i=n-1 to m+n repeat Step 4.
Step 4: Compute Str2[j]=Str1[i].
Step 5: Compute Str2[m]='\0'.
Step 6: Display Str2.
Flowchart:--
START
Read String
Str1, m & n
NO
i<m+n
YES
Str2[j]=Str1[i]
i=i+1, j=j+1
Str2[m]='\0'
Display Str2
END
Program:--
//Write a program that will copy m consecutive characters from a string s1 beginning at
// position n into another string s2.
//Date: 18/03/2010
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Str1[MAX],Str2[MAX];
int i,m,n,j;
clrscr();
printf("Enter A String:--\n");
scanf("%[^\n]s",Str1);
for(i=n-1,j=0;i<m+n;i++,j++)
{
Str2[j]=Str1[i];
}
Str2[m]='\0';
Output:--
Enter A String:--
Ritesh Kumar Jain
Enter Number of Characters Which U Wnat to Copy-->
4
Enter Beginnig Index from Which U Want to Copy-->
8
Copied String is-->
esh K
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.
Algorithm:--
Flowchart:--
START
Read n,
Roll_No[i] &
Stu_Name[i] for Read Name
i=0 to n
i=0
Read Roll
NO
i=0 i<n
YES
NO
i<n strcmp(Stu_Nam
e[i],Name)==0
YES NO
YES
Roll==Roll_ Index=i
No[i]
NO
YES Display Index
Index=i
END
Display Index
A
Program:--
// 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.
#include<stdio.h>
#include<conio.h>
#define MAX 50
void main()
{
char Stu_Name[MAX][MAX],Name[MAX];
int Roll_No[MAX],n,i,Roll,Index;
clrscr();
for(i=0;i<n;i++)
{
scanf("%d",&Roll_No[i]);
scanf("%s",Stu_Name[i]);
}
for(i=0;i<n;i++)
{
if(Roll==Roll_No[i])
{
Index=i;
}
}
for(i=0;i<n;i++)
{
if(strcmp(Stu_Name[i],Name)==0)
{
Index=i;
}
}
Output:--
Algorithm:--
Str= “123456789”
i=0
NO
i<5
YES
k=4
NO
k>i
YES
Display “ ” (Space)
k = k-1
l=i
j=0
NO
j <= i
YES
Display Str[l]
l=l+1
j=j+1
l=l–2
k=0
NO
k<i
YES
Display Str[l]
k=k+1
l=l-1
i = i+1
END
Program:--
//Given a string
// char str[ ] =”123456789”;
//Write a program that displays the following:
// 1
// 232
// 3 4 5 4 3
// 4 5 6 7 6 5 4
//5 6 7 8 9 8 7 6 5
#include<stdio.h>
#include<conio.h>
void main()
{
char Str[]="123456789";
int i,j,k,l;
clrscr();
for(i=0;i<5;i++)
{
for(k=4;k>i;k--)
{
printf(" ");
}
for(j=0,l=i;j<=i;j++,l++)
{
printf("%c",Str[l]);
}
l=l-2;
for(k=0;k<i;k++,l--)
printf("%c",Str[l]);
printf("\n");
}
getch();
Output:--
1
232
34543
4567654
567898765