You are on page 1of 39

Program 1

Design and develop a flowchart or an algorithm that takes tree coefficients (a, b and c) of 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 the possible roots for a given set of coefficients with appropriate
messages.
Algorithm: To compute all possible roots of the quadratic equation
Step 1: Start
Step 2: [Input coefficients of quadratic equation]
read a, b, c
Step 3: [Check for the valid coefficients]
if a=0 and b=0 then
print Invalid coefficients
else a=0 then
root1 = -c/b
print Linear equation
print root
Step 4: [Compute discriminate value]
disc = b+b-4*a*c
Step 5: [Based on discriminate value, classify and calculate all possible roots and print them]
if disc=0 then
root1 = root2 = -b/(2*a)
print The roots are real and equal
print root1,root2
else if disc>0 then
root1 = (-b+disc)/(2*a)
root2 = (-b -disc)/(2*a)
print The roots are real and distinct
print root1,root2
else
real = -b/(2*a)
imag = disc / (2*a)
print The roots are real and complex
print root1,root2
end if
end if
Start 6: Stop

Start

read a, b, c

is a=0 and b=0?

T
print Invalid coefficients

F
is
a=0?

F
disc = b+b-4*a*c

is disc>0 ?

is disc=0 ?

T
print Linear equation

root1= -c/b

print root1

print Roots are real and


equal

root1=root2= -b/(2*a)

print Roots are real and


distinct

print Roots are real and


complex

root1=(-b+disc)/(2*a)
root2=(-b -disc)/(2*a)

real = -b/(2*a)
imag = disc / (2*a)

print root1,root2

Stop

#include<stdio.h>
#include<math.h>
void main()
{
float a,b,c,disc,root1,root2,real,img;
printf("enter a,b,c values\n");
scanf("%f %f %f",&a,&b,&c);
if((a==0)&&(b==0))
{
printf("roots cannot be determined\n");
}
else if(a==0)
{
printf("linear equation\n");
root1=-c/b;
printf("Root1=%.3f\n",root1);
}
else
{
disc=b*b-4*a*c;
if(disc==0)
{
printf("The roots are real and equal\n");
root1=root2=-b/(2*a);
printf("Root1=%.3f\n Root2=%.3f\n",root1,root2);
}
else if(disc>0)
{
printf("The roots are real and distinct\n");
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("Root1=%.3f\n Root2=%.3f\n",root1,root2);
}
else
{
printf("The roots are real and imaginary\n");
real=-b/(2*a);
img=sqrt(fabs(disc))/(2*a);
printf("Root1=%.3f+i%.3f\n Root2=%.3f-i%.3f",real,img,real,img);
}
}
}

Program 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.
Algorithm: To check whether the given number is palindrome or not
Step 1: Start
Step 2: [Input an integer number]
read num
Step 3: [Set the value of num into the variable n]
n=num
Step 4: [Reverse the given number]
while n!=0 then
rem = num%10
num = num/10
rev = rev*10+rem
end while
Step 5: [Output the reverse number]
print rev
Step 6: [Check the number is palindrome or not]
if rev=n then
print Palindrome
else
print Not a palindrome
end if
Step 7: Stop

Start

read num

n=num

F
n!=0

T
rem = num%10
num = num/10
rev = rev*10+rem

is rev=n?

Print Not a palindrome

print Palindrome

Stop

#include<stdio.h>
void main()
{
int n,a,rem,rev=0;
printf("Enter the value\n");
scanf("%d",&n);
a=n;
while(n!=0)
{
rem=n%10;
rev=rev*10+rem;
n=n/10;
}
printf("Reverse number=%d\n",rev);
if(a==rev)
printf("%d is a palindrome\n",a);
else
printf("%d is not a palindrome\n",a);
}

Program 3
3 a) 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: dont use library function sqrt
(n).
Algorithm: To find the square root of the given number.
Step 1: Start
Step 2: [Input the number to find the square root]
read n
Step 3: [Check if the given number is positive or negative number]
if n>=0 then
goto to step 4
else
print No square root for negative number.
end if
Step 4: [Compute the square root of the given number]
[Calculate decimal part of the square root]
for s=1 to n
decrement s
end for
[Calculate the fractional part]
for d=0.001 to 1.0
x=s+d
if x*x>n then
x=x-0.001
goto to Step 5
end if
end for
Step 5: [Display the calculated square root result]
print x
Step 6: [ Display the square root of the built in function sqrt()]
print sqrt(n)
Step 7: Stop

Start

read n

is
n>=0

s=1
s++
s*s<=n

s--

d=.001
d=d+.00
d<1.0

T
x=s+d

is
x*x>n

T
x=x-.001

print No square root for


negative number

print x

print sqrt(n)

stop

#include<stdio.h>
#include<math.h>
void main()
{
int s;
float n,d,x;
printf("Enter the value\n");
scanf("%f",&n);
if(n>=0)
{
for(s=1;s*s<=n;s++);
s--;
for(d=0.001;d<1.0;d+=0.001)
{
x=s+d;
if(x*x>n)
{
x=x-0.001;
break;
}
}
printf("Square root of %.3f=%.3f\n",n,x);
printf("Square root using inbuilt function=%.3f\n",sqrt(n));
}
else
printf("Invalid input\n");
}

3 b) 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.
Algorithm: To find the given year is leap year or not
Step 1: Start
Step 2: [Input the valid year]
read year
Step 3:[Check for leap year]
if year%4==0 and year%100!=0 or year%400==0 then
print It is Leap Year
else
print It is not a Leap Year
end if
Step 4: Stop
Start

read year

is
year%4==0 and year%100!=0 or year%400==0

print it is leap year

print it is not leap year

stop

#include<stdio.h>
void main()
{
int year;
printf("Enter the valid year\n");

scanf("%d",&year);
if((year%4==0)&&(year%100!=0)||(year%400==0))
printf("%d is a leap year\n",year);
else
printf("%d is not a leap year\n",year);
}

Program 4
Design and develop an algorithm for evaluating the polynomial F(x)=a 4x4+ a3x3+ a2x2+a1x1+a0x0,for a given
value of x and its coefficients using Horners method. Implement a C program for the developed algorithm
and execute for different sets of values of coefficients and x.
Algorithm: To evaluate the polynomial expression
Step 1:Start
Step 2:[Input the number of elements]
read n
Step 3:[ Input the values into the array]
for i=n to 0
read a[i]
end for
Step 4:[Input the value of x]
read x
Step 5:[Intialize sum to zero]
sum=0
Step 6:[Calculate sum ]
for i=n to 1
sum=(sum+a[i])*x
end for
Step 7:[Add last term to the sum]
Sum=sum+a[0]
Step 8:[Output the result]
print sum
Step 9:Stop

#include<stdio.h>
void main()
{
int a[10],i,n,sum,x;
printf("Enter the number of co-efficients\n");
scanf("%d",&n);
printf("Enter %d values\n",n+1);
for(i=n;i>=0;i--)
{
scanf("%d",&a[i]);
}
printf("Enter the value of x\n");
scanf("%d",&x);
sum=a[n]*x;
for(i=n-1;i>0;i--)

{
sum=(sum+a[i])*x;
}
sum=sum+a[0];
printf("sum=%d\n",sum);
}

Start

read n

i--

i=n

i >=0

T
read a[i]

read x

sum=0

F
i--

i=n
i >=0

T
sum=(sum+a[i])*x

sum=sum+a[0]

print sum

Stop

Program 5
Draw the flowchart and write C program to compute sin(x) using Taylor series approximation given by sin(x)

x x3 x 5 x 7
= 1 ! 3 ! + 5 ! 7 ! + . Compare the result with the built in library functions and print the both the
results with appropriate messages.
Algorithm: To compute sin(x) using Taylor series
Step 1:Start
Step 2:[Input the degree]
read degree
Step 3:[Define PI]
PI=3.142
Step 4:[Compute the value of x]
x=degree*(PI/180)
Step 5:[Assign the values for num,den and i]
num=x
den=1
i=2
Step 6:[Calculate the sum of all terms]
do
term=num/den
num=num*x*x
den=den*i*(i+1)
sum=sum+term
i=i+2
while(fabs(term)>=0.00001))
end do while
Step 7:[Print calculated and predefined sine values]
print sum
Step 8:Stop

Start

Read degree

PI=3.142

x=degree*(PI/180)

num=x
deno=1
i=2

term=num/den
num=num*x*x
den=den*i*(i+1)
sum=sum+term
i=i+2

T
fabs(term)>=0.00001

F
Print calculated sine value&predefined sine value

Stop

#include<stdio.h>
#include<math.h>
#define PI 3.142
void main()
{
int degree,i;
float x,nume,deno,term,sum=0;
printf("Enter the value of degree\n");
scanf("%d",&degree);
x=degree*(PI/180);
nume=x;
deno=1;
i=2;
do
{
term=nume/deno;
nume=-nume*x*x;
deno=deno*i*(i+1);
sum=sum+term;
i=i+2;
}while(fabs(term)>=0.000001);
printf("sine of %d=%.3f\n",degree,sum);
printf("sine using built-in function for %d=%.3f",degree,sin(x));
}

Program 6
Develop an algorithm, implement and execute a C program that reads N integer numbers and arrange them
in ascending order using Bubble sort.
Algorithm: To sort N integer numbers using Bubble sort
Step 1: Start
Step 2: [Input the number of elements]
read n
Step 3: [Input unsorted elements in array]
read a[ ]
Step 4: [Output the original elements]
print a[ ]
Step 5: [Sort the elements in ascending order]
for each value i in array a[i] to n
for each value j in array a[j] to n-i
if a[j] > a[j+1] then
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp
end if
end for
end for
Step 6: [Output the sorted elements]
print a[ ]
Step 7: Stop

Start

read n

i++

i=0
i<n

T
read a[i]

i++

i=0
i<n

T
print a[i]

i++

i=1
i<n

T
j++

i=0
i<n

T
is a[j]>a[j+1]?

T
temp = a[j]
a[j] = a[j+1]
a[j+1] = temp

i++

i=0
Stop
print
a[i]

i<n

#include<stdio.h>
void main()
{
int a[10],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the values\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The original elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
for(i=1;i<n;i++)
{
for(j=0;j<n-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
printf("\nThe sorted elements are\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}

Program 7
Develop, implement and execute a C program that reads matrices A (m*n) and B (p*q) and compute the
product A and B. Read matrix A in major order and matrix B in column major order. Print both the input
matrices and resultant matrix with suitable headings and in matrix format. Program must check the
compatibility of orders of the matrices for multiplication. Report appropriate message in case of
incompatibility.
Algorithm: To compute product of two matrices
STEP 1: Start
STEP 2: [input order of matrix A]
read m,n
STEP 3: [input order of matrix B]
read p,q
STEP 4: [check for compatibility]
if(n!=p)
Print multiplication not possible
return
end if
STEP 5: [input elements of matrix A]
for (i=0;i<m;i++)
for(j=0;j<n;j++)
read a[i][j]
end for
end for
STEP 6: [input elements of matrix B]
for(i=0;i<p;i++)
for(j=0;j<q;j++)
read b[i][j]
end for
end for
STEP 7: [find product of 2 matrices]
for(i=0;i<m;i++)
for(j=0;j<q;j++)
c[i][j]=0;
for(k=0;k<n;k++)
c[i][j]=c[i][j]+a[i][k]*b[k][j]
end for
end for
end for
STEP 8: [print matrix A]

for(i=0;i<m;i++)
for(j=0;j<n;j++)
print a[i][j]
end for
end for
STEP 9: [print matrix B]
for (i=0;i<p;i++)
for(j=0;j<q;j++)
print b[i][j]
end for
end for
STEP 10: [print resultant matrix C]
for (i=0;i<m;i++)
for(j=0;j<q;j++)
print c[i][j]
end for
end for
STEP 11: Stop

Start

Read m,n

Read p,q

(n!=p)

T
A

Print multiplication not possible

i++

i=0
i<m
T

j++

j=0

j<n
T
Read a[i][j]

i++

i=0
i<p
T

B
j++

j=0

j<q
T
Read b[i][j]

i++

i=0
i<m
T

j++

j=0
j<q
T

C[i][j]=0

k++

k=0

k<n
T
C[i][j]=c[i][j]+a[i][k]*b[k][j]

i++

i=0
i<m
T

j++

j=0

j<n
T
print a[i][j]

i++

i=0
i<p
T

C
j++

j=0

j<q
T
print b[i][j]

i++

i=0
i<m
T

j++

j=0

j<q
T
print c[i][j]

Stop

#include<stdio.h>
void main()
{
int a[10][10],b[10][10],c[10][10],i,j,k,m,n,p,q;
printf("Enter the order of matrix A\n");
scanf("%d %d",&m,&n);
printf("Enter the order of matrix B\n");
scanf("%d %d",&p,&q);
if(n==p)
{
printf("Enter the values for matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter the values for matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
scanf("%d",&b[i][j]);
}
}

for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
c[i][j]=0;
for(k=0;k<n;k++)
{
c[i][j]=c[i][j]+a[i][k]*b[k][j];
}
}
}
printf("The elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
printf("The elements of matrix B\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("The resultant matrix C\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
else
printf("Matrix multiplication not possible\n");
}

Program 8
Develop, implement and execute a C program to search a name in a list of names using Binary searching
technique.
Algorithm: To search a name using Binary search
Step 1: Start
Step 2: [Input the number of elements]
read n
Step 3: [ Input the array items]
for i=0 to n
read name[i]
end for
Step 4: [Input the key]
Read key
Step 5: [Intialize low to zero and high to last element]
low=0
high=n-1
Step 6: [Calculate mid element and compare with key ]
while( low<=high)
mid=low+high/2
if name[mid] = key then
printsearch successful,print in mid+1 location
goto step 8
else if name[mid]>key then
high=mid+1
else
low=mid+1
end if
end while
Step 7:[Print a message if not successful]
Print search unsuccessful name not present in the list
Step 9:Stop

Start

Read n

i++

i =0
i<n

T
Read name[i]

Read key

Low=0
High=n-1

F
Print unsuccessful search

low<=high

T
Mid=(low+high)/
2

If(name[mid]==key)

Print search successful

T
F

If(name[mid]>key)

T
low=mid+1

High=mid-1

stop

#include<stdio.h>
#include<string.h>
void main()
{
char name[10][20],key[20];
int n,i,low,high,mid,found=0;
printf(enter the number of names to read,n=);
scanf(%d,&n);
printf(enter the names in ascending order\n);
for((i=0;i<n;i++)
scanf(%s,name[i]);
printf(enter the name to search:);
scanf(%s,key);
low=0;
high=n-1;
while(low<=high&&!found)
{
mid=(low+high)/2;
if(strcmp(name[mid],key)==0)
found=1;
else if(strcmp(name[mid],key)<0)
low=mid+1;
else
high=mid-1;
}
if(found==1)
printf(name found in position:%d,mid+1);
else
printf(name not found);
}

Program 9
9 a) Write and execute a c program that implements string copy operation STRCOPY(str1,str2) that copies a
string str1 to another string str2 without using library function.
Algorithm: To copy contents of one string to another without library function.
Step 1: Start
Step 2: [Input source string]
Read str1
Step 3: [call the function strcopy by passing str1 and str2]
Strcopy(str1,str2)
Step 4: [print destination string]
Print str2
Step 5: Stop
Algorithm: Function to implement strcopy without library function.
Step 1: [Receive string from main function]
Step 2: [To copy the string]
while (s1[i]!=\0)
s2[i] = s1[i];
i++;
end while
Step 3: [Append the null string to the destination string]
s2[i] = \0
Step 4: [end of function]

Start

Read str1

Strcopy(s1,s2)

s1[i]!=\0

T
Strcopy(str1,str2)
s2[i] = s1[i]
i++
Print str2

Stop

S2[i] = \0

#include<stdio.h>
void strcopy(char s1[50],char s2[50])
{
int i=0;
while (s1[i]!=\0)
{
s2[i]=s1[i];
i++;
}
s2[i]=\0;
}
void main( )
{
char str1[50],str2[50];
printf (enter the source string\n);
gets (str1);
strcopy (str1,str2);
printf (destination string is\n);
puts (str2);
}

9 b) Write and execute a c program that reads a sentence and prints frequency of each of the vowels and total
count of consonants.
Algorithm: To count number of consonants and vowels
Step 1: Start
Step 2: [input the sentence]
read s
Step 3: [check whether the letter of the sentence are alphabets]
for i=0 to strlen[s]
if isalpha(s[i]) then
ch=tolower(s[i])
if ch=a or ch=e or ch=i or ch=o or ch=u then
increment vc
else
increment cc
end if
end if
end for
Step 5: [print no of vowels]
Print vc
Step 6: [print no of consonants]
Print cc
Step 7: Stop

Start

Read s

i++

i=0
i<strlen(s)

F
T
is(isalphas[i])

T
Ch=tolower(s[i])

Is ch==a or ch==e or ch==I or ch==o


or ch==u

Increment cc++

Increment vc++

Print vc

Print cc
Stop

#include<stdio.h>
#include<string.h>
void main( )
{
char s[100],ch;
int i, vc, cc=0;
printf(enter the sentence\n);
gets(s);
for(i=0;i<strlen(s);i++)
{
if(isalpha(s[i]))
{
ch=tolower(s[i]);
if(ch==a||ch==e||ch==o||ch==u)
vc++;
else
cc++;
}
}
}

Program 10
10 a) Design and develop a C function right_shift(x,n) that takes two integers x and n as input and return
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 result with suitable
headings.
Algorithm: To right shift a given number x by n position.
Step 1 : Start.
Step 2 : [Input x and n]
read x,n
Step 3 : [Call function right shift by passing x and n]
result = right_shift(x,n).
Step 4 : [Display the result]
print result
Step 5 : Stop.
Algorithm: Function to shift the number to right.
Step 1 : [receive the value of x and n]
Step 2 : [Perform shift operation]
for i = 0 to n
if x%2=0 then
x = x >>1
else
x= x>>1
x=x+pow(2.31)
end if
end for
Step 3 : [return the value x]
Step 4 : End of function.
right_shift(x.n)
Start

i++
read x and n

result = right_shift(x,n)

i=0
i <n

x%2=0

T
Print result

Stop

x=x>>1

return
x=x>>1x
x=x+pow(2,31)

return x

#include<stdio.h>
#include<math.h>
unsigned int rightshift(unsigned int,unsigned int);
void main()
{
unsigned int x,n,result;
printf("Enter the value and the number of bits to be rotated\n");
scanf("%u%u",&x,&n);
result=rightshift(x,n);
printf("resulting value after right rotating %u by %u position=%u\n",x,n,result);
}
unsigned int rightshift(unsigned int x,unsigned int n)
{
int i;
for(i=0;i<n;i++)
{
if(x%2==0)
x=x>>1;
else
{
x=x>>1;
x=x+pow(2,31);
}
}
return x;
}

10 b) Design and develop a C function isprime(num) that accepts an integer argument and returns 1 if the
argument isprime, a 0 otherwise. Write a C program that invokes this function to generate prime numbers
between the given ranges.
Algorithm: To check whether a given number is prime or not.
Step 1 : Start.
Step 2 : [Input the range].
read x and y.
Step 3 : [Assign 0 to flag]
flag =0
Step 4 : [Print the prime numbers]
for i = x to y
if isprime(i) then
print i
flag =1
end if

end for
Step 5 : [Print the message if there is no number in the given range]
if flag =0 then
print no prime numbers within the range.
Step 6 : Stop.

Algorithm: Function to find the number is prime are not.


Step 1 : [Receive the value of n]
Step 2 : [Check whether the number is equal to 0 or 1]
if n=0 or n=1
return 0
Step 3 : [Check whether given number is prime or not]
for i = 2 to n
if n % i=0 then
return 0
end if
end for
return 1.
Step 4 : End of function.

#include<stdio.h>
#include<math.h>
int isprime(int);
void main()
{
int x,y,i,flag=0;
printf("enter the range\n");
scanf("%d%d",&x,&y);
printf("the prime num\n");
for(i=x;i<=y;i++)
{
if(isprime(i))
{
printf("%d\t",i);
flag=1;
}
}
if(flag==0)
{
printf("there are no prime numbers in this range\n");
}
}
int isprime(int n)
{
int i;
for(i=2;i<=sqrt(n);i++)
{
if(n%i==0)
return 0;
}

return 1;
}

Start

isprime(n)
read x , y

i++

flag =0

i= 2
i <=sqrt(n)

i++

i=x

i<=y

T
is n%i=0

T
F

Isprime(i)

return 0
T
print i

return 1

is
flag = 0

T
print No prime numbers

Stop

Program 11
Draw the flowchart and write a recursive C function to find the factorial of number, n!, defined by
fact(n)=1,if n=0.otherwise fact(n)=n*fact(n-1). Using the function, wirte a C program to compute binomial
coefficient

nC r .Tabulate the result for different values of n and r with suitable message.

Algorithm: To compute the binomial co-efficient nCr


Step 1 : Start.
Step 2 : [Input the values for n and r]
read n,r
Step 3 : [Call function to find factorial]
res=fact(n)/(factc(n-r)*fact(r))
Step 4 : [Display the result]
print res
Step 5 : Stop.
Algorithm: Recursive function to find factorial of the number
Step 1 : [receive the value of n]
Step 2 : [Check the base condition]
if n=0 then
return 1
Step 3 : [Call function recursively]
return n*fact(n-1)
Step 4 : End of function.

res=

fact(n)

Start

fact(n)

read n,r

Is
n=0 ?

fact(n-r)

fact(r)
return 0

print res

Start

return n*fact(n-1)

#include<stdio.h>
int fact(n)
{
if(n==0)
return 1;
else
return (n*fact(n-1));
}
void main()
{
int n,res,r;
printf("enter the value of n and r\n");
scanf("%d %d",&n,&r);
res=fact(n)/(fact(n-r)*fact(r));
printf("the %dC%d=%d",n,r,res);
}

Program 12
Given two university information files studentname.txt and studentusn.txt that contents students Names
and USN respectively. Write a C program to create a new file called output.txt and copy the content of files
studentname.txt and studentusn.txt into output file in the sequence show below. Display the contents of
output file output.txt on the screen.
#include<stdio.h>
void main( )
{
FILE *fp1,*fp2,*fp3;
char name[20],USN[20];
fp1=fopen("sname.txt","r");
if(fp1= =NULL)
printf("file doesnot exist\n");
fp2=fopen("susn.txt","r");
if(fp2= =NULL)
printf("file doesnot exist\n");
fp3=fopen("output.txt","w");
while(!feof(fp1)&&!feof(fp2))
{
fscanf(fp1,"%s",name);
fscanf(fp2,"%s ",USN);
fprintf(fp3,"%-15s %10s\n",name,USN);
}
fclose(fp1);
fclose(fp2);
fclose(fp3);
fp3=fopen("output.txt","r");
printf("name\t\t USN\n");
printf("----------------------\n");
while(!feof(fp3))
{
fscanf(fp3,"%s",name);
fscanf(fp3,"%s\n",USN);
printf("%-15s %10s\n",name,USN);
}
fclose(fp3);
}

Program 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). Each field is of an appropriate datatype. Print the marks of all
student given student name as input.
#include<stdio.h>
#include<string.h>
struct student
{
int rollno,marks;
char name[20],grade;
};
void main()
{
struct student s[20];
int n,i,found=0;
char sname[20];
printf("enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the details of student %d\n",i+1);
printf("enter the rollno:");
scanf("%d",&s[i].rollno);
printf("enter the name:");
scanf("%s",s[i].name);
printf("enter the marks:");
scanf("%d",&s[i].marks);
printf("enter the grade:");
scanf(" %c",&s[i].grade);
}
printf("\nstudent details\n");
printf("\nrollno\t name\t\t marks\t grade\n");
for(i=0;i<n;i++)
{
printf("%d\t %s\t\t %d\t %c\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("enter the student name to print the marks\n");
scanf("%s",sname);
for(i=0;i<n;i++)
{
if(strcmp(s[i].name,sname)==0)
{
printf("marks of the student=%d",s[i].marks);
found=1;
}
}

if(found==0)
printf("name not found\n");
}

Program 14
Write a C program using pointers to compute sum, mean and standard deviation of all elements stored in an
array of n real numbers.
#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr, mean, std,sum=0,sumstd=0;
int i,n;
printf("enter the no of elements\n");
scanf("%d",&n);
printf("enter the array elements\n");
for(i=0;i<n;i++)
{
scanf("%f",&a[i]);
}
ptr=a;
for(i=0;i<n;i++)
{
sum=sum+*ptr;
ptr++;
}
mean=sum/n;
ptr=a;
for(i=0;i<n;i++)
{
sumstd=sumstd+pow((*ptr-mean),2);
ptr++;
}
std=sqrt(sumstd/n);
printf("sum=%.3f\t",sum);
printf("mean=%.3f\t",mean);
printf("standard deviation=%.3f\t",std);
}

You might also like