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

Unit 4 - Exercise Solution

C programming language

Uploaded by

apurbabarman.mtb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Unit 4 - Exercise Solution

C programming language

Uploaded by

apurbabarman.mtb
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 61

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

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.

Algorithm:-

Step 1: Store n=10


Step 2: For i=1 to i=10, Enter the values of x[i]=v1.
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.
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])
total_x2=total_x2+(x[i]*x[i])

Step 7: Compute temp=total_x*total_y


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

Step 8: Display y=mx+c.


Step 9: Stop
Flowchart:--

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. */

// Date March 16,2010

#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();

printf("Enter the values for x: ");

for(i=0;i<10;i++)
{
scanf("%d",&v1);
x[i]=v1;
}

printf("Enter the values for y: ");

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;

printf(" \nThe equation of the straight line is: ");


printf(" Y=%fX+%f",m,c);

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:--

Step 1: Read Array Temp.


Step 2: Display Temp
Step 3: Store MinTemp=MaxTemp=Temp [0][0],City1=0,City2=0.
Step 4: For i=0 to 2 repeat Step 5 to Step
Step 5: For j=0 to 2 repeat Step 6 to Step
Step 6: Check MaxTemp<Temp[i][j] go to Step 7
Step 7: Compute MaxTemp=Temp[i][j],City1=j+1
Step 8: Check MinTemp>Temp[i][j] go to Step 9
Step 9: Compute MinTemp=Temp[i][j], City2=j+1
Step 10: Display MaxTemp & MinTemp
Flowchart:--
A

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();

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

printf(" City \n ");

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;
}
}
}

printf("\n\nHighest Temperature of City %d is %d\n",City1,MaxTemp);


printf("Lowest Temperature of City %d is %d\n",City2,MinTemp);

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:--

Step1. Initialize c1,c2, c3,c4,c5,count,count_sp with 0.


Step2. For i=0 to i=5, repeat step3.
Step3. Enter the value of v and store in vote[i]
Step4. For i=0 to i=5, repeat from step5 till step14 and for i>5,go to step15.
Step5. Check if vote[i]==1,if true, go to step6,if false go to step7.
Step6.Calculate c1=c1+1
Step7. Check if vote[i]==2,if true, go to step8,if false go to step9.
Step8.Calculate c2=c2+2
Step9.Check if vote[i]==3,if true, go to step10,if false go to step11.
Step10. Calculate c3=c3+2
Step 11.Check if vote[i]==4,if true, go to step12,if false go to step13.
Step12.Calculate c4=c4+2
Step13.Check if vote[i]==5,if true, go to step14,if false go to step15.
Step14.Calculate c5=c5+2
Step15. Display c1,c2,c3,c4 and c5.
Step16. For i=0 to i=5, repeat from step 17 to step, and i>5,go to step 20
Step17.Check if vote[i]<5,if true go to step18 and if false, go to step 19.
Step18.count=count+1 and go to step 20.
Step19.count_sp=count_sp+1 and go to step 20.
Step20. Display count and count_sp
Step21. Stop.
Flowchart:--

Start

c1=c2=c3=c4=c5=cou
nt=count_sp=0
c1,c2,c3,
c4,c5
i=1

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


i<=5 i<=5
1 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+ Vote[i]== c5=c5+


1 5 1

Count count=count Vote[i i<=5 i=1


Count_s +1 ]<=5
p

i=i+1
END count_sp=count_sp+
1
Program:--

/* 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. */

// Date March 16,2010

#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();

printf("Enter your votes for 5 candidates:");


for(i=1;i<=5;i++)
{
scanf("%d",&v);
vote[i]=v;
}

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;
}

printf(" The number of valid votes is:%d",count);


printf(" \nThe number of spoilt votes is:%d",count_sp);
getch();
}

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

5.1 The annual examination results of 10 students are tabulated as follows:

Roll No. Subject1 Subject2 Subject3


.
.
.
.____________________________________________________________________

Write a program to read the data and determine the following:


(a) Total marks obtained by each student.

(b) The highest marks in each subject and the Roll No. of the student who
secured it.

(c) The student who obtained the highest total marks.

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:--

/* The annual examination results of 10 students are tabulated as follows:

Roll No. Subject1 Subject2 Subject3


.
.
.
.

Write a program to read the data and determine the following:


(a)Total marks obtained by each student.
(b) The highest marks in each subject and the Roll No. of the student who secured it.
(c) The student who obtained the highest total marks.
*/

// Date March 16,2010

#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();

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

for(i=0;i<MAX;i++)
scanf("%d",&sub1[i]);

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

for(i=0;i<MAX;i++)
scanf("%d",&sub2[i]);

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

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;
}
}

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


printf("\nThe highest marks in subject2 is %d and the roll number is %d",max2,roll2);
printf("\nThe highest marks in subject3 is %d and the roll number is %d",max3,roll3);
printf("\n The highest total marks is %d and the roll number is %d ",max,roll);

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:--

Step 1: Read m, n, Array a & Array b.


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

Read m,n arrays a, & b

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;

printf("Enter no. of elements of array : ");


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

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


for(i=0;i<n;i++)
scanf("%d",&a[i]);

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


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

mn=m+n;

while(ax<n && bx<m)


{
if(a[ax]<b[bx])
{
c[cx]=a[ax];
ax++;
}
else
{
c[cx]=b[bx];
bx++;
}
cx++;
}
if(ax==n)
{
while(bx<m)
{
c[cx]=b[bx];
bx++;
cx++;
}
}
else
{
while(ax<n)
{
c[cx]=a[ax];
ax++;
cx++;
}
}
//sorted array
printf("the sorted array is : \n");
for(i=0;i<mn;i++)
printf("%d ",c[i]);

getch();
}

7.7 Write a program that will read the values of elements of A and B and produce the
product matrix C.

Algorithm:--

Step 1: Read row, col, Array a and Array b.


Step 2: Display Array a and b.
Step 3: For i=0 to row repeat Step 4 to Step 7
Step 4: For j=0 to col repeat Step 5 to Step 7
Step 5: Store 0 to c[i][j]
Step 6: For k=0 to col repeat Step 7
Step 7: Compute c[i][j]=c[i][j]+(a[i][k]*b[k][j])
Step 8: Display c.
Flowchart:--
START

Read row, col, Array


a and Array b

Display Array a and b

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])

Display Array a and b

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();

printf("Enter row of matrix");


scanf("%d",&row);

printf("Enter column of matrix");


scanf("%d",&col);

printf("Enter first matrix\n");

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

printf("\nEnter second matrix \n");

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

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

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

printf("\nSecond matrix is\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();
}

7.8 Write a program that fills a five-by-five as follows:


 Upper left triangle with +1s
 Lower right triangle with -1s
 Right to left diagonal with zeros
Display the contents of the matrix using not more than two printf statements.

Algorithm:--

Step 1: Store Upper left triangle with +1s


Step 2: Store Lower right triangle with -1s
Step 3: Store Right to left diagonal with zeros
Step 4: Display A
Flowchart:--
START

Store Upper left


triangle with +1s

Store Lower right


Triangle with -1s

Store Right to left


diagonal with zeros

Display A

END

Program:--

//Write a program that fills a five-by-five as follows:


//• Upper left triangle with +1s
//• Lower right triangle with -1s
//• Right to left diagonal with zeros
//Display the contents of the matrix using not more than two printf statements.

// 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();
}

7.9 Write a program to implement selection sort.

Algorithm:--

Step 1: Read Array A.


Step 2: For k=0 to 9 repeat Step 3 to Step 8.
Step 3: Compute Small=A[k] & Loc=k.
Step 4: For i=0 to 9 repeat Step 5 to Step 7 otherwise go to Step 7.
Step 5: Check Small>A[i] then go to Step 6 otherwise go to Step 4.
Step 6: Compute Small=A[i] & Loc=i.
Step 7: Compute A[Loc]=A[k], A[k]=Small.
Step 8: Display Sorted Array A.
Flowchart:-- START

Read Array A

k=0

NO
k<=9

YES

Small=A[k] & Loc=k

i=0

NO
i<=9

YES

Small
>A[i]
YES

Small=A[i] & Loc=i

A[Loc]=A[k], A[k]=Small

Display A

END

Program:--

//Write a program to implement selection sort.


// Date : 16/03/2010

#include<stdio.h>
#include<conio.h>
void main()
{
int A[10];
int i,k,Small,Loc;

clrscr();

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

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;
}

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

for(i=0;i<=9;i++)
printf("%d ",A[i]);

getch();
}

7.10 Write a program to implement Binary Search algorithm.

Algorithm:--

Step 1: Store 0 to Beg & 9 to End.


Step 2: Compute Mid=(Beg+End)/2.
Step 3: Read a Sorted Array Str & an Item to Search.
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
Step 6: Compute End=Mid-1
Step 7: Compute Beg=Mid+1
Step 8: Compute Mid=(Beg+End)/2
Step 9: Check Beg>End go to Step 10 otherwise go to Step 11
Step 10: Display “Item Not Found”
Step 11: Display “Item Found”
Flowchart:--
START

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:--

//Write a program to implement Binary Search algorithm.


// Date : 16/03/2010

#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;

printf("Enetr Any Sorted Array:--\n");

for(i=0;i<10;i++)
scanf("%d",&Str[i]);

printf("Enter Item Which U want to Search:--\n");


scanf("%d",&Item);

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:--

Step 1: Read a string Str.


Step 2: For i=0 to End of String repeat Step 3 to Step
Step 3: Compute Len=Len+1
Step 4: Display Len.
Flowchart:-- START

Read a string Str

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;

printf("Length of String is %d",Len);


getch();

7.12 Write a program that will count the number occurrences of a specified character in a
given line of text.

Algorithm:--

Step 1: Read a string Str & a Character CheckChar


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

Read a string Str


& a Character
CheckChar

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;

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


getch();

7.13 Write a program to read a matrix of size m*n and print its transpose.

Algorithm:--

Step 1: Read Row, Col & Matrix A


Step 2: Display A
Step 3: For i=0 to Col repeat Step 4 to Step 5 otherwise go to Step 6
Step 4: For j=0 to Row repeat Step 5 otherwise go to Step 3
Step 5: Compute C[i][j]=A[j][i]
Step 6: Display C
Flowchart:--
START

Read Row, Col


& Matrix A

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 Number of Rows:--\n");


scanf("%d",&Row);

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


scanf("%d",&Col);

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:

Sum= (1*first digit) + (2*second digit) + (3*third digit)+…………….+ (9*ninth digit)

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

Valid ISBN END


Program:--

//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;

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

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:--

Step 1: Read Row, Col, Array A & B.


Step 2: Display Array A & B.
Step 3: Compute Addition of Array A & B & Store in Array C.
Step 4: Display Array C
Flowchart:--
START

Read
Row,Col

Read Array A
&B

Display 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();

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


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

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

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

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

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:--

Step 1: Read a String Str.


Step 2: Read Number of Characters Which We Want to Extract Say m.
Step 3: Read Beginnig Index from Which We Want to Extract Say n.
Step 4: For i=n-1 to m+n-1 repeat Step 5.
Step 5: Display Str[i].

Flowchart:-- START

Read String
Str, m & n

i=n-1

NO
i<m+n-1

YES

Display
Str[i]

END

Program:--

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

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

#define MAX 50

void main()
{
char Str1[MAX];
int i,m,n,j;

clrscr();

printf("Enter A String:--\n");
scanf("%[^\n]s",Str1);

printf("\nEnter Number of Characters Which U Wnat to Extract-->\n");


scanf("%d",&m);

printf("\nEnter Beginnig Index from Which U Want to Extract-->\n");


scanf("%d",&n);

printf("\nExtracted String is:--\n\n");

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:

Vehicle Type Month of sales Price


MARUTI-800 02/01 210000
MARUTI-DX 07/01 265000
GYPSY 04/02 315750
MARUTI-VAN 08/02 240000

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:--

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
Say Veh[i], St_Mon[i], En_Mon[i] & Price[i].
Step 3: Display Veh[i], St_Mon[i], En_Mon & Price[i] for i=0 to n-1.
Step 4: Read Type of Vehicle Say Vehicle.
Step 5: Read Starting & Ending Month Say StMon & EnMon.
Step 6: For i=0 to n-1 repeat Step 7
Step 7: Check (strcmp(Veh[i],Vehicle)==0) & St_Mon[i]>=StMon & En_Mon[i]<=EnMon then
go to Step 8 otherwise go to Step 9
Step 8: Display Vehicle Type Vehicle is Sold During Period StMon / EnMon
Step 9: Display Vehicle Type Vehicle is Not Sold During Period StMon / EnMon

Flowchart:--
A
START

i=0

Read n, Veh[i], St_Mon[i],


En_Mon[i] & Price[i] for
i=0 to n-1
i<=n-1 NO

Display Veh[i], YES


St_Mon[i], En_Mon &
Price[i] for i=0 to n-1
NO (strcmp(Veh[i],Vehicle)
==0) &
St_Mon[i]>=StMon &
En_Mon[i]<=EnMon
Read Vehicle,
StMon & EnMon
YES

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:

// Vehicle Type Month of sales Price


// MARUTI-800 02/01 210000
// MARUTI-DX 07/01 265000
// GYPSY 04/02 315750
// MARUTI-VAN 08/02 240000

// 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();

printf("How Entries U Want to Enter\n");


scanf("%d",&n);

printf("Enter Vehcle Type,Starting Month, Ending Month & Price:--\n");

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();

printf("Vehicle Type Month of Sales Price\n");


for(i=0;i<n;i++)
{
printf("%s 0%d / 0%d %ld\n",Veh[i],St_Mon[i],En_Mon[i],Price[i]);
}
printf("Enter The Type of Vehicle\n");
scanf("%s",Vehicle);

printf("Enter the Starting & Ending Month\n");


scanf("%d %d",&StMon,&EnMon);

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);
}
}
}
}

printf("Vehicle Type %s Not Sold During Period 0%d/0%d\n",Vehicle,StMon,EnMon);

getch();
}

Output:--

How Entries U Want to Enter 4


Enter Vehcle Type,Starting Month, Ending Month & Price:--
MARUTI-800 02/01 210000
MARUTI-DX 07/01 265000
GYPSY 04/02 315750
MARUTI-VAN 08/02 240000

Vehicle Type Month of sales Price


MARUTI-800 02/01 210000
MARUTI-DX 07/01 265000
GYPSY 04/02 315750
MARUTI-VAN 08/02 240000

Enter The Type of Vehicle MARUTI-800


Enter the Starting & Ending Month 02 01

Vehicle Type MARUTI-800 is Sold During Period 02/01


8.9 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”.

Algorithm:--

Step 1: Read Cost.


Step 2: Compute Rup = Cost, Pai = (Cost - Rup)*100 and i=Rup/100.
Step 3: Check i=1 Display “ONE HUNDRED”………….i=9 Display “NINE HUNDRED”
Step 4: Compute i=Rup%100, R=i/10, Re=(float)i/10.
Step 5: Check R=1 Display “TEN”………….i=9 Display “NINETY”
Step 6: Compute R=(Re-R)*10.
Step 7: Check R=1 Display “ONE”………..R=9 Display “NINE” & Display “AND PAISE”
Step 8: Compute i=Pai/10, Re=(float)Pai/10, R=(Re-i)*10
Step 9: Check i=1 Display “TEN”………….i=9 Display “NINETY”
Step 10: Check R=1 Display “ONE”………..R=9 Display “NINE”

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

Rup = Cost, Pai =


(Cost - Rup)*100 i
and i=Rup/100

TEN TWENT THIRTY FOURT FIFTY SIXTY SEVEN EIGHTY NINETY


Y Y TY

1 2 3 4 5 6 7 8 9
i=Rup%100,
R=i/10,
Re=(float)i/10 R

ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE

1 2 3 4 5 6 7 8 9

R=(Re-R)*10 R
AND PAISE

TEN TWENT THIRTY FOURT FIFTY SIXTY SEVEN EIGHTY NINETY


Y Y TY

1 2 3 4 5 6 7 8 9
i=Rup%100,
R=i/10,
i
Re=(float)i/10

ONE TWO THREE FOUR FIVE SIX SEVEN EIGHT NINE

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();

printf("\nEnter Cost of an ITEM-->\n");


scanf("%f",&Cost);

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;
}

printf("AND PAISE ");

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:--

Enter Cost of an ITEM-->


125.25

ONE HUNDRED TWENTY FIVE AND PAISE TWENTY FIVE

8.10 Develop a program that will read and store the details of a list of students in the format

Roll No. Name Marks Obtained


…………. ……… ………………..
…………. ……… ………………..

And produce the following output lists:


a) Alphabetical list of names, roll numbers and marks obtained.
b) List sorted on roll numbers.
c) List sorted on marks (rank-wise list)

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

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;

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();

printf("How Many Student Name U Want to Enter\n\n");


scanf("%d",&n);

printf("Enter Roll No. & Students Name & Total Marks:--\n");

for(i=0;i<n;i++)
{
scanf("%d",&Roll_No[i]);
scanf("%s",Stu_Name[i]);
scanf("%d",&Marks[i]);
}

clrscr();

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(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;
}
}
}

printf("\nAccording to Student Names:--\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(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:--

Step 1: Read two strings Say Str1 & Str2.


Step 2: Check strcmp(Str1,Str2)=0 true then Display “Both strings are equal” otherwise go to
Step 3.
Step 3: Check strcmp(Str1,Str2)<0 then Display “First string is less than Second” otherwise go to
Step 4.
Step 4: Display “First string is greater than Second”.

Flowchart:--
START

Read two
strings Say Str1
& Str2

YES
strcmp(Str1, Display “Both
Str2)=0 strings are equal”

NO

YES Display “First


strcmp(Str1,
string is less than
Str2)<0
Second”

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();

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


scanf("%[^\n]s",Str1);

fflush(stdin);

printf("Enter Second String:--\n");


scanf("%[^\n]s",Str2);

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:--

Enter First String:--


Ritesh
Enter Second String:--
Jain
First String is Greater Than

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:--

Step 1: Read text Str1 & substring Str2, Count=0.


Step 2: Compute Len=strlen(Str1), Len1=strlen(Str2).
Step 3: Copy Str1 to Str3.
Step 4: For i=0 to Len repeat Step 5 to Step 6 otherwise go to Step 8
Step 5: Check (Str3=strstr(Str3,Str2))!=NULL then go to Step 6 otherwise go to Step 7
Step 6: Compute i=i+Len1, Count=Count+1.
Step 7: Copy Str3+Len1 to Str3.
Step 8: Display Count.

Flowchart:--
START

Read text Str1


substring Str2
Count=0

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

Copy Str3+Len1 to Str3, i=i+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));
}

printf("\n\nNumber of occurences is:-- %d",Count);


getch();
}
8.13 Write a program that will copy m consecutive characters from a string s1 beginning at
position n into another string s2.

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

i=n-1 & j=0

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);

printf("\nEnter Number of Characters Which U Wnat to Copy-->\n");


scanf("%d",&m);

printf("\nEnter Beginnig Index from Which U Want to Copy-->\n");


scanf("%d",&n);

for(i=n-1,j=0;i<m+n;i++,j++)
{
Str2[j]=Str1[i];
}

Str2[m]='\0';

printf("\n\nCopied String is--> %s \n\n",Str2);


getch();
}

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:--

Step 1: Read n (Number of Student Name U Want to Enter).


Step 2: Read Roll_No[i] & Stu_Name[i] for i=0 to n.
Step 3: Read Roll (Student Roll No which U want to Search).
Step 4: For i=0 to n repeat Step 5 to Step 6
Step 5: Check Roll==Roll_No[i] then go to Step 6
Step 6: Compute Index=i
Step 7: Display Index.
Step 8: Read Name (Student Name which U want to Search).
Step 9: For i=0 to n repeat Step 5 to Step 6
Step 10: Check strcmp(Stu_Name[i],Name)==0 then go to Step 6
Step 11: Compute Index=i
Step 12: Display Index.

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();

printf("How Many Student Name U Want to Enter\n\n");


scanf("%d",&n);

printf("Enter Roll No. & Students Name:--\n");

for(i=0;i<n;i++)
{
scanf("%d",&Roll_No[i]);
scanf("%s",Stu_Name[i]);
}

printf("\nEnter Student Roll No which U want to Search:--\n");


scanf("%d",&Roll);

for(i=0;i<n;i++)
{
if(Roll==Roll_No[i])
{
Index=i;
}
}

printf("\nName of Student is --> %s whose Roll No is:--%d",Stu_Name[Index],Roll);

printf("\n\nEnter Student Name which U want to Search:--\n");


scanf("%s",Name);

for(i=0;i<n;i++)
{
if(strcmp(Stu_Name[i],Name)==0)
{
Index=i;
}
}

printf("\n\nRoll No of is:-- %d Student Whose Name is:--


%s\n",Roll_No[Index],Stu_Name[Index]);
getch();
}

Output:--

How Many Student Name U Want to Enter


3
Enter Roll No. & Students Name:--
2 Ritesh
7 Amit
12 Pooja
Enter Student Roll No which U want to Search:--
7
Name of Student is --> Amit whose Roll No is:--7

Enter Student Name which U want to Search:--


Pooja
Roll No of is:-- 12 Student Whose Name is:-- Pooja

8.15 Given a string

char str[ ] =”123456789”;

Write a program that displays the following:


1
232
34543
4567654
567898765

Algorithm:--

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
Step 4: Display “ ” (Space).
Step 5: Store i to l.
Step 6: For j=0 to i repeat Step 7 to Step 8
Step 7: Display Str[l]
Step 8: Compute l=l+1
Step 9: Compute l=l-2
Step 10: For k=0 to i repeat Step 11 to Step 12
Step 11: Display Str[l]
Step 12: l=l-1.
Step 13: Display “\n”
Flowchart:-- START

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

You might also like