Bpops103-203 Lab Manual
Bpops103-203 Lab Manual
Bpops103-203 Lab Manual
BELAGAVI-590018
Course Outcomes:
Elucidate the basic architecture and functionalities of a computer and also recognize the
CO 1. hardware parts.
Apply programming constructs of C language to solve the real-world problem
CO 2.
Explore user-defined data structures like arrays in implementing solutions to problems
CO 3. like searching and sorting.
CO 4. Explore user-defined data structures like structures, unions and pointers in implementing
solutions
CO 5. Design and Develop Solutions to problems using modular programming constructs using
functions
Syllabus:
Laboratory Programs.
PART-A
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.
4. Write a C Program to display the following by reading the number of rows as input,
8. Develop a program to sort the given set of N numbers using Bubble sort.
10. Implement structures to read, write, compute average- marks and the students scoring
11. Develop a program using pointers to compute the sum, mean and standard deviation
12.Write functions to implement string operations such as compare, concatenate, string length.
On completion of every experiment/program in the laboratory, the students shall be evaluated and marks
shall be awarded on the same day.
The 15 marks are for conducting the experiment and preparation of the laboratory record, the other 05
marks shall be for the test conducted at the end of the semester.
• The CIE marks awarded in the case of the Practical component shall be based on the continuous
evaluation of the laboratory report. Each experiment report can be evaluated for 10 marks. Marks of all
experiments’ write-ups are added and scaled down to 15 marks.
• The laboratory test (duration 03 hours) at the end of the 15th week of the semester /after completion of
all the experiments (whichever is early) shall be conducted for 50 marks and scaled down to 05 marks.
Scaled-down marks of write-up evaluations and tests added will be CIE marks for the laboratory component
of IC/IPCC for 20 marks.
Lab Programs
1. Develop a program to solve simple computational problems using
arithmetic expressions and use of each operator leading to
simulation of a commercial calculator. (No built-in math function)
Algorithm:-
Step 1 : Start
Step 2 : Read
a, op, b
Step 3 :
switch (op)
case ‘+’ : res← a + b
Print “res”
goto Step 4
case ‘-’ : res← a - b
Print “res”
goto Step 4
case ‘*’ : res← a * b
Print “res”
goto Step 4
case ‘/’ : if(b==0)
Print “ Divide by Zero error”
goto Step 4
else
res← a / b
Print “res”
goto Step4
end if
Step 4 : Stop
Program:-
#include<stdio.h>
#include<stdlib.h>
void main()
{
float a,b,res;
char op;
printf("Enter a valid Expression\n");
scanf("%f%c%f",&a,&op,&b);
switch(op)
{
case '+' : res=a+b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
case '-' : res=a-b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
case '*' : res=a*b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
case '/' : if(b==0)
{
printf("Divide by Zero Error\n");
exit(0);
}
else
res=a/b;
printf("\n The Resultant of %f %c %f = %f\n",a,op,b,res);
break;
default : printf("Invalid Expression\n");
break;
}
}
Output:-
RUN 1 :-
mite@mite-Veriton-Series:~/Desktop/POP$ cc prog1.c
mite@mite-Veriton-Series:~/Desktop/ POP $ ./a.out
Enter a valid Expression
7+8
The Resultant of 7.000000 + 8.000000 = 15.000000
RUN 2 :-
RUN 3 :-
RUN 4 :-
RUN 5 :-
Algorithm:-
Step 1: Start
Step 3: if(a==0)
Print “Invalid Input”
end if
Step 4: disc← b*b-4*a*c
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
void main()
{
float a,b,c,x1,x2,disc;
printf("Enter the values of a,b,c\n");
scanf("%f%f%f",&a,&b,&c);
if(a==0)
{
printf("Invalid Input\n");
exit(0);
}
disc=b*b-4*a*c;
if(disc>0)
{
printf("Roots are Real and Distinct\n");
x1=((-b)+sqrt(disc))/(2*a);
x2=((-b)-sqrt(disc))/(2*a);
printf("Root1= %f\n Root2= %f\n",x1,x2);
}
else if(disc==0)
{
printf("Roots are Real and Equal\n ");
x1=(-b)/(2*a);
printf("Root1=Root2=%f\n",x1);
}
else
{
printf("Roots are Imaginary\n");
x1=(-b)/(2*a);
x2=(sqrt(fabs(disc)))/(2*a);
printf("Root1= %f +i %f\n",x1,x2);
printf("Root2= %f -i %f\n",x1,x2);
}
}
Output:-
RUN 1 :-
RUN 2 :-
RUN 3 :-
RUN 4 :-
mite@mite-Veriton-Series:~/Desktop/ POP$ ./a.out
Enter the values of a,b,c
057
Invalid Input
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:-
Step 1 : Start
Step 2 : Read name,units
Step 3: if(units<=200) then
ta← (units*0.80)+100
end if
goto Step
Step 4: if(units>200 && units<=300) then
ta← (200*0.80+(units-200)*0.90)+100
end if
goto Step
Step 5 : if(units>300) then
ta← ta← (200*0.80+100*0.90+(units-300)*1)+100
end if
goto Step 6
Step 7 : if(ta>400) then
ta← ta+(ta*0.15)
end if
goto Step8
Step 8 : Print “name and total amount to be paid is ta ”
Step 9: Stop
Program:-
#include<stdio.h>
void main()
{
char name[20];
int units,mtc=100;
float charge,ta;
printf("Enter the consumer name\n");
scanf("%s",name);
printf("Enter the units consumed\n");
scanf("%d",&units);
if(units<=200)
{
charge=units*0.80;
ta=charge+mtc;
}
else if(units>200&&units<=300)
{
charge=200*0.80+(units-200)*0.90;
ta=charge+mtc;
}
else
{
charge=200*0.80+100*0.90+((units-300)*1);
ta=charge+mtc;
}
if(ta>400)
ta=ta+(ta*0.15);
printf("%s has Consumed %d units\n Hence the total amount to be paid is
%f Rupees only",name,units,ta);
}
Output:-
RUN 1 :-
RUN 2 :-
RUN 3 :-
#include <stdio.h>
void main()
{
int i,j,n;
printf("Input number of rows : ");
scanf("%d",&n);
for(i=0;i<=n;i++)
{
/* print blank spaces */
for(j=1;j<=n-i;j++)
printf(" ");
/* Display number in ascending order upto middle*/
for(j=1;j<=i;j++)
printf("%d",j);
/* Display number in reverse order after middle */
for(j=i-1;j>=1;j--)
printf("%d",j);
printf("\n");
}
}
RUN 1 :-
Algorithm:-
Step 1 : Start
Step 2 : Read n
Step 3: for i← 0 to n do
Read a[i]
end for
Step 6 : REPEAT
mid← (low+high)/2
if(key==a[mid]) then
flag← 1
goto Step 7
end if
if( key>a[mid]) then
low← mid+1
else
high←mid-1
end if
UNTIL (low<=high)
Step 7 : if(flag==1) then
Print “Element found at mid+1 position”
else
Print “Element not found ”
end if
Step 8 : Stop
Program:-
#include<stdio.h>
void main()
{
int a[100],n,i,low,high,mid,key,flag=0;
printf("Enter the size of the array\n");
scanf("%d",&n);
printf("Enter %d elements in ascending order\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Enter the element to be searched\n" );
scanf("%d",&key);
low=0;
high=n-1;
while(low<=high)
{
mid=(low+high)/2;
if(key==a[mid])
{
flag=1;
break;
}
else
if(key>a[mid])
low=mid+1;
else
high=mid-1;
}
if(flag==1)
printf("Element found at position %d\n",mid+1);
else
printf("Element not found\n");
}
Output:-
RUN 1 :-
RUN 2 :-
Algorithm:-
Step 1 : Start
Step 2 : Read m,n,p,q
Step 3 : if(n!=p) then
Print “Matrix Multiplication is not Possible”
end if
goto Step
Step 4 : Read Matrix a
for i←0 to m do
for j←0 to n do
Read a[i][j]
end for
end for
Step 5 : Read Matrix b
for i←0 to p do
for j←0 to q do
Read b[i][j]
end for
end for
Step 6 : for i←0 to m do
for j←0 to q do
c[i][j] ← 0
for k←0 to n do
c[i][j] ← c[i][j]+a[i][k]*b[k][j]
end for
end for
end for
Step 7: Print “Resultant Matrix”
for i←0 to m do
for j←0 to q do
Print “ c[i][j]”
end for
end for
Step 8 : Stop
Program:-
#include<stdio.h>
void main()
{
int a[50][50],b[50][50],c[50][50];
int m,n,p,q,i,j,k;
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 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("Matrix A is displayed as\n");
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
{
printf("%d\t",a[i][j]);
}
printf("\n");
}
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]);
}
}
printf("Matrix B is displayed as\n");
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",b[i][j]);
}
printf("\n");
}
printf("\n***** MATRIX MULTIPLICATION *****\n");
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 is\n");
for(i=0;i<m;i++)
{
for(j=0;j<q;j++)
{
printf("%d\t",c[i][j]);
}
printf("\n");
}
}
}
Output:-
RUN 1 :-
RUN 2 :-
Algorithm:-
Step 1 : Start
Step 2 : Read degree
Step 3 : initialize sum←0, pi←3.142
Step 4 : x←(pi*degree)/180
Step 5: num←x, den← 1, i←2
Step 6 : REPEAT Step 6
term←num/den
sum←sum+ term
num← -num*x*x
den←den*i*(i+1)
i←i+2
UNTIL (fabs(term)>0.000001)
Step 7 : Print “value of sin(degree) = sum without using library function ”
Step 8 : Print “value of sin(degree) = sin(x) using library function ”
Step 9 : Stop
Program:-
#include<stdio.h>
#include<math.h>
#define pi 3.142
void main()
{
int degree,i;
float x,num,den,term,sum=0;
printf("Enter the value of degrees\n");
scanf("%d",°ree);
x=(degree*pi)/180;
num=x;
den=1;
i=2;
do
{
term=num/den;
sum=sum+term;
num=-num*x*x;
den=den*i*(i+1);
i=i+2;
}while(fabs(term)>0.000001);
printf("sin(%d)= %f without using library function\n",degree,sum);
printf("sin(%d)= %f using library function\n",degree,sin(x));
}
Output:-
RUN 1 :-
RUN 2 :-
Algorithm:-
Step 1 : Start
Step 2 : REPEAT Step 2
Print “String Operations”
1. String Compare 2. String Concatenate 3. String Length 4. Exit
Step 3 : Read choice
Step 4 : switch (Choice)
case 1: Read str1,str2
compare(str1,str2)
break
case 2 : Read str1,str2
concatenate(str1,str2)
break
case 3 : Read str1
stringlength(str1)
break
default : Print “Invalid Choice”
UNTIL(!choice ==4)
Step 5 : Stop
function compare(str1,str2)
len1←strlen(str1)
len2←strlen(str2)
if(len1!=len2) then
Print “Strings are Different”
end if
for i←0 to str1[i]!= ‘\0’ do
if(str1[i]!=str2[i]) then
flag=0
end if
end for
if(flag==0) then
Print “Strings are Different”
else
Print “Strings are Same”
end if
function concatenate(str1,str2)
for i←0 to str1[i]!= ‘\0’ do
str3[i] ←str1[i]
k←k+1
end for
for i←0 to str2[i]!= ‘\0’ do
str3[k] ←str2[i]
k←k+1
end for
str3[k] ← ‘\0’
Print “The Concatenated String is = str3”
function stringlength(str1)
for i←0 to str1[i]!= ‘\0’ do
count←count+1
end for
Print “The length of the String = count”
Program:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void compare(char str1[50],char str2[50]);
void concatenate(char str1[50],char str2[50]);
void stringlength(char str1[50]);
void main()
{
char str1[50],str2[50];
int choice;
do
{
printf("\n STRING OPERATIONS\n");
printf("1:String Compare\t 2: String Concatenate\n");
printf("3: String Length \t 4: Exit\n");
printf("Enter your Choice :");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter two Strings\n");
scanf("%s%s",str1,str2);
compare(str1,str2);
break;
case 2: printf("Enter two Strings\n");
scanf("%s%s",str1,str2);
concatenate(str1,str2);
break;
case 3: printf("Enter a String\n");
scanf("%s",str1);
stringlength(str1);
break;
default: printf("\nYou have Either Exited or have entered an Invalid
choice\n");
}
}while(!(choice==4));
}
}
}
str3[k]='\0';
printf("The Concatenated String =%s",str3);
}
Output:-
RUN 1 :-
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :1
Enter two Strings
mite mite
Both the strings are same
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :2
Enter two Strings
Anushka
Kholi
The Concatenated String =AnushkaKholi
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :3
Enter a String
Rockstar
The Length of the string = 8
STRING OPERATIONS
1:String Compare 2: String Concatenate
3: String Length 4: Exit
Enter your Choice :4
Step 1: Start
Step 2 : Read n
Step 3: for i←0 to n do
Read a[i]
end for
Step 4: for i← 1 to n do
for j← 0 to n-i do
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 5 : Print “Sorted Array”
for i←0 to n do
Print “a[i]”
end for
Step 6 : Stop
Program:-
#include<stdio.h>
void main()
{
int a[50],n,i,j,temp;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d elements\n",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("The entered elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
printf("\n***** SORTING ******\n");
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("The sorted elements are\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
Output:-
RUN 1 :-
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.
Algorithm:-
Step 1 : Start
Step 2 : Define a Structure Student with members rollno,name,marks,grade
Define a structure variable s
Step 3 : Read n
Step 4: for i←0 to n do
Read s[i].rollno
Read s[i].name
Read s[i].marks
Read s[i].grade
sum←sum+s[i].marks
end for
Step 5 : avg←sum/n
Step 6 : Display the Student details
for i←0 to n do
Print “s[i].rollno,s[i].name,s[i].marks,s[i].grade”
end for
Step 7 : for i←0 to n do
if(s[i].marks>avg)then
Print “Details of student who scored more than average
marks”
else
Print “Details of student who scored below than average
marks”
end if
end for
Step 8 : Stop
Program:-
#include<stdio.h>
#include<string.h>
struct student
{
int rollno;
char name[20];
int marks;
char grade[1];
};
void main()
{
struct student s[20];
char sname[20];
int i,n;
float sum=0,avg=0;
printf("Enter the number of students\n");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("\nEnter student %d details\n",i+1);
printf("\nEnter Roll Number :");
scanf("%d",&s[i].rollno);
printf("Enter Name :");
scanf("%s",s[i].name);
printf("Enter Marks :");
scanf("%d",&s[i].marks);
printf("Enter Grade :");
scanf("%s",s[i].grade);
sum+=s[i].marks;
}
avg=sum/n;
printf("\nAverage Marks= %f\n",avg);
printf("\n Student Details are \n");
printf("ROLL_NO.\t NAME\t MARKS\t GRADE\n");
for(i=0;i<n;i++)
{
printf("%d\t\t%s\t%d\t%s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("\nDetails of students who scored above Average Marks\n");
printf("ROLL_NO.\t NAME\t MARKS\t GRADE\n");
for(i=0;i<n;i++)
{
if(s[i].marks>=avg)
printf("%d\t\t%s\t%d\t%s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
printf("\nDetails of students who scored below Average Marks\n");
printf("ROLL_NO.\t NAME\t MARKS\t GRADE\n");
for(i=0;i<n;i++)
{
if(s[i].marks<avg)
printf("%d\t\t%s\t%d\t%s\n",s[i].rollno,s[i].name,s[i].marks,s[i].grade);
}
}
Output:-
RUN 1 :-
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, *ptr
Step 3 : for i← 0 to n do
Read a[i]
end for
Step 4 : ptr←a
Step 5 : for i←0 to n do
sum←sum+ *ptr
ptr←ptr+1;
end for
Step 6 : mean←sum/n
Step 7 : for i←0 to n do
sumvar←sumvar+pow( (*ptr-mean),2)
ptr←ptr+1
end for
Step 8 : var←sumvar/n
Step 9 : sd←sqrt(num)
Step 10 : Print “sum,mean,sd”
Step 11 : Stop
Program:-
#include<stdio.h>
#include<math.h>
void main()
{
float a[50],sum=0,sumvar=0,mean,var,sd;
float *ptr;
int n,i;
printf("Enter the number of elements\n");
scanf("%d",&n);
printf("Enter %d array elements\n",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++)
{
sumvar=sumvar+(pow((*ptr-mean),2));
ptr++;
}
var=sumvar/n;
sd=sqrt(var);
printf("Sum = %f\n",sum);
printf("Mean = %f\n",mean);
printf("Standard Deviation = %f\n",sd);
}
Output:-
RUN 1 :-
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>
int main() {
FILE *input_file, *target_file;
char input_file_name[100], target_file_name[100];
char ch;
// read the input file name
printf("Enter the input file name: ");
scanf("%s", input_file_name);// give only file name with extension
if (input_file == NULL) {
printf("Error: could not open input file %s\n", input_file_name);
return 1;
}
Output:-
RUN 1 :-