POP Lab Manual 2023 24
POP Lab Manual 2023 24
Algorithm: -
STOP
Start
Read option
if
(option=?)
result=num1+num2
result=num1-num2
result=num1*num2
if(num2==0)
number1 is not divide by
zero
div=num1/num2
Result
Stop
2. Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate
messages.
Algorithm:
Step 1: Start
Step 2: Read a,b, c //Read Co-efficient
Step 3: if a=0, b=0, c=0
Write Invalid Input
Step 4: db*b-4a*c //calculate disc
Step 5: if d=0 //Roots are equal
Write Roots are equal
root1 -b/(2*a);
root2 -b/(2*a);
write root1, root2
[ Goto Step 8]
Write Roots are Real and Distinct
root1 -b +sqrt (d)/ (2*a);
root2 -b -sqrt (d)/ (2*a);
write root1, root2
[ Goto Step 8 ]
Step 7: if d<0 // Roots are imaginary
WriteRoots are imaginary
Real -b/(2*a);
ImaginarySqrt (fabs (d))/(2*a);
write root1, root2
Step 8: Stop
Start
Read a,b,c
if (a==0||b==0||
c==0)
d=b*b-4*a*c
A
if(d==0)
root1 -b/2a
Write roots are Img Write roots are Real root2 -b/2a
A
Stop
3. An electricity board charges the following rates for the use of electricity: for the first 200
units 80 paise per unit: for the next 100 units 90 paise per unit: beyond 300 units Rs 1
per unit. All users are charged a minimum of Rs. 100 as meter charge. If the total amount
is more than Rs 400, then an additional surcharge of 15% of total amount is charged.
Write a program to read the name of the user, number of units consumed and print out
the charges.
ALGORITHM
PURPOSE: Read the Name of The User, Number of Units Consumed and Print
INPUT: name [10], unit
OUTPUT: Print the charges for total number of units consumed
START
STEP 1: [Input a name and units]
read name and unit
STEP 2: [Initialization] Metercharge=100
STEP3: [To check electricity unit is less than or equal to 200 and calculate metercharge]
If unit less than or equal to 200
metercharge= metercharge+(unit*.80)
STEP 4: [Else check unit is greater than 200 and greater than 300 and calculate metercharge]
If unit greater than 200 and unit greater than or equal to 300
metercharge= metercharge+(200*0.80)+((unit-200) *0.90))
STEP 5: [Else check unit is greater than 300 and calculate metercharge]
If unit is greater than 300
metercharge= metercharge+(200*0.80)+(300*0.90)+((unit-300)*1))
STEP 6: [To check and calculate if meter charge is greater than 400]
If metercharge greater than or equal to 400
metercharge=metercharge+(metercharge*0.15);
STEP 7: [Finished]
STOP
/*A program to read the name of the user, number of units consumed & print out the charges*/
#include<stdio.h>
#include<conio.h>
void main()
{
int units, minimum=100;
float amount;
char name[20];
clrscr();
printf("Enter user name\n");
scanf("%s", name);
printf("Enter number of consumed units\n");
scanf("%d", &units);
if(units<=200)
{
amount=units * 0.8 + minimum;
}
else if(units>200 && units<=300)
{
amount=units * 0.9 + minimum;
}
else if(units>300)
{
amount=units*1.0 + minimum;
}
if(amount>400)
{
amount=amount + amount*0.15;
}
printf("Electricity Bill Details...............\n”);
printf (“User Name: %s\n", name);
printf ("Number of Units Consumed: %d\n”, units);
printf ("Amount is charged: Rs. %f", amount);
getch ();
/* Flow Chart to read the name of the user, number of units consumed & print out the charges*/
4. Write a C Program to display the following by reading the number of rows as input.
1
121
12321
1234321
---------------------------
nth row
Terminator
Start
Read n
i=0 F
i++
i<n
Read arr[i]
i=0 F
i++
i<n
Print arr[i]
Read key
Initialize
low = 0
high = n-1
while F
low <= high
mid = (low+high)/2
F
(cond = strcmp(arr[mid], key)) == 0
F
cond < 0
T
low = mid + 1
high = mid - 1
B Stop
F
c[i][j] = 0
n != p
k=0 F
T k++
k<n
Print "Product cannot T
be computed"
c[i][j]=c[i][j]+a[i][k]*b[k][j]
A
i=0 F
i++
i<m
T
i=0 F
j=0 F i++
j++ i<m
j<n
T
T
Read a[i][j] j=0 F
j++
j<n
T
Print a[i][j]
i=0 F
i++ B
i<p
T
j=0 F
j++
j<q C
T
Read b[i][j]
i=0 F
i++
i<p
T
j=0 F
j++
j<q
T
Print b[i][j]
i=0 F
i++
i<m
T
j=0 F
j++
j<q
T
Print c[i][j]
A Stop
#include<stdio.h>
#include<conio.h>
void main( )
{
int i,j,k,m,n,p,q,a [10][10],b [10][10],c [10][10];
clrscr( );
printf ("enter the size of matrix A\n");
scanf ("%d %d", &m, &n);
printf ("enter the size of matrix B\n");
scanf("%d %d",&p,&q);
if(n! =p)
{
printf("the product cannot be computed");
getch( );
exit(0);
}
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];
}
}
}
7.Compute sin(x)/cos(x) using Taylor series approximation. Compare your result with the
built-in library function. Print both the results with appropriate inferences.
Algorithm:
Step 1: Start
Step 2:readn , degree // Read number of terms and x value in degree
Step 3: xdegree*3.142/180 //Convert x into radius.
termx
sumterm
Step 4: //Calculate Sin series
for i=3 to n [ increment by 2 ]
term -term*x *x/(i*(i-1))
sumsum+term
end for
Step 5://compare results
Write sum
Write sin(x )
Step 6: Stop
Start
Read n, degree
x degree*3.142/180
term x
sum term
i=3 F
i=i+2
i<=n
term -term*x*x/i*(i-1)
sum term+sum
Stop
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
float term,degree,x,sum;
int i,n;
clrscr( );
printf("\n enter the value in n\n");
scanf("%d",&n);
printf("\n enter the value in degree\n");
scanf("%f",°ree);
x=degree*3.142/180;
term=x;
sum=term;
for(i=3;i<=n;i=i+2)
{
term=-term*x*x/(i *(i-1));
sum=sum+term;
}
printf("the User define sin(x) value is=%f\n",sum);
printf("the built-in sin(x) value is=%f\n",sin(x));
getch( );
}
/*Flow Chart to sort the give set of N numbers using Bubble Sort*/
Start
Read n
A
i=0 F
i++
i<n
Print "Sorted Array is"
T
Read a[i]
i=0 F
i=0 F i++
i++
i<n i<n
T T
print a[i]
print a[i]
i=0 F
i++
i<n
Stop
T
A
j=n-1 F
j--
j>i
T
F
a[j] < a[j-1]
temp= a[j]
a[j] = a[j-1]
a[j-1] = a[j]
/*A Program to sort the give set of N numbers using Bubble Sort */
#include<stdio.h>
#include<conio.h>
void main()
{
int a [10], n,i, j, temp;
clrscr();
printf(“enter the size of an array\n”);
scanf(“%d”,&n);
printf(“\n enter the elements of an array\n”);
for(i=0;i<n;i++)
scanf(“%d”,&a[i]);
for(i=0;i<n-1;i++)
{
for(j=n-1;j>i;j--)
{
if(a[j]<a[j-1])
{
temp=a[j];
a[j]=a[j-1];
a[j-1]=temp;
}
}
}
printf(“\n the elements of a sorted array are\n”);
for(i=0;i<n;i++)
printf(“%d\n”,a[i]);
getch();
}
9.Write functions to implement string operations such as compare, concatenate, and find string
length. Use the parameter passing techniques.
/*A Program to implement string operations such as compare, concatenate, string length*/
#include<stdio.h>
#include<conio.h>
int strlength(char s [50]);
int strcompare(char s1[50],char s2[50]);
void strconcatenate(char s1[50],char s2[50]);
void main()
{
char str1[150],str2[150];
int len;
clrscr();
printf("Enter two string\n");
gets(str1);
gets(str2);
len=strlength(str1);
printf("String length of first string=%d\n",len);
len=strlength(str2);
printf("String length of second string=%d\n",len);
strconcatenate(str1,str2);
if(strcompare(str1,str2)==0)
printf("\nCompared Strings are same");
else
printf("\nCompared Strings are not same");
getch();
}
else
return 1;
}
return 0;
}
void strconcatenate(char s1[50],char s2[50])
{
int i=0;
int l=strlength(s1);
while(s2[i]! ='\0')
{
s1[l]=s2[i];
i++;
l++;
}
s1[l]='\0';
printf("\nConcatenation of strings are %s",s1);
10. Implement structures to read, write, compute average- marks and the students scoring
above and below the average marks for a class of N students.
/*A Program to Implement structures to read, write, compute average- marks and the students
scoring above and below the average marks for a class of N students*/
#include<stdio.h>
#include<conio.h>
void main()
{
struct student
{
int rollno;
char name [20];
int marks;
}s[100];
int i,n;
float avg=0;
clrscr();
printf(“enter the N number of students\n”);
scanf(“%d”,&n);
for(i=0;i<n;i++)
{
printf(“\n student %d details are:\n”,i+1);
printf(“\enter the roll no\n”);
scanf(“%d”,&s[i]. rollno);
printf(“enter the name \n”);
scanf(“%s”, s[i].name);
printf(“Marks of the student:\n”);
scanf(“%d”,&s[i]. marks);
avg=avg+s[i]. marks;
}
avg=avg/n;
printf(“\n the average marks for the class is %f:”,avg);
for(i=0;i<n;i++)
{
printf(“\n student %d details are:”,i+1);
printf(“\n Roll no of the student:\n”);
printf(“%d”,s[i].rollno);
printf(“\n Name of the student:\n”);
printf(“%s”, s[i].name);
printf(“Marks of the student:\n”);
printf(“%d”,s[i].marks);
if(s[i].marks> avg)
printf(“ The student has scored above average Marks\n”);
else
printf(“ The student has scored below average Marks\n”);
}
getch();
}
11. Develop a program using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers.
Algorithm:
Step 1: Start
Step 2: read n //number of elements.
Step 3: readx[i]
for i 0 to n [increment by 1 ]
read x[i]
end for
Step 4: assign array to pointer
ptr=x
Step 5: for i<0 to n
Sumsum + *ptr
End for
Step 6: calculate mean
meansum/n
Step 7: for i=0 to n [increment by 1 ]
Stddiv_sumstddivsum+ pow((*ptr-mean),2)
End for
Step 8: stddiv sqrt(stddiv_sum/n)
Step 9: writesum
writemean
writestddiv
Step 10:Stop
/*A Program to using pointers to compute the sum, mean and standard deviation of all
elements stored in an array of n real numbers*/
#include <stdio.h>
#include <math.h>
#define MAXSIZE 10
void main()
{
float x[MAXSIZE];
int i, n;
float *ptr, average, variance, stddev, sum = 0, sum1 = 0;
printf("Enter the value of N \n");
scanf("%d", &n)
printf("Enter %d real numbers \n", n);
for (i = 0; i < n; i++)
{
scanf("%f", &x[i]);
}
ptr = x;
variance = sum1 / n;
stddev= sqrt(variance);
printf(“\n Sum of all the numbers=%f\n”,sum);
printf("Average of all elements = %.2f\n", average);
printf("variance of all elements = %.2f\n", variance);
printf("Standard deviation = %.2f\n", stddev);
getch();
}
12. Write a C program to copy a text file to another, read both the input file name and target file
name.
Algorithm :
Step 1: Start
Step 2: FILE *fp1 , *fp2 ,*fp3
fp1 fopen(“name.text”,”r”)
fp2 fopen(“USN.text”,”r”)
fp3 fopen(“output.text”,”w”)
Step 3:if (fp1=NULL or fp2== NULL)
Write unable to open a file
Goto step 5
Step4: printf("students name \t\t USN \n");
fprintf(fp3,"STUDENT NAME\t USN\n");
Step 5:while(fgets(s1 , 80 ,fp1)!==NULL and fgets(s2 , 80 ,fp2)!==NULL))
fprintf(fp3,"%s\t%s\n",s1,s2);
printf("%s\t %s\n",s1,s2);
Step 6:Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp1,*fp2,*fp3;
char s1[80],s2[80];
clrscr();
fp1=fopen("name.txt","r");
fp2=fopen("usn.txt","r");
fp3=fopen("output1.txt","w");
if(fp1==NULL||fp2==NULL)
{
printf("unable to open");
getch();
exit(0);
}
printf("students name \t\t USN \n");
fprintf(fp3,"STUDENT NAME\t\t USN\n");
while((fscanf(fp1,"%s",s1)!=EOF)&&(fscanf(fp2,"%s",s2)!=EOF))
{
fprintf(fp3,"%s\t%s\n",s1,s2);
printf("%s\t %s\n",s1,s2);
}
fcloseall();
getch();
}