PCD Lab Manual 2017
PCD Lab Manual 2017
LABORATORY MANUAL
SEMESTER - I / II B.E
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
SYLLABUS
Laboratory Experiments
Implement the programs with WINDOWS / LINUX platform using appropriate C
compiler.
1. Design and develop a flowchart or an algorithm that takes three coefficients (a, b, and c)
of a Quadratic equation (ax2+bx+c=0) as input and compute all possible roots. Implement a
C program for the developed flowchart/algorithm and execute the same to output the
possible roots for a given set of coefficients with appropriate messages.
2. Design and develop an algorithm to find the reverse of an integer number NUM and
check whether it is PALINDROME or NOT. Implement a C program for the developed
algorithm that takes an integer number as input and output the reverse of the same with
suitable messages. Ex: Num: 2014, Reverse: 4102, Not a Palindrome.
3a. Design and develop a flowchart to find the square root of a given number N. Implement
a C program for the same and execute for all possible inputs with appropriate messages.
Note: Don’t use library function sqrt(n).
3b. Design and develop a C program to read a year as an input and find whether it is leap
year or not. Also consider end of the centuries.
4. Design and develop an algorithm for evaluating the polynomial f(x) = a4x4 + a3x3 + a2x2 +
a1x + a0, for a given value of x and its coefficients using Horner’s method. Implement a C
program for the same and execute the program for different sets of values of coefficients and
x.
5. Draw the flowchart and Write C Program to compute Sin(x) using Taylor series
approximation given by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + ……. Compare the result
with the built- in Library function and print both the results with appropriate messages.
6. Develop an algorithm, implement and execute a C program that reads N integer numbers
and arrange them in ascending order using Bubble Sort.
7. Develop, implement and execute a C program that reads two matrices A (m x n ) and B
(p x q ) and Compute the product A and B. Read matrix A and matrix B in row major order
and in column major order respectively. Print both the input matrices and resultant matrix
with suitable headings and output should be in matrix format only. Program must check the
compatibility of orders of the matrices for multiplication. Report appropriate message in
case of incompatibility.
8. Develop, implement and execute a C program to search a Name in a list of names using
Binary searching Technique.
BMSIT&M 1/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
10 a. Design and develop a C function RightShift(x ,n) that takes two integers x and n as
input and returns value of the integer x rotated to the right by n positions. Assume the
integers are unsigned. Write a C program that invokes this function with different values for
x and n and tabulate the results with suitable headings.
10b. Design and develop a C function isprime(num) that accepts an integer argument and
returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this
function to generate prime numbers between the given range.
11.Draw the flowchart and write a recursive C function to find the factorial of a number, n!,
defined by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this function, write a C
program to compute the binomial coefficient nCr. Tabulate the results for different values of
n and r with suitable messages.
11. Given two university information files “studentname.txt” and “usn.txt” that contains students
Name and USN respectively. Write a C program to create a new file called
“output.txt” and copy the content of files “studentname.txt” and “usn.txt” into output file in the
sequence shown below. Display the contents of output file “output.txt” on to the screen.
13. Write a C program to maintain a record of “n” student details using an array of structures with
four fields (Roll number, Name, Marks, and Grade). Assume appropriate data type for each field.
Print the marks of the student, given the student name as input.
14. Write a C program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
Reference Book:
1. Reema Thareja, Computer Fundamentals and Programming in C, Oxford Press, 2012.
BMSIT&M 2/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
BMSIT&M 3/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Small circle Shows the continuation from one point in the process
flow to another
Advantages of Flowcharts:
A flowchart is a diagrammatic representation that illustrates the sequence of steps that
must be performed to solve a problem. They are usually drawn in the early stages of
formulating computer solutions to facilitate communication between programmers
and business people.
Flowcharts help programmers to understand the logic of complicated and lengthy
problems.
They help to analyse the problem in a more effective manner
Flowchart can be used to debug programs that have error(s).
E.g.: To compute the Area of Rectangle
BMSIT&M 4/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Start
Print Area
Stop
Pseudocode:
It is a form of structured English that describes algorithms. It facilitates the designers to
focus on the logic of the algorithm without getting bogged down by the details of language
syntax. Pseudocode is a compact and informal high-level description of an algorithm that
uses the structural conventions of a programming language. It is meant for human reading
rather than machine reading, so it omits the details that are not essential for humans. Such
details include keywords, variable declarations, system-specific code and subroutines.
There are no standards defined for writing a pseudocode because it is not an executable
program. Flowcharts can be considered as a graphical alternative to pseudocode, but are
more spacious on paper.
BMSIT&M 5/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 1: Design and develop a flowchart or an algorithm that takes three
coefficients (a, b, and c) of a Quadratic equation (ax2+bx+c=0) as input and compute all
possible roots. Implement a C program for the developed flowchart/algorithm and
execute the same to output the possible roots for a given set of coefficients with
appropriate messages.
Algorithm: To find and output all the roots of a given quadratic equation for non zero
coefficients
Step 1: [Start]
Begin
Step 2: [Input the co-efficients of the quadratic equation
Read a,b,c
Step 3:[Check for the non-zero coefficient of a]
If a = 0 then print “Invalid input”, go to step 2
Step 4: [Find the value of disc]
disc = b * b – 4 * a * c
Step 5: [Find the type and the values of roots of a given quadratic equation] If
(disc = 0) then
Print “The roots are equal”
root1 = root2 = -b / 2.0*a Go
to step 6
Else If (disc > 0) then
Print “The roots are real and distinct”
root1 = (-b + sqrt(disc)) / 2.0*a
root2 = (-b - sqrt(disc)) / 2.0*a
Go to step 6
else
Print “The roots are imaginary”
root1 = -b / 2.0*a
root2 = sqrt(fabs(disc)) / 2.0*a
Step 6: [Output]
Print root1, root2
Step 7: [Stop]
End
BMSIT&M 6/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 1:
#include<stdio.h>
#include<math.h>
int main()
{
float x1,x2,a,b,c,disc;
printf("enter the coordinates a b c\n");
scanf("%f%f%f",&a,&b,&c);
disc=b*b-4*a*c;
if(disc==0)
{
x1=x2=-b/(2*a);
printf("the roots are equal\n");
printf("the roots are x1=&f\nx2=%f\n",x1,x2);
}
//else if(disc>0)
if(disc>0)
{
x1=-b+sqrt(disc)/(2*a);
x2=-b-sqrt(disc)/(2*a);
//else
if(disc<0)
x1=b/(2*a);
x2=sqrt(fabs(disc))/(2*a);
return;
BMSIT&M 7/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 2: Design and develop an algorithm to find the reverse of an integer
number NUM and check whether it is PALINDROME or NOT. Implement a C
program for the developed algorithm that takes an integer number as input and output
the reverse of the same with suitable messages. Ex: Num: 2014, Reverse: 4102, Not a
Palindrome.
rev 0
Step 4: [Repeat step 4 until num become 0]
digit num mod 10 rev
rev *10 + digit num
num / 10.
Step 5: [Check for palindrome] If
(temp == rev)
Print (” num is a palindrome”)
else
Print (“num is not a palindrome”)
Step 6: [Stop]
End
BMSIT&M 8/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 2:
#include<stdio.h>
int main()
{
int n,temp,digit,rev=0;
printf("enter a integer number\n");
scanf("%d",&n);
temp=n;
while (n!=0)
{
digit=n%10;
n=n/10;
rev=digit+10*rev;
}
if(temp==rev)
{
printf("%d is a PALINDROME\n",temp);
}
else
{
printf("%d is not a PALINDROME\n",temp);
}
return;
}
BMSIT&M 9/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No.3:
3a. Design and develop a flowchart to find the square root of a given number N.
Implement a C program for the same and execute for all possible inputs with
appropriate messages. Note: Don’t use library function sqrt(n).
BMSIT&M 10/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 3a:
#include<stdio.h>
#include<stdio.h>
#include<math.h>
int main()
int i;
float n,root,x;
scanf("%f",&n);
x=1;
for(i=1;i<=30;i++)
root=x-(x*x-n)/(2*x);
if(root<0.001)
break;
else
x=root;
printf("sqrt(%f) is %f\n",n,root);
printf("sqrt(%f) is %f\n",n,sqrt(n));
return;
BMSIT&M 11/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
3b. Design and develop a C program to read a year as an input and find whether it is
leap year or not. Also consider end of the centuries.
BMSIT&M 12/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 3b:
#include<stdio.h>
int main()
int y;
scanf("%d",&y);
if(y%100==0)
if(y%400==0)
printf("Century Leapyear\n");
else
else if(y%4==0)
else
return;
BMSIT&M 13/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No.4: Design and develop an algorithm for evaluating the polynomial f(x) =
4 3 2
a4x + a3x + a2x + a1x + a0, for a given value of x and its coefficients using
Horner’s method. Implement a C program for the same and execute the program for
different sets of values of coefficients and x.
Step 1: [Start]
Begin
Step 2: [Input (integer / float) the coefficients of a polynomial of degree 4] For i
BMSIT&M 14/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 4:
#include<stdio.h>
#include<math.h>
int i;
double sum;
sum=a[n]*x;
for(i=n-1;i>=1;i--)
sum=(sum+a[i])*x;
sum=sum+a[0];
return sum;
int main()
int i,n;
double a[20],x,sum;
scanf("%d",&n);
scanf("%lf",&x);
for(i=0;i<=n;i++)
scanf("%lf",&a[i]);
sum=polynomial(a,n,x);
return;
BMSIT&M 15/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 5: Draw the flowchart and Write C Program to compute Sin(x) using Taylor
series approximation given by Sin(x) = x - (x3/3!) + (x5/5!) - (x7/7!) + ……. Compare the result
with the built- in Library function and print both the results with appropriate messages.
BMSIT&M 16/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 5:
#include<stdio.h>
#include<math.h>
int main()
int i,n,teta;
float x,term,pow,fact;
scanf("%d%d",&teta,&n);
x=3.14/180*teta;
term=pow=x;fact=1;
for(i=1;i<2*n;i+=2)
pow=-pow*x*x;
fact=fact*(i+1)*(i+2);
term=term+(pow/fact);
return;
BMSIT&M 17/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Step 1: [Start]
Begin
Step 2: [Input the number of elements in the array]
Read n
Step 3: [Input the n integer numbers] For i
T
Print num[ i ] Stop
BMSIT&M 18/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 6:
#include<stdio.h>
int i,j,temp;
for(i=0;i<n-1;i++)
for(j=0;j<n-(i+1);j++)
if(a[j]>a[j+1])
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
int main()
int a[10],i,n;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
bubble_sort(a,n);
for(i=0;i<n;i++)
printf("%d\n",a[i]);
return;
BMSIT&M 19/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 7: Develop, implement and execute a C program that reads two
matrices A (m x n ) and B (p x q ) and Compute the product A and B. Read matrix A
and matrix B in row major order and in column major order respectively. Print both
the input matrices and resultant matrix with suitable headings and output should be in
matrix format only. Program must check the compatibility of orders of the matrices for
multiplication. Report appropriate message in case of incompatibility.
Step 1: [Start]
Begin
Step 2: [Input the order of matrix A and matrix B]
Read m, n, p, q
Step 3: [Check whether two matrices are multplicable]
If (n = p) then go to step 4
Else
Print (“MULTIPLICATION NOT ALLOWED…TRY AGAIN”)
Go to step 2
End if
Step 4: [Read matrix A]
For i 0 thru m-1 in steps of 1
For j 0 thru n-1 in steps of 1
Read A[ I ][ j ]
End for
End for
Step 5: [Read matrix B]
For i 0 thru p-1 in steps of 1
For j -1 in steps of 1 Read B[ I ][ j
]
End for
End for
Step 6: [Compute C[ I ][ j ]]
For i 0 thru m-1 in steps of 1
For j 0 thru q-1 in steps of 1 C[ I ][ j ]
0
For k 0 thru n-1 in steps of 1
C[ I [ j ] C[ I ][ j ] + A[ I ][ k ] * B[ k ][ j ]
End for
End for
End for
Step 7: [Output]
Print A[m][n], B[p][q],
C[m][q] Step 8: [Stop]
End
BMSIT&M 20/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 7:
#include<stdio.h>
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&x[i][j]);
int i,j;
for(i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d ",x[i][j]);
printf("\n");
int i,j,k;
for(i=0;i<m;i++)
for(j=0;j<q;j++)
BMSIT&M 21/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j];
int main()
int a[10][10],b[10][10],c[10][10],m,n,p,q;
scanf("%d%d",&m,&n);
scanf("%d%d",&p,&q);
if(n!=p)
exit(0);
read_matrix(a,m,n);
read_matrix(b,p,q);
mulmatrix(a,b,c,m,n,p,q);
write_matrix(c,m,n);
return;
BMSIT&M 22/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Algorithm: Binary search to search a given name in list of names & report success or failure.
Step 1: [Start]
Begin
Step 2: [Input the total number of elements in the list of names]
Read n
Step 3: [Input the n names]
Program 8:
BMSIT&M 23/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
#include<stdio.h>
#include<string.h>
int i;
int low,high,mid;
low=0; high=n-1;
while(low<=high)
mid=(low+high)/2;
if(strcmp(key,name[mid])==0)
return mid;
if(strcmp(key,name[mid])>0)
low=mid+1;
if(strcmp(key,name[mid])<0)
high=mid-1;
return -1;
int main()
char name[10][20],key[20];
int n,i,pos;
printf("enter no of names\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s",name[i]);
scanf("%s",key);
BMSIT&M 24/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
pos=name_search(name,n,key);
if(pos==-1)
else
return;
BMSIT&M 25/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
BMSIT&M 26/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 9 a:
#include<stdio.h>
#include<string.h>
int i=0;
while(s1[i]!='\0')
s2[i]=s1[i];
i++;
s2[i]='\0';
int main()
char s1[20],s2[20];
printf("enter string\n");
gets(s1);
my_strcpy(s2,s1);
puts(s2);
return;
BMSIT&M 27/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Algorithm 9b: To find frequency of each of the vowels and total count of consonants.
Step 1: [Start]
Begin
Step 2: [Input any string]
Read a string
Step 3: [Initialize all count variables:]
a=0,e=0,i=0,o=0,u=0,con=0
Step 4: [Increment the count variables]
repeat While ((c=getchar())!=’\n’)
if (c=='a'||c=='A') a=a+1;
if (c=='e'||c=='E') e=e+1;
if (c=='i'||c=='I') i=i+1;
if (c=='o'||c=='O') o=o+1;
if (c=='u'||c=='U') u=u+1;
if((c>='a'&&c<='z') || (c>='A'&&c<='Z')) con++;
Step 5: [Output]
print a, e, i, o, u count and consonants count
Step 6: [Stop]
End
BMSIT&M 28/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 9 b:
#include<stdio.h>
#include<ctype.h>
#include<string.h>
int main()
char s[40],ch;
int i=0,vc=0,cc=0;
scanf("%s",s);
while(s[i]!='\0')
if(isalpha(s[i]))
ch=tolower(s[i]);
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u')
vc++;
else
cc++;
i++;
}
printf("no of vowels=%d\n",vc);
printf("no of consonants=%d\n",cc);
return;
Experiment No 10:
BMSIT&M 29/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
a. Design and develop a C function RightShift(x ,n) that takes two integers x and n as
input and returns value of the integer x rotated to the right by n positions. Assume the
integers are unsigned. Write a C program that invokes this function with different
values for x and n and tabulate the results with suitable headings.
b. Design and develop a C function isprime(num) that accepts an integer argument and
returns 1 if the argument is prime, a 0 otherwise. Write a C program that invokes this
function to generate prime numbers between the given range.
Algorithm 10a: A function rightshift(x,n) returns the value of the integer x rotated to the
right by n bit positions.
Step 1: [Start ]
Begin
Step 2: [Input the unsigned integer X, and N (N > 0) ]
Read x, n
Step 3: [Invoke function rightshift(x, n)]
X1 rightshift(x, n)
return
Step 4: [Output]
Print X1
Step 5: [Stop]
End
Compute a x >> n
Compute z x << (16 – n)
Compute y a|z
Return y
BMSIT&M 30/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 10 a:
#include<stdio.h>
#include<math.h>
int main()
int i,n;
unsigned wl,x,temp,max;
scanf("%u%d",&x,&n);
temp=x;
wl=sizof(int);
if(wl==4)
max=2147483648;
else
max=32768;
for(i=1;i<=n;i++)
if(x%2==0)
x=x>>1;
else
{
x=x>>1;
x+=max;
}
BMSIT&M 31/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
printf("%d",sizeof(int));
return;
BMSIT&M 32/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Algorithm 10b: To determine the given integer number is prime or not and to generate prime
numbers between the given range.
Step 1: [Start]
Begin
Step 2: [Input the range of integer number]
Read a,b
Step 3: [Invoke function isprime(num)] For
i a thru i<=b in steps of 1
res isprime(num)
Step 4: [Output]
If (res = 1)
Print(“Prime numbers between range a and b”)
Else
Print(“No prime numbers exists”)
Step 5: [Stop]
End
Pseudo code
Function isprime(num)
For i 2 thru num / 2 in steps of 1
{
If ( ( num mod i) = 0) then
Return 0
}
Return 1
End For
BMSIT&M 33/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 10b:
#include<stdio.h>
int isprime(int n)
int j;
for(j=2;j<=i-1;j++)
if(i%j==0)
return 0;
return 1;
int main()
int i,j,n1,n2;
scanf("%d%d",&n1,&n2);
for(i=n1;i<=n2;i++)
if(isprime(i))
printf("%d\n",i);
return;
BMSIT&M 34/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment 11: Draw the flowchart and write a recursive C function to find the factorial of
a number, n!, defined by fact(n)=1, if n=0. Otherwise fact(n)=n*fact(n-1). Using this
function, write a C program to compute the binomial coefficient. Tabulate the results for
different values of n and r with suitable messages.
Algorithm 11: To determine factorial of a number and compute the binomial coefficient
using factorial function.
Step 1: [Start]
Begin
Step 2: [Input the number]
Read n, r
Step 3: [Invoke function factorial and binomialCoefficient]
Factorial (n)
binomialCoeff(n,r)
Step 4: [Output]
Print factorial and binomialCoefficient
Step 5: [Stop]
End
Pseudo code
Function factorial (n)
if(n<=0)
return
1;
else
return n*factorial(n-1);
End if
Pseudo code
Function binomialCoeff(n,r)
return factorial(n) / ( factorial(r) * factorial(n-r) );
BMSIT&M 35/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 11:
#include<stdio.h>
int fact(int n)
if (n==0)
return 1;
else
return n*fact(n-1);
int main()
int n,r;
float res;
scanf("%d%d",&n,&r);
res=(fact(n))/(fact(n-r)*fact(r));
printf("%f\n",res);
return;
BMSIT&M 36/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 12: Given two university information files “studentname.txt” and “usn.txt”
that contains students Name and USN respectively. Write a C program to create a new file
called “output.txt” and copy the content of files “studentname.txt” and “usn.txt” into output file
in the sequence shown below. Display the contents of output file “output.txt” on to the screen.
Student Name USN
Name 1 USN1
Name 2 USN2
……. ……..
…..... ……..
Note : Students are required to create two files “studname.txt” and studusn.txt” with the
Contents
studname.txt studusn.txt
Asha 1CG14CS001
Bharath 1CG14CS002
Uma 1CG14CS003
Shilpa 1CG14CS004
Algorithm 12: To copy the content of files “studentname.txt” and “usn.txt” into output file and
display the contents of output file “output.txt” on to the screen.
Step 1: [Start]
Begin
Step 2: [Create the files]
fp1,fp2,fp3
Step 3: If files not created print file not found
Step 4: [Repeat]
while( !feof(fp1) && !feof(fp2) )
write contents of fp1 and fp2 to fp3
Step 4: [Output]
while(!feof(fp3))
Print contents of output file fp3
Step 5: [Stop]
BMSIT&M 37/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 12:
#include<stdio.h>
int main()
FILE *fp1,*fp2,*fp3;
char name[20],usn[20];
fp1=fopen("name.txt","r");
fp2=fopen("usn.txt","r");
fp3=fopen("output.txt","w");
while(fscanf(fp1,"%s",name)>0)
if(fscanf(fp2,"%s",usn)>0)
fprintf(fp3,"%s\t%s\n",name,usn);
else break;
fclose(fp1);
fclose(fp2);
fclose(fp3);
printf("name\tusn\n");
fp3=fopen("output.txt","r");
while(fscanf(fp3,"%s%s",name,usn)>0)
printf("%s\t%s\n",name,usn);
return;
BMSIT&M 38/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 13: Write a C program to maintain a record of “n” student details
using an array of structures with four fields (Roll number, Name, Marks, and Grade).
Assume appropriate data type for each field. Print the marks of the student, given
student name as input.
Algorithm 13: To maintain a record of “n” student details with four fields and print marks of
the student given student name as input
Step 1: [Start]
Begin
Step 2: [Input the number of students]
Read n
Step 3: [Input roll number,name,marks,grade]
Read roll,grade,marks,name
Step 4: [Input name of student whose marks to be displayed]
Read sname
Step 5: [Search student details]
For i 0 thru n in steps of 1
If (strcmp (sname, s[i].name)==0)
Print roll, name, grade marks
Else
Print record not found
Step 6: [Stop]
End
BMSIT&M 39/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 13:
#include<stdio.h>
#include<string.h>
struct student
int rollno;
char name[20];
float m;
char grade;
};
int main()
int i,n;
char key[20];
printf("enter no of
students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
printf("enter %d
student detail\n",i+1);
printf("Rollno:");
scanf("%d",&s[i].rollno);
printf("Name:");
scanf("%s",s[i].name);
printf("Marks:");
scanf("%f",&s[i].m);
getchar();
printf("Grade:");
scanf("%c",&s[i].grade);
printf("enter a name to
search\n");
scanf("%s",key);
for(i=0;i<n;i++)
BMSIT&M 40/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
if(strcmp(key,s[i].name)=
=0)
printf("%d\t%s\n",s[i].roll
no,s[i].name);
printf("%f\t%c\n",s[i].m,s[
i].grade);
break;
if(i==n)
return;
BMSIT&M 41/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Experiment No. 14: Write a C program using pointers to compute the sum, mean and
standard deviation of all elements stored in an array of n real numbers.
Algorithm 14: To compute the sum, mean and standard deviation of all elements stored in an
array of n real numbers.
Step 1: [Start]
Begin
Step 2: [Input the elements]
Read n, a[i]
Step 3: [Initialize] sum=0,
sumstd=0
Step 4: [Compute sum, mean, standard deviation]
Ptr=a
For i 0 thru n in steps of 1
Sum=sum+*ptr
Ptr++
Mean=sum/n
Ptr=a
For i 0 thru n in steps of 1
Sumstd=sumstd+pow((*ptr-mean),2)
Ptr++
std= sqrt(sumstd/n)
Step 5: [Output]
Print sum, mean, standard deviation
Step 6: [Stop]
End
BMSIT&M 42/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
Program 14:
#include<stdio.h>
#include<math.h>
int main()
float a[20],sum1=0,sum2=0,mean,var,dev;
int i,n;
printf("enter no of ele\n");
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%f",a+i);
sum1=sum1+*(a+i);
mean=sum1/n;
for(i=0;i<n;i++)
sum2=sum2+pow((*(a+i)-mean),2);
var=sum2/n;
dev=sqrt(var);
printf("sum is%f\n",sum1);
printf("mean is%f\n",mean);
printf("variance is%f\n",var);
printf("devn is%f\n",dev);
return;
BMSIT&M 43/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
VIVA-VOCE Questions
BMSIT&M 44/46
COMPUTER PROGRAMMING LABORATORY 15CPL16 / 26
BMSIT&M 45/46