Lab Manual - 21CPL17
Lab Manual - 21CPL17
Lab Manual - 21CPL17
LABORATORY MANUAL
Prepared By
Prof. R.Vadivel
Prof.Sarumathi
Prof.S.Rezni
Prof.Veeresh
Verified By
DQAC HOD
HKBK COLLEGE OF ENGINEERING
(Affiliated to VTU, Belgaum and Approved by AICTE)
BANGALORE -560045
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Vision
To empower students through wholesome education and enable the students to develop
into highly qualified and trained professionals with ethics and emerge as responsible
citizen with broad outlook to build a vibrant nation.
Mission
M1: To achieve academic excellence in science, engineering and technology through
dedication to duty, innovation in teaching and faith in human values.
M2: To enable our students to develop into outstanding professional with high ethical
standards to face the challenges of 21st century.
M3: To provide educational opportunities to the deprived and weaker section of the
society to uplift their socioeconomic status.
Vision
To advance the intellectual capacity of the nation and the international community by
imparting knowledge to graduates who are globally recognized as innovators,
entrepreneur and competent professionals.
Mission
M1: To provide excellent technical knowledge and computing skills to make the
graduates globally competitive with professional ethics.
M2: To involve in research activities and be committed to lifelong learning to make
positive contributions to the society
HKBK COLLEGE OF ENGINEERING
(Affiliated to VTU, Belgaum and Approved by AICTE)
BANGALORE -560045
DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING
Bangalore-560045
COMPUTER PROGRAMMING
LABORATORY
Course Code 21CPL27/17 CIE Marks 50
Teaching Hours/Week (L:T:P: S) 0:0:2:0 SEE Marks 50
Total Hours of Pedagogy -- Total Marks 100
Credits 01 Exam Hours 03
Course Objectives:
1. Explain problem statements and identify appropriate solutions
2. Demonstrate the use of IDE, C Compiler, and identify and rectify the
syntax and syntactic errors during programming.
3. Development of algorithms and programs using constructs of C programming
language
4. Reporting the observations
Sl. Practise Programs
No.
Calculation of Simple Interest,
Check whether the given number is even or odd
Convert string case
Check for palindrome, prime number, perfect square.
Development of linear search algorithm Etc…
PART A – List of problems for which student should develop program
and execute in the Laboratory
1 Simulation of a SimpleCalculator.
Compute the roots of a quadratic equation by accepting the coefficients. Print
2
appropriate
messages.
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:
3 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 Implement Binary Search on Integers / Names.
5 Implement Matrix multiplication and validate the rules of multiplication.
Compute sin(x)/cos(x) using Taylor series approximation. Compare your
6
result with the
built-in library function. Print both the results with appropriate inferences.
7 Sort the given set of N numbers using Bubble sort.
Write functions to implement string operations such as compare, concatenate,
8
string
length. Convince the parameter passing techniques.
Implement structures to read, write and compute average- marks and the
9
students scoring
above and below the average marks for a class of N students.
Develop a program using pointers to compute the sum, mean and standard
10
deviation of all
elements stored in an array of N real numbers.
11 Implement Recursive functions for Binary to Decimal Conversion.
PART B – Practical Based
Learning
A problem statement for each batch is to be generated in consultation with the
1 co-examiner
and student should develop an algorithm, program and execute the program
for the given problem with appropriate outputs.
PO1 PO2 PO3 PO4 PO5 PO6 PO7 PO8 PO9 PO10 PO11 PO12 PSO1 PSO2 PSO3
CO1 3 2 - 1 - 2 1 - - 1 1 - 3 2 1
CO2 2 2 1 1 1 1 1 - 1 - 1 - 3 2 2
CO3 2 3 1 1 2 2 3 - 1 - 3 - 2 3 2
CO4 2 3 - - 1 1 2 - 1 - 3 2 2 3 2
CO5 - 1 - 2 1 1 3 - 1 3 - - 2 2 2
Avg 2.2 2.2 1 1.5 1.5 1.5 2 - 1 2 2 2 2.4 2.4 1.8
No -
Correlation
Low 1
Moderate 2
High 3
List of Experiments
Sl.No Name of the Experiment
1. Simulation of a Simple Calculator.
2. Compute the roots of a quadratic equation by accepting the coefficients. Print
appropriate messages.
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. Implement Binary search on integers/ Names.
5. Implement Matrix multiplication and validate the rules of multiplication.
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.
7. Sort the given set of N numbers using Bubble Sort.
8. Write functions to implement string operations such as compare, concatenate,
string length. Convince the parameter passing techniques.
9. Implement structures to read, write and compute average marks and students
scoring above and below the average marks for a class of N students.
10. Develop a program using pointers to compute the sum, mean and standard
deviation of all elements stored in an array of Nreal numbers.
11. Implement Recursive functions for Binary to Decimal Conversion.
Laboratory Programs:
PART-A
Flowchart
Start
Read a op b
(op)
res=a+b
op=’+’
res=a-b
op=’-’
res=a*b
op=’*’
res=a/b
op=’/’
res=a%b
op=’%’
default
Stop
Algorithm:
Step 1: Start
goto step 5
case'-': res=a-b;
goto step 5
case'*': res=a*b;
goto step 5
case'/': if(b!=0)
res=a/b;
Otherwise
goto step 5
case'%': res=a%b;
goto step 5
Step 5: Stop
Program:
#include<stdio.h>
#include<conio.h>
void main()
float a,b,res;
char op;
clrscr();
scanf("%f%c%f",&a,&op,&b);
switch(op)
case'+': res=a+b;
printf("\nsum=%f",res);
break;
case'-': res=a-b;
printf("\nDifference=%f",res);
break;
case'*': res=a*b;
printf("\nProduct=%f",res);
break;
case'/': if(b!=0)
res=a/b;
printf("\nQuotient=%f",res);
else
{
printf("\nDivide error!");
break;
case'%': if(b!=0)
res=(int)a%(int)b;
printf("\nRemainder=%f",res);
else
printf("\nDivide error!");
break;
break;
getch();
2 20-15 Res=5
3 20*30 Res=600
4 20/2 Res=10
5 20%6 Res=2
Sl.NO Questions
1. What is algorithm?
2. What is flowchart ?
3. What is an operator?
4. What is an operand?
Flowchart:
Start
Read
a,b,c
Write T F
a==0
“Invalid
Output”
r1=(-b+sqrt(d))/ T
d>0
(2*a)
r1=(-b-sqrt(d))/
Write “roots are F
(2*a)
real”
Write r1,r2
r1=(-b+sqrt(d))/ T
d<0
(2*a)
F
r1=(-b-sqrt(d))/
Write
(2*a)“roots are r1=r2=-b/(2*a)
real”
Write r1,r2
Write “roots are
equall”
Write r1,r2
stop
Algorithm:
1. Start.
2. Read the coefficients a, b and c.
3. If any of the coefficients is zero display “Invalid Input”
4. Otherwise,
1. calculate db*b-4*a*c
2. Find rsqrt(d)
3. If d is greater than zero
i. r1(-b+r)/(2*a)
ii. r2(-b-r)/(2*a)
iii. Display “Roots are real and distinct”
iv. Display the roots r1 and r2
4. If d is equal to zero
i. r1=r2=-b/(2*a)
ii. Display “Roots are real and equal”
iii. Display r1 and r2.
5. Otherwise
i. rp-b/(2*a)
ii. ipr/(2*a)
iii. Display “Roots are imaginary”
iv. Display the roots
5. End
Program:
#include <stdio.h>
#include <math.h>
void main()
inta,b,c;
float r1,r2,d,rp,ip,r;
scanf("%d%d%d",&a,&b,&c);
if (a==0)
printf("Invalid Input\n");
else
{
d=b*b-4*a*c;
r=sqrt(fabs(d));
if (d>0)
r1=(-b+r)/(2.0*a);
r2=(-b-r)/(2.0*a);
printf("Root1=%f\nRoot2=%f\n",r1,r2);
else if (d==0)
r1=r2=-b/(2.0*a);
printf("Root1=Root2=%f\n",r1);
else
rp=-b/(2.0*a);
ip=r/(2.0*a);
printf("Root1=%f+i%f\nRoot2=%f-i%f\n",rp,ip,rp,ip);
getch();
}
Test Cases:
Root1=Root2=-2.000000
Root1=3.000000
Root2=2
.000000
Root1=-0.750000+i1.198958
Root2=-0.750000-i1.198958
4 1 0 8
5 0 3 6
Viva Questions:
Sl.NO Questions
FlowChart:
Algorithm:
Step 1: Start
Step 3: if (units<=200)
charges=units *0.8;
charges=200*0.8+(units-200)*0.9;
else
Step 5: charges=200*0.8+100*0.9+(units-300)*1.0;
Step 6: charges=charges+100;
Program:
#include <stdio.h>
#include <conio.h>
void main()
int units;
float charges;
char name[20];
scanf("%s",name);
if (units<=200)
charges=units *0.8;
else if (units<=300)
charges=200*0.8+(units-200)*0.9;
else
charges=200*0.8+100*0.9+(units-300)*1.0;
charges=charges+100;
if (charges> 400)
getch();
Test Case
Units: 670
Units: 380
Units: 180
Viva Questions:
Sl.NO Questions
Flowchart:
Algorithm:
Step 1: Start
Step 3: Enter the elements in ascending order one by one using for loop
if (key == a[mid])
found =1;
break;
otherwise
high = mid - 1;
low = mid + 1;
Step 7: if found=1
Otherwise
Step 8:Stop
Program:
#include <stdio.h>
#include<conio.h>
void main()
int a[10];
int i, j, n, key;
clrscr();
scanf("%d", &n);
for (i=0;i<n;i++)
scanf("%d",&a[i]);
scanf("%d", &key );
low = 0;
high = n-1;
while(low<=high)
if (key == a[mid])
found =1;
break;
high = mid - 1;
low = mid + 1;
if (found==1)
else
getch();
Test Cases:
1 n=5
SEARCH
Elements: 1 2 3 4 5
SUCCESSFUL
Key= 4
2 n=6 SEARCH
SUCCESSFUL
Elements: 11 12 13 14 15 16
Key= 11
3 n=5
SEARCH
Elements: 3 4 5 6 7
UNSUCCESFUL
Key= 2
4 n=6
SEARCH
Elements: 21 22 23 24 25 26
UNSUCCESFUL
Key= 11
Viva Questions:
Sl.No Questions
FlowChart:
START
Read m,n,p,q
T Print Multiplication
not allowed
If(n!
=p)
F
B
For(i=0;i<m;i+
F +)
For(j=0;j<n;j+
+)
Read A[i][j]
F
For(i=0;i<p;i+
+)
T
For(j=0;i<q;j+
+)
T A
Read B[i][j]
Algorithm:
Step 1: Start
Step 2: Read the order of two matrices A and B
Step 3: Check the number of rows and column of A and B matrices for compatibility
For j from 0 to q:
a. Let c[i][j] = 0
b. For k from 1 to n:
Set c[i][j] ← c[i][j]+a[i][k]*b[k][j];
Step 7: Stop
Program:
#include<stdio.h>
void main()
inti,j,k,a[10][10],b[10][10],c[10][10],m,n,p,q;
scanf("%d%d",&m,&n);
scanf("%d%d",&p,&q);
if (n!=p)
else
for (i=0;i<m;i++)
for (j=0;j<n;j++)
scanf("%d",&a[i][j]);
for (i=0;i<p;i++)
for (j=0;j<q;j++)
scanf("%d",&b[i][j]);
//matrix multiplication
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("Matrix A:\n");
for (i=0;i<m;i++)
for(j=0;j<n;j++)
printf("%d\t",a[i][j]);
printf("\n");
printf("Matrix B:\n");
for (i=0;i<p;i++)
{
for(j=0;j<q;j++)
printf("%d\t",b[i][j]);
printf("\n");
printf("Matrix C:\n");
for (i=0;i<m;i++)
for(j=0;j<q;j++)
printf("%d\t",c[i][j]);
printf("\n");
getch();
Test Cases:
VIVA Questions
Sl. No Questions
1. What is matrix?
Flowchart:
START
Read X
Temp=x
x=x*(3.142/180.0)
While(term>=fabs(0.0001)
)
term=nume/deno;
sum=sum+term
i=i+2
demo=deno*(i-1)*i
e.nume=-
nume*x*x
Print sinx with
and without
library functoin
Stop
Algorithm:
Step 1: Start
a.term=nume/deno;
b.sum=sum+term
c.i=i+2
d.deno=deno*(i-1)*i
e.nume=-nume*x*x
Step 6: Display Sin(x) and Sum using Taylor Series and Built in library function
Step 7: Stop
Program:
#include<stdio.h>
#include<math.h>
void main()
float x,sum,nume,deno,term;
int degree, i;
printf("Enter degree:\n");
scanf("%d",°ree);
x=degree*(3.142/180);
sum=0;
nume=x;
deno=1;
i=1;
do
term=nume/deno;
sum=sum+term;
i=i+2;
deno=deno*(i-1)*i;
nume=-nume*x*x;
}while(fabs(term)>0.00001);
getch();
Test Cases:
Sl. No Questions
Flowhart:
start
Read n
F
for(i=0;i<n;i++)
Read num[i]
num[i]
F
for(i=0;i<n-1;i++)
T
F
for(j=0;j<n-i-1;j++)
T
F
num[j]=num[j+1])
temp=num[i];
num[j]=num[j+1];
F num[j+1]=temp
for(i=0;i<n;i++)
T
Stop
Print num[i]
Algorithm:
1. Start
2. Read n.
3. Read the elements in an array using for loop.
4. for i=0 to i<n-1
5. for j=0 to j<n-1
if (a[j]>a[j+1])
swap a[j],a[j+1]
Program:
void main()
intn,i,j,a[10],t;
printf("Enter n\n");
scanf("%d",&n);
printf("Enter elements\n");
for (i=0;i<=n;i++)
scanf("%d",&a[i]);
for (i=0;i<n-1;i++)
for (j=0;j<n-1;j++)
if (a[j]>a[j+1])
t=a[j];
a[j]=a[j+1];
a[j+1]=t;
}
printf("Sorted Array\n");
for (i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
Test Cases:
N Elements
1 4 5321 1235
2 5 56231 12356
Viva Questions:
Sl.No Questions
3 If loop syntax
4. What is an array?
Flowchart:
start
Read
Call length(str1) A
Call length(str2) A
Callcmp_str(str1,str2) B
Call concat2str(str1,str2) C
stop
A
B
For(i=0;str[i]!=’\
0’i++
len(s1)!=
return i
len(s2)
C
return 1
i=0,j=len(s1)
For(i=0;str[i]!=’\0’ ;i++ return 0
While( s2[i]!=’\0’)
s1[i]!=
S1[i]= s2[j]
s2[i]
i++, j++
return s1 return 1
Algorithm:
Step 1: Start
Step 9:Stop
Increment len
Step 1: Compute the length of string s1 and s2 using the function len_str(s1) and len_str(s2)
Return 1
if (s1[i] !=s2[i])
return 1
Step 4: return 0
Step 2: while(s2[i]!='\0')
Assign s1[j]=s2[i];
Increment j and i
Program:
#include <stdio.h>
#include<conio.h>
intlen_str(char s[50]);
void main()
char s1[50],s2[50],len,c;
clrscr();
scanf("%s",s1);
len=len_str(s1);
len=len_str(s2);
c=comp_str(s1,s2);
if(c==0)
else
concat_str(s1,s2);
getch();
intlen_str(char s[50])
int len = 0, i;
len++;
return len;
}
len1 = len_str(s1);
len2 = len_str(s2);
if (len1 != len2)
return 1;
if (s1[i] !=s2[i])
return 1;
return 0;
int i=0,j;
j=len_str(s1);
while(s2[i]!='\0')
s1[j]=s2[i];
j++;
i++;
}
s1[j]='\0';
Test Cases:
String1 String2
Viva Questions
SL.NO Questions
2. What is string?
Flowchart:
Start
Read n
For i=1 to n
average= sum/n
Print average
For i=1 to n
If
s[i].marks>av
g
For i=1 to n
If s[i].marks<
avg
Algorithm:
Step 1: Start
Step 6: Display all the details of student whose average is greater than or equal to 35
Step 7:Display all the details of student whose average is less than 35
Step 8:Stop
Program:
#include <stdio.h>
#include<stdio.h>
void main()
struct stud
char name[25];
char usn[25];
int marks;
};
int n,i,sum;
float average;
sum=0;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%s%s%d",s[i].name,s[i].usn,&s[i].marks);
sum=sum+s[i].marks;
average=sum/(float)n;
printf("average marks=%f",average);
for(i=0;i<n;i++)
if (s[i].marks>= average)
for(i=0;i<n;i++)
if (s[i].marks< average)
Test Cases:
1 2 12 John 98 Average=97
13 Edwin 99
12 John 98
Viva questions
SL NO Questions
1 What is structure?
5 Explain strcmp?
10. Develop a program using pointers to compute the sum, mean and standard deviation of
allelements stored in an array of n real numbers.
FlowChart:
Algorithm:
Step 1: Start
sum=sum+*ptr
Mean=sum/n
sumstd=sumstd+pow((*ptr-mean),2)
std=sqrt(sumstd/n)
Program:
#include<stdio.h>
#include<math.h>
void main()
float a[10],*ptr,mean,std,sum=0,sumstd=0;
intn,i;
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=%f\n",sum);
printf("Mean=%f\n",mean);
printf("standard deviation=%f\n",std);
getch();
}
Test Cases:
Viva Questions:
SL NO Questions
1 Define pointer?
Flowchart:
n/=10;
return
Stop
Algorithm:
Step 1: Start
Step 3: res=bin_dec(x)
Step 5:Stop
Algorithm for bin_dec(num)
Step 2: rem=num%10
term=rem*base
num=num/10
base=base*2
Step 3:if(num==0)
return term;
otherwise
return(term+bin_dec(num));
Program:
#include<stdio.h>
#include<conio.h>
longintbin_dec(long intnum);
void main()
longintx,res;
scanf(“%ld”,&x);
res=bin_dec(x);
longintbin_dec(long intnum)
rem=num%10;
term=rem*base;
num=num/10;
base=base*2;
if(num==0)
return term;
else
return(term+bin_dec(num));
Test Cases:
1 10011 19
2 00101 5
3 100011 35
4 1100110 102
Viva Questions:
SL NO Questions
1 What is Recursion?