0% found this document useful (0 votes)
49 views33 pages

C Manual 22 Scheme

The document describes several lab experiments involving programming concepts like simulating a calculator, solving quadratic equations, billing calculations, number pattern generation, binary search, and matrix multiplication. Code snippets are provided to implement the algorithms for each experiment.

Uploaded by

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

C Manual 22 Scheme

The document describes several lab experiments involving programming concepts like simulating a calculator, solving quadratic equations, billing calculations, number pattern generation, binary search, and matrix multiplication. Code snippets are provided to implement the algorithms for each experiment.

Uploaded by

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

Lab Experiment No.

1: Simulation of a Simple Calculator


Algorithm: To simulate a simple calculator
Step 1: Start
Step 2: Read two numbers and arithmetic operator
Step 3: Perform any one arithmetic operation using switch case
case ‘+’:
printa+b
goto step 4
case ‘-’ :
print a-b
goto step 4
case ‘*’ :
print a*b
goto step 4
case ‘/’ :
print a/b
goto step 4
case ‘%’:
printa%b
goto step 4
default:
print ‘Invalid Arithmetic Operator’
Step 4: Stop
#include <stdio.h>
void main()
{
int a,b;
char op;
printf("Enter the arithmetic operator");
scanf("%c",&op);
printf("Enter two numbers");
scanf("%d%d",&a,&b);
switch(op)
{
case '+':
printf("Sum is %d",a+b);
break;
case '-':
printf("Difference is %d",a-b);
break;
case '*':
printf("Product is %d",a*b);
break;
case '/':
printf("Quotient is %f",(float)a/b);
break;
case '%':
printf("Remainder is %d",a%b);
break;
default:
printf("Invalid Arithmetic Operator");
}
}
Out Put :
Enter the arithmetic operator +
Enter two numbers 5 5
Sum is 10
Enter the arithmetic operator -
Enter two numbers 10 20
Difference is -10
Enter the arithmetic operator *
Enter two numbers 8 9
Product is 72
Enter the arithmetic operator %
Enter two numbers 8 7
Reminder is 1
Enter the arithmetic operator /
Enter two numbers 45 54
Quotient is 0.833333
Enter the arithmetic operator &
Enter two numbers 7 8
Invalid Arithmetic Operator
Lab Experiment 2:

Compute the roots of a quadratic equation by accepting the coefficients. Print appropriate messages.

Algorithm: To compute the roots of a quadratic equation.

Step 1: Start
Step 2: Read the coefficients
Step 3: if (a==0 && b== 0) then
print‘Invalid inputs’
goto step 8
Step 4: if(a==0) then
root1=-c/b
print ‘Linear roots’, root1
goto step 8
Step 5: d=b*b-4*a*c
if(d==0) then
root1=root2=-b/(2*a)
print ‘Roots are equal’, root1, root2
goto step 8
Step 6: if (d>0)
root1=(-b+(sqrt(d)))/ (2*a)
root2=(-b-(sqrt(d)))/ (2*a)
print ‘Roots are distinct’,root1,root2
goto step 8
Step 7: print ‘Roots are imaginary’
Step 8: Stop
#include <stdio.h>
#include<math.h>
int main()
{
float a,b,c,d,root1,root2,rpart,ipart;
printf("Enter the coefficients");
scanf("%f%f%f",&a,&b,&c);
if(a==0 && b==0)
printf("Invalid inputs");
else if(a==0)
{
printf("Linear roots\n");
root1=-c/b;
printf("Root is %.3f",root1);
}
else
{
d=b*b-4*a*c;
if(d==0)
{
printf("Roots are equal\n");
root1=root2=-b/(2*a);
printf("Root1=%.3f\n",root1);
printf("Root2=%.3f",root2);
}
else if(d>0)
{
printf("Roots are distinct\n");
root1=(-b+(sqrt(d)))/(2*a);
root2=(-b-(sqrt(d)))/(2*a);
printf("Root1=%.3f\n",root1);
printf("Root2=%.3f",root2);
}
else
{
printf("Roots are imaginary\n");
rpart=-b/(2*a);
ipart=sqrt(fabs(d))/(2*a);
printf("Root1=%.3f+i%.3f\n",rpart,ipart);
printf("Root2=%.3f-i%.3f",rpart,ipart);
}
}
return 0;
}

Out Put :
Enter the coefficients 1 -4 4
Roots are equal
Root1=2.000
Root2=2.000

Enter the coefficients 1 -5 6


Roots are distinct
Root1=3.000
Root2=2.000

Enter the coefficients 1 2 3

Roots are imaginary


Root1=1.000+i1.414
Root2=1.000-i1.414
Lab Experiment 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 thecharges.

Algorithm: To compute Electricity Bill

Step 1: Start

Step 2: Read name, unit

Step 3: Calculate the Electricity Bill

billAmount = 100

if(unit<=200) then

billAmount= billAmount+(unit*0.80)

goto Step 4

else if(unit>200 && unit<=300) then

billAmount= billAmount+(200*0.80)+((unit-200)*0.90);

goto Step 4

else if(unit > 300) then

billAmount= billAmount+(200*0.80)+(100*0.90)+((unit-300)*1);

goto Step 4

if(billAmount>400) then

billAmount=billAmount+(billAmount*0.15);

goto Step 4

Step 4: print name, billAmount


Step 5: End
#include <stdio.h>
void main()
{
char name[10];
int unit;
float billAmount;
printf("Enter the customer name: \n");
scanf("%s",name);
printf("Enter the total units consumed:\n");
scanf("%d", &unit);
billAmount = 100;
if(unit<=200)
billAmount= billAmount+(unit*.80);
else if(unit>200 && unit<=300)
billAmount= billAmount+(200*0.80)+((unit-200)*0.90);
else if(unit > 300)
billAmount= billAmount+(200*0.80)+(100*0.90)+((unit-300)*1);
if(billAmount>400)
billAmount=billAmount+(billAmount*0.15);
printf("Customer name: %s\n", name);
printf("Total units consumed: %d\n", unit);
printf("Total Bill Amount: %.2f\n", billAmount);
}

Out Put :
Enter the customer name: Shambu
Enter the total units consumed: 493
Customer name:Shambu
Total units consumed:493
Total Bill Amount:624.45
Lab Experiment 4:

Write a C Program to display the following by reading the number of rows as input:

121

12321

1234321

Algorithm: To print number in specified format

Step 1: Start

Step 2: Input the number of rows

Step 3: Print the numbers in specified format using nested loop

for i=1 to row

for j= 1 to row-i

print" "

End of inner loop

forj=1 to i

print j

End of inner loop

forj=i-1 down to 1

print j

End of inner loop

print new line character

End of outer loop


Step 4: End
#include<stdio.h>
void main()
{
Int i,j,row;
` printf("Enter number of rows: ");
scanf("%d",&row);
for(i=1;i<=row;i++)
{
for(j=1;j<=row-i;j++)
{
printf(" ");
}
for(j=1;j<=i;j++)
{
printf(" %d", j);
}
for(j=i-1;j>=1;j--)
{
printf(" %d", j);
}
printf("\n");
}}

Out Put :
Enter number of rows :3
1
121
12321
Lab Experiment 5:

Implement Binary Search on Integers

Algorithm: Implement Binary Search on Integers


Step 1: Start
Step 2: Read n
Step 3: Repeat for i=0 to n-1
Read a[i]
Step 4: read key
Step 5: Initialize low =0, high = n-1
Step 6: Repeat through step 6 while (low <= high)
mid = (low+ high)/2
if(key==a[mid])
found=1
else if(key>a[mid])
low=mid+1
else
high=mid-1
end while
Step 7: if(found ==1)
print “Element found”
else
print” Element not found”
Step 8: End
#include<stdio.h>
void main()
{
int i,n,a[10],mid,low,high,key, found=0;
printf("\n Enter the number of elements:\n");
scanf("%d", &n);
printf("Enter the array element in the ascending order:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
printf("\n 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])
{
found=1;
break;
}
else if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(found ==1)
printf("Item found in position : %d",mid+1);
else
printf("\n Item not found:\n");
}

Out Put :

Enter the number of elements : 5


Enter the array element in the ascending order :
10
20
30
40
50
Enter the key element to be searched: 30
Item found in position 3
Enter the number of elements : 5
Enter the array element in the ascending order :
10
20
30
40
50
Enter the key element to be searched: 100
Item not found
Lab Experiment: 6

Implement Matrix multiplication and validate the rules of multiplication.

Algorithm: To implement Matrix multiplication

Step 1: Start
Step 2: Read the order of the matrix A
Read m,n
Step 3: Read the order of the matrix B
Read p,q
Step 4: To check for compatibility condition of matrix multiplication
if(n!=p)
print “Matrix multiplication not possible”
goto step 9
Step 5: Read the elements of matrix A
Repeat through step 5 for i=0 to m-1
Repeat for j=0 to n-1
Read A[i][j]
Step 6: Read the elements of matrix B
Repeat through step 6 for i=0 to p-1
Repeat for j=0 to q-1
Read B[i][j]
Step 7: Calculate the product of two given matrices
Repeat through step 7 for i=0 to m-1
Repeat for j=0 to q-1
C[i][j]=0
Repeat for k=0 to n-1
C[i][j]=C[i][j]+A[i][k]*B[k][j]
Step 8: Print the resultant matrix
Repeat step 8 for i0 to m-1
Repeat for j0 to n-1
Print C[i][j]
Step 9: Stop
#include<stdio.h>
void main()
{
int m,n,p,q,i,j,k,A[10][10],B[10][10],C[10][10];
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("Matrix multiplication is not possible\n");
else
{
printf("Enter the elements of matrix A \n");
for(i=0;i<m;i++)
for(j=0;j<n;j++)
scanf("%d",&A[i][j]);
printf("Enter the elements of 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 resultant matrix C is \n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
printf("%d\t",C[i][j]);
printf("\n");
}
}
}

Out Put :
Enter the order of matrix A
33
Enter the order of matrix B
33
Enter the elements of matrix A
111
111
111
Enter the elements of matrix B
111
111
111
The resultant matrix C is
3 3 3
3 3 3
3 3 3

Enter the order of matrix A


32
Enter the order of matrix B
32
Matrix multiplication is not possible
Lab Experiment: 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: To compute sin(x) using Taylor series.

Step 1: Start
Step 2: Input the value of degree
Read degree
Step 3: Initialization
x=degree*(PI/180)
nume=x
fact=1
i=2
Step 4: Calculate the sum
term=nume/fact
nume=-nume*x*x
fact=fact*i*(i+1)
sum=sum+term
i=i+2
Repeat the step 4 until while (fabs(term)>=0.00001)
Step 5: Print the result
Print sum
Step 6: End
#include<stdio.h>
#include<math.h>
#define PI 3.142
void main()
{
int i,degree;
float x,sum=0,term,nume,fact;
printf("Enter value of degree \n");
scanf("%d",&degree);
x=degree*(PI/180);
nume=x;
fact=1;
i=2;
do
{
term=nume/fact;
nume=-nume*x*x;
fact=fact*i*(i+1);
sum=sum+term;
i=i+2;
}while(fabs(term)>=0.00001);
printf("The sine of %d is %.3f",degree,sum);
printf("Using inbuilt function sin(%d) is %.3f",degree,sin(x));
}

Out Put:
Enter value of degree:60
The sine of 60 is 0.866
Using inbuilt function sin(60) is 0.866
Lab Experiment: 8

Sort the given set of N numbers using Bubble sort.

Algorithm: to sort the given numbers using Bubble sort


Step 1: Start
Step 2: Input the size of the array
Read n
Step 3: Input the array elements
Repeat for i=0 to n-1
Read a[i]
Step 4: Print the given array
Repeat for i=0 to n-1
print a[i]
Step 5: Repeat through step 5 for i1 to n-1
Repeat for j0 to n-i-1
if(a[j]>a[j+1])
tempa[j]
a[j] a[j+1]
a[j+1]temp
Step 6: Print the sorted array
Repeat for i 0 to n-1
print a[i]
Step 7: Stop
#include<stdio.h>
void main()
{
int a[100],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter the %d elements of array\n",n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("The Input array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
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;
}
}
}
printf("\nThe sorted array is\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
}
Out Put :
Enter the number of elements: 5
Enter the 5 elements of array
100
10
550
250
50
The Input array is
100
10
550
250
50

The sorted array is


10
50
100
250
550
Lab Experiment: 9

Write functions to implement string operations such as compare, concatenate and find string length. Use
parameter passing techniques.

#include <stdio.h>
int length(char []);
int compare(char [], char []);
voidconcat(char [], char []);
void main()
{
char s1[1000], s2[1000];
int l1,l2;
printf("Enter a string1\n");
gets(s1);
printf("Enter a string2\n");
gets(s2);
l1 = length(s1);
l2 = length(s2);
printf("Length of %s = %d\n", s1, l1);
printf("Length of %s = %d\n", s2, l2);
if (compare(s1, s2) == 0)
printf("Strings are Equal\n");
else
printf("Strings are Unequal\n");
concat(s1, s2);
printf("After concatenation string is :%s", s1);
}
int length(char s1[])
{
int c = 0;
while (s1[c] != '\0')
c++;
return c;
}

int compare(char s1[], char s2[])


{
int c = 0;
while (s1[c] == s2[c])
{
if (s1[c] == '\0' || s2[c] == '\0')
break;
c++;
}
if (s1[c] == '\0' && s2[c] == '\0')
return 0;
else
return 1;
}

voidconcat(char s1[], char s2[])


{
int c, d; c = 0;
while (s1[c] != '\0')
{
c++;
}
d = 0;
while (s2[d] != '\0')
{
s1[c] = s2[d];
d++;
c++;
}
s1[c] = '\0';
}

Out Put:

Enter a string1 :
hello
Enter a string2:
hello
Length of hello=5
Length of hello=5
Strings are Equal
After concatenation string is :hellohello

Enter a string1 :
hello
Enter a string2:
yenepoya
Length of hello=5
Length of hello=8
Strings are Unequal
After concatenation string is :helloyenepoya
Lab Experiment: 10

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.

#include <stdio.h>
struct student
{
char usn[50];
char name[50];
int marks;
} s[10];
void main()
{
int i,n,countav=0,countbv=0;
floatsum,average;
printf("Enter number of Students\n");
scanf("%d",&n);
printf("Enter information of students:\n");
for(i=0; i<n;i++)
{
printf("Enter USN: ");
scanf("%s",s[i].usn);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter marks: ");
scanf("%d",&s[i].marks);
printf("\n");
}
printf("Displaying Information:\n\n");
for(i=0; i<n; i++)
{
printf("\nUSN: %s\n",s[i].usn);
printf("Name: %s\n ", s[i].name);
printf("Marks: %d",s[i].marks);
printf("\n");
}
for(i=0;i<n;i++)
{
sum=sum+s[i].marks;
}
average=sum/n;
printf("\nAverage marks: %f",average);
for(i=0;i<n;i++)
{
if(s[i].marks>=average)
countav++;
else
countbv++;
}
printf("\nTotal No of students above average= %d",countav);
printf("\nTotal No of students below average= %d",countbv);
}

Out Put :
Enter number of Students 2
Enter information of students
Enter USN :01
Enter name :pooja
Enter marks :50

Enter USN :02


Enter name :praja
Enter marks :67

Displaying Information:

Enter USN :01


Enter name :pooja
Enter marks :50

Enter USN :02


Enter name :praja
Enter marks :67

Average marks :58.500000


Total No of students above average=1
Total No of students below average=1
Lab Experiment: 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

#include<stdio.h>
#include<math.h>
void main()
{
float a[10],*ptr,mean,std,sum=0,sumstd=0;
int n,i;
printf("Enter a value of n\n");
scanf("%d",&n);
printf("Enter real values\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\
n",sum); printf("Mean=%.3f\n",mean);
printf("Standard Deviation =%.3f",std);
}

Out Put :
Enter a value of n
5
Enter real values
34 88 32 12 10
Sum=176.000
Mean=35.200
Standard Deviation=28.188
Lab Experiment: 12

Write a C program to copy a text file to another, read both the input file name and target file name.

#include <stdio.h>
void main()

{
FILE *fptr1, *fptr2;
charch, fname1[20], fname2[20];
printf(" Enter the source file name : ");
scanf("%s",fname1);
fptr1=fopen(fname1,"r");
printf(" Enter the new file name : ");
scanf("%s",fname2);
fptr2=fopen(fname2, "w");
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);
}

Out Put :
Enter the source file name :abc.txt

Enter the new file name :xyz.txt

The file abc.txt copied successfully in the file xyz.txt

You might also like