22POP13LAB
22POP13LAB
ALGORITHM
Step 1: Start
Step 2: Read the value of operator op.
Step 3: Read the values of num1 and num2.
Step 4: Verify op value by using switch statement
if op=’+’ then goto step 5 (or)
if op=’-’ then goto step 6 (or)
if op=’*’ then goto step 7 (or)
if op=’/’ then goto step 8 (or)
otherwise goto step 9
Step 5: if op=’+’ then compute result=num1+num2
print result and goto step 10
Step 6: if op=’-’ then compute result=num1-num2
print result and goto step 10
Step 7: if op=’*’ then compute result=num1*num2
print result and goto step 10
Step 8: if op=’/’ then compute divresult=num1/num2
print divresult and goto step 10
Step 9: if op value is other than above options then
print “Invalid input, Try again” goto step 10
Step 10: Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int num1,num2,result;
float divresult;
char op;
clrscr();
printf("Enter the operation to be done \n");
scanf("%c",&op);
printf("Enter the value of num1 and num2 \n");
scanf("%d%d",&num1,&num2);
switch(op)
{
case '+': result = num1 + num2;
printf(“Addition=%d\n”,result);
break;
case '-': result = num1 - num2;
printf(“Subtraction=%d\n”,result);
break;
case '*': result = num1 * num2;
printf(“Multiplication=%d\n”,result);
break;
case '/': divresult = num1 / (float)num2;
printf(“Division=%.2f\n”,divresult);
break;
default: printf("Invalid Input ,Try Again\n");
}
getch();
}
OUTPUT
First Run:
Enter the operation to be done
+
Enter the value of num1 and num2
10
20
Addition=30
Second Run:
Enter the operation to be done
-
Enter the value of num1 and num2
10
20
Subtraction=-10
Third Run:
Enter the operation to be done
*
Enter the value of num1 and num2
20
30
Multiplication=600
Fourth Run:
Enter the operation to be done
/
Enter the value of num1 and num2
40
50
Division=0.80
Fifth Run:
Enter the operation to be done
@
Enter the value of num1 and num2
40
50
Invalid Input ,Try Again
RESULT: Thus, the program to implement the simple calculator has been executed
successfully and the output was verified.
Laboratory Program 2
AIM: To write a C Program to Compute the roots of a quadratic equation by accepting the
coefficients
ALGORITHM
Step 1: Start
Step 2: Read the value of non-zero coefficient a.
Step 3: If a is zero goto Step 10. Otherwise read the values of coefficients b and c.
Step 4: Compute the value of discriminant (disc) using the formula (b*b) - (4*a*c).
Step 5: Check if disc is equal to 0. If true, then go to Step 6. Otherwise, goto Step 7
Step 6: Compute the equal roots.
root1 = root2 = (-b)/(2*a)
Print the values of roots, root1 and root2. Go to Step 10.
Step 7: Check if disc is greater than zero or not. If true, then go to Step 8. Otherwise, goto
Step 9.
Step 8: Compute the real and distinct roots
root1 = (-b+sqrt(disc))/(2*a)
root2 = (-b-sqrt(disc))/(2*a)
Print the values of roots, root1 and root2. Go to Step 10.
Step 9: Compute the complex and imaginary roots.
Compute the real part, realp = (-b)/(2*a)
Compute the imaginary part, imagp = sqrt(-disc)/(2*a)
Print roots as
root1 = realp + imagp
root2 = realp – imagp
Step 10: Stop.
PROGRAM
#include <stdio.h>
#include <conio.h>
#include <math.h>
#include <stdlib.h>
void main()
{
float a,b,c,root1,root2,realp,imagp,disc;
clrscr();
if(a == 0)
{
printf("\n Invalid input...Retry again");
exit(0);
}
printf(" Enter the value of coefficients b and c:\n ");
scanf("%f%f",&b,&c);
disc = b*b-4*a*c; // compute discriminant
if(disc == 0)
{
printf("The roots are real and equal\n");
root1 = root2 = -b / (2.0*a);
printf(" Root1 = Root2 = %.2f\n", root1);
}
else
{
if(disc > 0)
{
printf("The roots are real and distinct\n");
root1 = (-b + sqrt(disc))/(2.0*a);
root2 = (-b - sqrt(disc))/(2.0*a);
printf("Root1 = %.2f\n", root1);
printf("Root2 = %.2f\n", root2);
}
else
{
printf("The roots are complex\n");
realp = -b/(2.0*a);
disc=-disc;
imagp = sqrt(disc)/(2.0*a);
printf("Root1 = %.2f + i%.2f\n",realp,imagp);
printf("Root2 = %.2f - i %.2f\n",realp, imagp);
}
}
getch();
}
OUTPUT
First Run:
Enter the value of coefficient a: 1
Enter the value of coefficients b and c:
44
The roots are real and equal
Root1 = Root2 = -2.00
Second Run:
Enter the value of coefficient a: 1
Enter the value of coefficients b and c:
-7 10
The roots are real and distinct
Root1 = 5.00
Root2 = 2.00
Third Run:
Enter the value of coefficient a: 2
Enter the value of coefficients b and c:
-3 6
The roots are complex
Root1 = 0.75 + i 1.56
Root2 = 0.75 - i 1.56
Fourth Run:
Enter the value of coefficient a: 0
RESULT: Thus, the program to compute the roots of quadratic equation has been executed successfully
and the output was verified.
Laboratory Program 3
AIM
To write a C program to read the Name of the User, Number of Units Consumed and
Print out the charges.
ALGORITHM
Step 1: Start
Step 2: Read the name of customer and the unit consumed by the customer.
Step 3: Check if the unit consumed is greater than 1 and less than 200, if true goto
step 4 else goto step 5.
Step 5: if unit is greater than 200 and less than 300,if true goto step 6 else goto step 7
Step 8: Check if the amt is greater than or equal to 400, if true goto step 9 otherwise
Step 10:. Print the amount charged and goto step 11.
#include <stdio.h>
#include<conio.h>
void main()
{
char name[10];
clrscr();
printf("Enter your name and unit Consumed:");
scanf("%s %f",name,&unit);
if(unit<=200)
amt=unit*0.80+100;
else if((unit>200)&&(unit<=300))
amt=200*0.80+((unit-200)*0.90)+100;
else
amt=200*0.80+100*0.90+((unit-300)*1)+100;
if(amt>400)
amt=1.15*amt;
printf("Name: %s\n Unit=%f \n charge=%f ",name,unit,amt);
getch();
}
OUTPUT
First Run:
Enter your name and unit Consumed: Siri 52Name: Siri
Unit=52 charge=141.600000Second Run:
Enter your name and unit Consumed: Rajesh 460Name: Rajesh
Unit=460 charge=586.500000
RESULT: -
Thus, the program to read the Name of the User, Number of Units Consumed and Print out the
charges has been executed successfully and the output was verified.
Laboratory Program 4
AIM
To write a C program to display the pattern by reading the number of rows as input.
ALGORITHM
Step 1: Start
Step 2: Read the number of rows.
Step 3: Let i=0, if i<=n, go to step 14
Step 4: Initialize j=1 if j<=n-i, Go to step 8
Step 5: Print blank spaces
Step 6: Increment j (ie. j=j+1) Go to step 5
Step 7: Initialize j=1 if j<=i Go to step 11
Step 8: Display number in ascending order up to middle.
Step 9: Increment j (ie. j=j+1) Go to step 8
Step 10: initialize j=i-1 if j>=1 Go to step 14
Step 11: Display number in reverse order after middle
Step 12: Increment j (ie. j=j+1) Go to step 13
Step 13: Increment i (ie. i=i+1) Go to step 3
Step 14: Stop.
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int i,j,n;
clrscr( );
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0; i<=n;i++)
{
for(j=1;j<=n-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf("%d",j);
}
for(j=i-1;j>=1;j--)
{
printf("%d",j);
}
printf("\n");
}
getch();
}
OUTPUT
First Run:
Second Run:
1
121
12321
1234321
123454321
12345654321
RESULT: -
Thus, the program to display the pattern by reading the number of rows as input has been
executed successfully and the output was verified.
Laboratory Program 5
ALGORITHM
Step 1: Start
Step 2: Read size of the array n
Step 3: Read the list of elements in sorted order a[i].
Step 4: Read the key element in key
Step 5: initialize low=0 and high= n-1
Step 6: Check if low is less than or equal to high. If condition is true, goto step 7,
otherwise goto Step 11
Step 7: find the middle element.i.e. mid=(low+high)/2;
Step 8: if key element is equal to mid element, then initialize loc value, loc = mid+1;
otherwise goto step 9
Step 9: Check if key element is less than mid element is true, then initialize high=mid-1
then goto step 6, if it is false goto step10.
Step 10: Check if key element is greater than mid element is true, then initialize
low=mid+1 then goto step 6.
Step 11: Check if loc value is greater than zero then print the search is successful then
goto step 13, otherwise goto step 12.
Step 12: print search is unsuccessful, then goto step 13.
Step 13: Stop
PROGRAM
#include <stdio.h>
#include <conio.h>
void main()
{
int n, a[100], i, key, high, low, mid, loc=-1;
clrscr( );
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter the elements of array in sorted order\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("Enter the key element to be searched\n");
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key= =a[mid])
{
loc = mid+1;
break;
}
else
{
if(key<a[mid])
high=mid-1;
else
low=mid+1;
}
}
if(loc>0)
printf("\n The element %d is found at %d ",key,loc);
else
printf("\nThe search is unsuccessful");
getch();
}
OUTPUT
First Run:
Enter the size of the array 5
Enter the elements of array in sorted order 10
20
30
40
50
Enter the element to be searched 40
The element 40 is found at 4
Second Run:
Enter the size of the array
4
Enter the elements of array in sorted order
4
6
8
9
Enter the key element to be searched
2
The search is unsuccessful
RESULT: -Thus, the program to implement the binary search on integers has been executed successfully
and the output was verified.
Laboratory Program 6
AIM: To develop a program to introduce 2D Array manipulation and implement Matrix multiplication
and ensure the rules of multiplication are checked.
ALGORITHM:
#include<stdio.h>
#include<conio.h>
void main()
{
int a[5][5],b[5][5],c[5][5],m,n,p,q,i,j,k;
clrscr();
printf("Enter the size of first matrix\n");
scanf("%d %d",&m,&n);
printf("Enter the size of second matrix\n");
scanf("%d %d",&p,&q);
if(n!=p)
printf(“Matrix multiplication is not possible”);
else
{
printf("Enter the elements of first matrix\n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&a[i][j]);
printf("Enter the elements of the second matrix\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("\n A- matrix is\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
}
printf("\n B- matrix is\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
}
printf("The product of two matrices is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
}
}
getch();
}
OUTPUT:
Enter the size of first matrix
23
Enter the size of second matrix
32
Enter the elements of first matrix
123456
Enter the elements of the second matrix
123456
A- matrix is
1 2 3
4 5 6
B - matrix is
1 2
3 4
5 6
The product of two matrices is
22 28
49 64
RESULT: -Thus, the program to implement matrix multiplication has been executed successfully and the
output was verified.
Laboratory Program 7
ALGORITHM:
Step-1: Start
Step-2: [Read the value of x in degree]- Read x
Step-3: [Initialization and Radians Conversion]
Temp = x
x=x*(3.142/180.0)
Term = x
sinx = term
n=1
Step-4: [Computer sin value]
Repeat while (term>FLT_EPSILON)
Fact = 2*n*(2*n+1)
Term = term * x * x /fact Sinx
sinx +term = n + 1n
Step-5: [Output]- Print sin(x) without using library function of Print sin(x) and with
using library function
Step-6: Stop
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<math.h>
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
f=f*i;
}
return f;
}
void main()
{
int x,n,i;
float rad, res, sum=0;
clrscr();
printf("Enter degree\n");
scanf("%d",&x);
printf("Enter number of terms\n");
scanf("%d",&n);
rad=x*3.14/180;
for(i=1;i<=n;i+=2)
{
if ((i-1)%4==0)
sum=sum+pow(rad,i)/fact(i);
else
sum=sum-pow(rad,i)/fact(i);
}
printf("Calculate sin(%d) = %f", x,sum);
printf("\nLibrary sin(%d) = %f", x,sin(rad));
getch();
}
OUTPUT
First Run:
Enter degree
30
Enter number of terms
5
Calculate sin(30) = 0.499772
Library sin(30) = 0.499770
Second Run:
Enter degree
60
Enter number of terms
2
Calculate sin(60) = 0.866029
Library sin(60) = 0.865760
RESULT: -Thus, the program to calculate sin(x) has been executed successfully and the output was
verified.
Laboratory Program 8
AIM: To develop a program to sort the given set of N numbers using Bubble sort.
ALGORITHM
ALGORITHM
Step 1: Start
Step 2: [Input two source strings]
read source1,source2
Step 3: [Calculate length1 of source1 by calling the user defined function,strlength();
Repeat the same for length2 of source2 ]
length1=strlength(source1)
length2=strlength(source2)
Step 4: [Ouput length1,length2]
Print length1,length2
Step 5: [Compare the two strings by calling the user defined function, strcompare()]
k=strcompare(source1,source2)
Step 6: [check k, to find the whether the strings are same or not]
if(k==0)
print Both strings are same
else
print strings are different
end if
Step 7: [Concatenate two strings by calling the user defined function, strconcat() and the concatenated
string is stored in source1]
strconcat(source1,source2)
print source1
Step 8: Stop
User Defined Function - strlength()
Step 1: Start
Step 2: [set i=0]
i=0
Step 3: [receive the source string as str, read character by character, count one by one until we reach
NULL character]
while(str[i]!=’\0')
i++ end while
Step 4: [return i to the calling function]
return i
User Defined Function - strcompare( )
Step 1: Start
Step 2: [set i=0]
i=0
Step 3: [Receive both the source strings as str1 and str2, read character by character until they match, if
the matched character is a NULL then go out of while loop, when any unmatched character then go out of
loop]
while(str1[i] = = str2[i])
if(str1[i] = = ’\0')
break
end if
i++
end while
Step 4: [calculate k]
k=str1[i]-str2[j]
Step 5: [return i to the calling function]
return k
User Defined Function - strconcat( )
Step 1: Start
Step 2: [set i=0]
i=0
Step 3: [Receive both the source strings as str1 and str2, calculate length of str1 using strlength() as l]
l=strlength(str1)
Step 4: [read str2 character by character and store it from the end of str1]
while(str2[i]!=’\0')
str1[l+i]=str2[i]
i++
end while
Step 5: [return to the calling function]
return;
PROGRAM
i++;
}
str1[l+i]=’\0';
}
int strcompare(char str1[50],char str2[50])
{
int i=0, k;
while(str1[i]==str2[i])
{
if(str1[i]==’\0')
break;
i++;
}
k=str1[i]-str2[i];
return k;
}
void main()
{
char source1[50],source2[50],dest[50];
int length1,length2,k;
clrscr();
printf(“\n Enter the source string 1:”);
gets(source1);
printf(“\n Enter the source string 2:”);
gets(source2);
length1=strlength(source1);
length2=strlength(source2);
printf(“\n string length of string 1 is %d”,length1);
printf(“\n string length of string 2 is %d”,length2);
k=strcompare(source1,source2);
if(k==0)
printf(“\n Both string are same”);
DATA STRUCTURES LAB
else
printf(“\n Both string are different”);
strconcat(source1,source2);
printf(“\n concatenated string is “);
puts(source1);
getch();
}
OUTPUT
First Run:
Enter the source string1: good
Enter the source string2: night
String length of string1 is: 4
String length of string2 is: 5
strings are different
concatenated string is: goodnight
Second Run:
Enter the source string1: good
Enter the source string2: good
String length of string1 is: 4
String length of string2 is: 4
Both strings are same
concatenated string is: goodgood
RESULT: - Thus, the program to implement string operations such as compare, concatenate, string
length has been executed successfully and the output was verified.
DATA STRUCTURES LAB
Laboratory Program 10
AIM: To write a C Program to implement structures to read, write and compute average-
marks of the students, list the students scoring above and below the average marks for a
class of N students.
ALGORITHM
Step-1: Start
Step-2: Read number of students
Step-3: For every student, read the student id, name , marks for all the subjects
Step-4: Calculate the average marks and store it in the avg field
Step-5: Print the results
Step-6: Initialise loop
Step-7: Read the average of every student
Step-8: Check for if avg>35.00
Step-9: If yes than print the result else goto next iteration
Step-10: Initialise the loop
Step-11: Read average of every student
Step-12: Check if avg<35.00
Step-13: If yes than print result else goto next iteration
Step-14: Stop
DATA STRUCTURES LAB
PROGRAM
#include<stdio.h>
#include<conio.h>
struct student
{
char usn[10];
char name[10];
int m1,m2,m3;
float avg, total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
clrscr();
printf("Enter the number of students");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
printf("\n Enter USN=");
scanf("%s",s[i].usn);
printf("\n Enter Name=");
scanf("%s",s[i].name);
printf("\nEnter the three subjects marks\n");
scanf("%d%d%d",&s[i].m1,&s[i].m2,&s[i].m3);
s[i].total=s[i].m1+s[i].m2+s[i].m3;
s[i].avg=s[i].total/3;
}
for(i=0;i<n;i++)
{
DATA STRUCTURES LAB
if(s[i].avg>=35)
printf("\n %s has scored above the average marks",s[i].name);
else
printf("\n %s has scored below the average marks",s[i].name);
}
getch();
}
OUTPUT
Enter the number of students2
Enter the detail of student 1
Enter USN=1
Enter Name=Arun
Enter the three-subject score
23 45 67
Enter the detail of student 2
Enter USN=2
Enter Name=Tharun
Enter the three-subject score
532
Arun has scored above the average marks
Tharun has scored below the average marks
RESULT: -Thus, the program to implement structures to read, write and compute average- marks of the
students, list the students scoring above and below the average marks for a class of N students has been
executed successfully and the output was verified.
DATA STRUCTURES LAB
Laboratory Program 11
PROGRAM
#include<stdio.h>
#include<conio.h>
#include<math.h>
int main()
{
int n , i;
float x[20],sum,mean;
float variance , deviation;
clrscr();
printf("Enter the value of n \n");
scanf("%d",&n);
printf("enter %d real values \n",n);
for (i=0;i<n;i++)
{
scanf("%f",(x+i));
}
sum=0;
for(i=0;i<n;i++)
{
sum= sum+*(x+i);
}
printf("sum=%f\n",sum);
mean=sum/n;
sum=0;
for(i=0;i<n;i++)
{
sum=sum+(*(x+i)-mean)*(*(x+i)-mean);
}
variance=sum/n;
deviation=sqrt(variance);
printf("mean(Average)=%f\n",mean);
printf("variance=%f\n",variance);
printf("standard deviation=%f\n",deviation);
getch();
}
Output:
Enter the value of n
5
Enter the 5 real values
3
7
23
1
4
Sum = 38.0000
Mean ( Average ) = 7.6000
Variance = 63.039997
Standard deviation = 7.9397
RESULT: -Thus, the program to implement pointers to compute the sum, mean and standard deviation of
all elements stored in an array of n real numbers has been executed successfully and the output was verified.
Laboratory Program 12
WRITE A C PROGRAM TO COPY A TEXT FILE TO ANOTHER, READ BOTH THE INPUT
FILE NAME AND TARGET FILE NAME.
AIM: To 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: Read the source file name fname1
Step 3: Open the file fname1 in read mode
Step 4: if fptr1 is equal to NULL
print " File does not found or error in opening.!!”
goto Step 12
Step 5: Read the new file name fname2
Step 6: Open the file fname2 in write mode
Step 7: if fptr2 is equal to NULL
print " File does not found or error in opening.!!"
goto Step 12
Step 8: Repeat while(1)
ch=fgetc(fptr1);
if ch is equal to EOF
break;
else
fputc(ch, fptr2);
Step 9: print “The file fname1 copied successfully in the file fname2”
Step 10: close file pointer fptr1
Step 11: close file pointer fptr2
Step 12: Stop
PROGRAM
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
void main()
{
FILE *fptr1, *fptr2;
char ch, fname1[20], fname2[20];
clrscr();
fptr1=fopen(fname1, "r");
if(fptr1==NULL)
{
printf(" File does not found or error in opening.!!");
exit(1);
}
printf(" Input the new file name : ");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
if(fptr2==NULL)
{
printf(" File does not found or error in opening.!!");
fclose(fptr1);
exit(2);
}
while(1)
{
ch=fgetc(fptr1);
if(ch==EOF)
{
break;
}
else
{
fputc(ch, fptr2);
}
}
printf(" The file %s copied successfully in the file %s. \n\n",fname1,fname2);
fclose(fptr1);
fclose(fptr2);
getchar();
}
OUTPUT:
RESULT: - Thus, the program to copy a text file to another, read both the input file name and target file
name has been executed successfully and the output was verified.