LAB MANUAL CPL 21scheme
LAB MANUAL CPL 21scheme
Program:
#include<stdio.h>
{
int l, b, area, peri;
printf("Enter the length and the breadth\n");
scanf("%d%d", &l, &b);
area = l * b;
peri = 2 * (l + b);
printf("The area is %d\n", area);
printf("The perimeter is %d\n', peri);
}
Output
The area is 20
The perimeter is 18
Algorithm:
Step-1: Start
Step-2: Read the variables a and b
Step-3: Options Display
Step-4: Read the input choice
Step-5: If Choice is
1 then execute step 6
2 then execute step 7
3 then execute step 8
4 then execute step 9
Other than above mentioned choice than execute step 10
Step-6: Addition of a and b
Step-7: Subtraction of a and b
Step-8: Multiplication of a and b
Step-9: Division of a and b
Step-10: Invalid choice
Step-11: Print the result
Step-12: Stop
Program:
#include<stdio.h>
void main()
{
int a,b;
int op;
printf(" 1.Addition\n 2.Subtraction\n 3.Multiplication\n 4.Division\n 5.Remainder\
n");
printf("Enter the values of a & b: ");
scanf("%d %d",&a,&b);
printf("Enter your Choice : ");
scanf("%d",&op);
switch(op)
{
case 1 :
printf("Sum of %d and %d is : %d",a,b,a+b);
break;
case 2 :
printf("Difference of %d and %d is : %d",a,b,a-b);
break;
case 3 :
printf("Multiplication of %d and %d is : %d",a,b,a*b);
break;
case 4 :
printf("Division of Two Numbers is %d : ",a/b);
break;
case 5:
printf("Remainder of %d and %d is : %d",a,b,a%b);
break;
default :
printf(" Enter Your Correct Choice.");
break;
}
}
Output:
Algorithm
Step-1: [Read values of A, B, C]-Read A, B, C.
Step-2: [Check for Co-efficient]-If A=B=C=0 then roots are non-determinant
Step-3:[Compute the value Disc]- Disc=B*B-4*A*C;
Step-4:[Different roots are obtained depending on the value of disc]-
If Disc is lesser than 0 than goto step 5
If Disc is equals to 0 than goto step 6
If Disc is greater than 0 than goto step 7.
Step-5: Print Imaginary roots.
Realp=-B/2*A;
Imagp=sqrt(Disc)/2*A;
Root1=realp + imagep;
Root2 = realp – imagep;
Step-6:Roots are real and equal
Root1=B/2*A;
Root2 = root1;
Step-7: roots are real and distinct
Root1= – B +sqrt(Disc)/2*A;
Root2 = -B -sqrt(Disc)/2*A;
Step-8: Stop
Flowchart:
Program:
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float a,b,c,disc;
float root1,root2,realp,imagp;
printf("Enter values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
{
disc=b*b-4*a*c;
if(disc>0)
{
root1=(-b+sqrt(disc))/(2*a);
root2=(-b-sqrt(disc))/(2*a);
printf("roots are real and distinct\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}
else if(disc==0)
{
root1=-b/(2*a);
root2=root1;
printf("roots are real and equal\n");
printf("root1=%f\n",root1);
printf("root2=%f\n",root2);
}
else if(disc<0)
{
realp=-b/(2*a);
imagp=sqrt(abs(disc)/(2*a));
printf("roots are complex\n");
printf("root1=%f+i%f\n",realp,imagp);
printf("root2=%f-i%f\n",realp,imagp);
}
}
Output:
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 rupees 1 per unit. All users are charged a minimum of
rupees 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:
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 4: Compute: amt=100+(0.8*units).
Step 5: if unit is greater than 200 and less than 300,if true goto step 6 else goto
step7
Step 6: Compute amt=100+(200*0.8)+((units-200)*0.9)
Step 7:Compute amt=100+(200*0.8)+(100*0.9)+((units-300)*1), then goto step 8
Step 8: Check if the amt is less than or equal to 400, if true goto step 9 otherwise
goto step 10.
Step 9: Print the amount charged and goto step 11.
Step 10: compute amt=amt*1.15,and print the amount charged.
Step 11: Stop
Program:
#include<stdio.h>
#include<string.h>
void main()
{
int cust_no, unit_con;
float charge,surcharge=0, amt, total_amt;
char nm[25];
if (unit_con <200 )
charge = 0.80;
else if (unit_con>=200 && unit_con<300)
charge = 0.90;
else
charge = 1.00;
amt = unit_con*charge;
if (amt>400)
surcharge = amt*15/100.0;
total_amt = amt+surcharge;
printf("\t\t\t\nElectricity Bill\n\n");
printf("Customer IDNO :\t%d",cust_no);
printf("\nCustomer Name :\t%s",nm);
printf("\nunit Consumed :\t%d",unit_con);
printf("\nAmount Charges @Rs. %4.2f per unit :\t%0.2f",charge,amt);
printf("\nSurchage Amount :\t%.2f",surcharge);
printf("\nMinimum meter charge Rs :\t%d",100);
printf("\nNet Amount Paid By the Customer :\t%.2f",total_amt+100);
Output
:
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,
other wise 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 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 then 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>
void main()
{
int n, a[100], i, key, high, low, mid, loc=-1;
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++)
Sri Sairam College of Engineering, Anekal, Bengaluru.
10 | P a g e
Computer Programming Laboratory 21CPL27
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}
if(key<a[mid])
high=mid-1;
else}
}
Algorithm
Step-1: Start
Step-2: [Read the order of both matrices M, N, P, Q]
Step-3: Check for i = 0 to M and j = 0 to N for the matrix A, read A[i][j]
Step-4: Check for i = 0 to P and j = 0 to Q for the matrix B, read B[i][j]
Step-5: If in case (N=P) then only multiplication is possible then goto step 6
otherwise goto step 7
Step-6: Initially set matrix C[i][j] as 0
For k=0 to n
C[i][j] = C[i][j] + A[i][k]*B[i][k] than goto step 8
Step-7: Multiplication is not possible
Step-8: Prints the multiplication of the two matrix
Step-9: Stop
Program:
#include<stdio.h>
int main()
{
int a[20][20],b[20][20],c[20][20];
int m,n,p,q,i,j,k;
printf("Enter rows and columns of matrix A\n");
scanf("%d%d",&m,&n);
printf("Enter rows and columns of matrix B\n");
scanf("%d%d",&p,&q);
if(n!=p)
{
printf("Matrix multiplication not possible\n");
return 0;
}
printf("Enter elements of matrix A\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&a[i][j]);
}
}
printf("Enter 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<p;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("Product of two matrices is\n");
Sri Sairam College of Engineering, Anekal, Bengaluru.
14 | P a g e
Computer Programming Laboratory 21CPL27
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\n",c[i][j]);
}
}
}
Output:
Enter rows and columns of matrix A
22
Enter rows and columns of matrix B
22
Enter elements of matrix A
66
66
Enter elements of matrix B
22
22
Product of two matrices is
24 24
24 24
6. 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: [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<stdlib.h>
#include<math.h>
void main()
{
int x,n,i;
float rad, res, sum=0;
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));
}
int fact(int m)
{
int i,f=1;
for(i=1;i<=m;i++)
{
f=f*i;
}
return 1;
Output
Algorithm
Step-1: Start
Step-2: Read un-sorted array of n elements into a[i], where i is the index value
Step-3: Initialize index i=0
Step-4: Check for the i if it is less than n. if ture, than goto step-5. Other wise goto
step output array
Step-5: Initialize the index J to zero
Step-6: Check if j is less than (n-i-1). If it is true than goto step-7, otherwise increment
j and goto step-4
Step-7: Check if a[j] is greater than a[j+1](i.e-adjacent elements are compared) inside
the for loop. if true swap the elements using temporary variables, otherwise goto
step-6
Step-8: Using the for loop output the sorted array elements
Step-9: Stop
Program:
#include<stdio.h>
int main()
{
int i,j,n,temp;
int a[20];
printf("enter the value of n");
scanf("%d",&n);
printf("Enter the numbers in unsorted order:\n");
for(i=0;i<n;i++)
scanf("%d", &a[i]);
// bubble sort logic
Sri Sairam College of Engineering, Anekal, Bengaluru.
19 | P a g e
Computer Programming Laboratory 21CPL27
for(i=0;i<n;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("The sorted array is\n");
for(i=0;i<n;i++)
{
printf("%d\n",a[i]);
}
}
Output:
Step-1: Start
Step-2: Press 1 – Compare, 2 – Concatenate, 3-length of string, if press 1 than goto
step 3, if press step 2 then goto step 4, if press 3 then goto step 5
Step-3: Read the string1 and string2 and cojmparsion function by using strcmp built
in unction is used. If res = 0 then print both strings are equal, otherwise print strints
are not equal and than goto the step 4
Step-4: Read String1 and sring2 and find concatenation of two strings using string
handling function strcat() and display sring and return back to main fuctions.
Step-5: Read string1 and call function to find the length of string by calling function
length ( *string)
Step-6: if digit=1 then gtoto step 2 else goto step 7
Sri Sairam College of Engineering, Anekal, Bengaluru.
20 | P a g e
Computer Programming Laboratory 21CPL27
Step-7: Stop
Program:
#include<stdio.h>
#include<string.h>
void compare(char [ ],char [ ]); .
void concat(char [ ],char [ ]);
void length(char *[ ]);
void main( )
{
int n,digit;
char str1[10],str2[10];
do
{
printf("press 1-compare 2-concatenate 3-length of string");
printf("\n enter your choice=");
scanf("%d",&n);
switch(n)
Sri Sairam College of Engineering, Anekal, Bengaluru.
21 | P a g e
Computer Programming Laboratory 21CPL27
{
case 1:printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
compare(str1,str2);
break;
case 2: printf("enter first string=");
scanf("%s",str1);
printf("enter second string=");
scanf("%s",str2);
concat(str1,str2);
break;
case 3:printf("enter string=");
scanf("%s",str1);
length(&str1);
break;
default: printf("wrong choice");
break;
}
printf("\n Do you want to continue(1/0)? ");
scanf("%d", &digit);
}while(digit==1);
}
void compare(char str1[ ],char str2[ ])
{
int i;
i=strcmp(str1,str2);
if(i==0)
printf("strings are equal\n ");
else
printf("string are not equal\n");
}
void concat(char str1[ ],char str2[ ])
{
strcat(str1,str2);
printf("concatenate string=%s",str1);
}
void length(char *str1[ ])
{
int len;
len=strlen(str1);
printf("the length of string=%d",len);
}
Output:
9. Implement structures to read, write and compute average- marks and 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 avarage marks and store it in the avg field
Step-5: Print the results
Step-6: Initialise loop
Step-7: Read teh average of every student
Step-8: Check for if avg>35.00
Step-9: If yes than print the result else goto next interation
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
Flowchart:
Program:
#include<stdio.h>
struct student
{
char usn[10];
char name[10];
float m1,m2,m3;
float avg,total;
};
void main()
{
struct student s[20];
int n,i;
float tavg,sum=0.0;
printf("Enter the number of student=");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Enter the detail of %d students\n",i+1);
Sri Sairam College of Engineering, Anekal, Bengaluru.
24 | P a g e
Computer Programming Laboratory 21CPL27
Output
10. 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
Step-3: For every value of n read the x
Step-4: Initialize sum=0 and i=0
Step-5: For every value of n and i, comuter sum using sum = sum + (*(x+i) – mean) * ( * ( x+i) –
mean)
Step-6: Using the sum value computer variance = sum / n and deviation = sqrt ( variance )
Step-7: Display mean, variance, deviation
Step-8: Stop
Flowchart:
Program:
#include<stdio.h>
#include<math.h>
int main()
{
int n , i;
float x[20],sum,mean;
float variance , deviation;
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++)
Sri Sairam College of Engineering, Anekal, Bengaluru.
27 | P a g e
Computer Programming Laboratory 21CPL27
{
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);
}
Output
Step-1: Start
Step-2: Read the binary number value
Step-3: Call the function find by passing arguments
Step-4: Check if number entered is zero than return 0 otherwise compute using formula-
( pow ( 2, count ) * ( x % 10 ) + find ( x /10 , count + 1 ) )
Step-5: Repeat the step-4 until recursive condition find becomes false
Step-6: Return the result of converted number
Step-7: Stop
Flowchart:
Program:
#include <stdio.h>
#include <math.h>
int find(int x, int count)
{
if (x == 0)
return 0;
else
return(pow(2, count) * (x%10 ) + find(x/10 ,count+1));
}
void main()
{
int binary;
printf("Enter a Binary number");
scanf ("%d",&binary);
printf(" The decimal value for binary %d is",binary);
printf("%d", find(binary,0));
}
Output: