II PU Lab Programs 2024-25 Final
II PU Lab Programs 2024-25 Final
II PU COMPUTER SCIENCE
PRACTICAL MANUAL
Page no 1
TABLE OF CONTENTS
4) Program to sort the element of an array in ascending order using insertion sort.
5) Program to search for a given element in an array using binary search method.
8) Program to find the area of square, rectangle and triangle using function overloading.
15) Program to perform enqueue and dequeue operations on queue data structure.
Page no 2
1. WAP to find the frequency of presence of an element in an array.
#include <iostream.h>
#include <conio.h>
class freq
{
int n, i, a[10], ele, c;
public:
void input( );
void logic( );
void output( );
};
void main( )
{ clrscr( );
freq o;
o.input( );
o.logic( );
o.output( );
getch( );
}
Page no 3
. 2. WAP to insert an element into an array 3. WAP to delete an element from an array at the
at given position. given position.
#include <iostream.h> #include <iostream.h>
#include <conio.h> #include <conio.h>
#include <stdlib.h> #include <stdlib.h>
class ins class del
{ int n, i, a[10], ele, pos; { int n, i, a[10], pos;
public: void input( ); public: void input( );
void logic( ); void logic( );
void output( ); void output( );
}; };
Page no 5
5. WAP to search an element from an array using binary search method.
#include <iostream.h>
#include <conio.h>
class search
{
int n, i, a[10], ele, low, high, m, loc;
public: void input( );
void logic( );
void output( );
};
void search :: input( )
{
cout<<”Enter array size \n”; cin>>n;
cout<<”Enter sorted elements \n”;
for(i = 0 ; i < n ; i++)
cin>>a[i];
cout<<”Enter search element\n “;
cin>>ele;
}
void search :: logic( )
{
low = 0, high = n-1, loc = -1;
while (low <= high)
{ m = (low + high) / 2;
if (ele == a[m])
{ loc = m;
break;
}
else if(ele < a[m])
high = m-1;
else
low = m + 1;
}
}
void search :: output( )
{
if(loc >= 0)
cout<<”Element is found at location \n “<<loc;
else
cout<<”Element is not found\n”;
}
void main( )
{ clrscr( );
search o;
o.input( );
o.logic( );
o.output( );
getch( );
}
Page no 6
6. WAP to create a class with data members (Principal amount, Time and Rate of
interest). Create a member function to accept data values and compute simple interest.
#include <iostream.h>
#include <conio.h>
class si
{
float p, t, r, si;
public:
void input( );
void logic( );
void output( );
};
void si :: input( )
{
cout<<”Enter p, t, r values\n ”;
cin>>p>>t>>r;
}
void si :: logic( )
{
si = (p * t * r) /100;
}
void si :: output( )
{
cout<<”\n Simple interest = “<<si;
}
void main( )
{
clrscr( );
si o;
o.input( );
o.logic( );
o.output( );
getch( );
}
Page no 7
7. WAP to create a class with data members a, b, c and member functions to input data,
compute the discriminants based on the following conditions and print the roots.
• If discriminant = 0, print the roots are equal and their value.
• If discriminant > 0, print the real roots and their values.
• If discriminant < 0, print the roots are imaginary and exit the program.
#include <iostream.h>
#include <conio.h>
#include <math.h>
class quad
{
float a, b, c, d, r1, r2;
public: void input( );
void logic( );
void output( );
};
void quad :: input( )
{
cout<<“Enter a, b, c values \n”;
cin>>a>>b>>c;
}
void quad :: logic( )
{
d = b * b – 4 * a * c;
}
void quad :: output( )
{ if(d == 0)
{ r1 = r2 = -b / (2 * a);
cout<<”Roots are equal \n “;
cout<<”\n Root1 = ”<<r1;
cout<<”\n Root2 = ”<<r2;
}
else if(d > 0)
{ r1 = (-b + sqrt(d)) / (2 * a);
r2 = (-b - sqrt(d)) / (2 * a);
cout<<” Roots are real and different \n“;
cout<<” \n Root1 = “<<r1;
cout<<” \n Root2 = ”<<r2;
}
else
cout<<“\n Roots are imaginary ”;
}
void main( )
{
clrscr( );
quad o;
o.input( );
o.logic( );
o.output( );
getch( );
}
Page no 8
8. WAP to find the area of square, rectangle and triangle using function overloading.
#include <iostream.h>
#include <conio.h>
#include <math.h>
class fo
{
float s;
public:
float area(float a)
{
return(a * a);
}
void main( )
{
float x, y, z;
fo o;
clrscr( );
cout<<”Enter values\n”;
cin>>x>>y>>z;
cout<<“ \n Area of square = ”<<o.area(x);
cout<<“\n Area of rectangle = ”<<o.area(x, y);
cout<<“ \n Area of triangle = ”<<o.area(x, y, z);
getch( );
}
Page no 9
9. WAP to find cube of a number using inline function.
#include <iostream.h>
#include <conio.h>
class line
{
public:
inline int cube(int a)
{
return(a * a * a);
}
};
void main( )
{
clrscr( );
line o;
int n;
cout<<“Enter a number\n”;
cin>>n;
cout<<“\n Cube = ”<<o.cube(n);
getch( );
}
Page no 10
10. WAP to find sum of the series 1 + x + x2 + x3 + …. + xn using constructor.
#include <iostream.h>
#include <conio.h>
#include <math.h>
class series
{
int x, n, s, i;
public:
series(int p, int q)
{
x = p;
n = q;
}
void logic( )
{
s = 0;
for(i = 0; i <= n; i++)
s=s + pow(x, i);
}
void output( )
{
cout<<“\n Sum of series= ”<<s;
}
};
void main( )
{
int a, b;
clrscr( );
cout<<“Enter base and exponent value\n”;
cin>>a>>b;
series o(a, b);
o.logic( );
o.output( );
getch( );
}
Page no 11
11. WAP to create a base class containing the data members - roll number and name.
Also create a member function to read and display the data using the concept of single
level inheritance. Create a derived class that contains marks of two subjects and total
marks as data members.
#include <iostream.h>
#include <conio.h>
class std
{
int reg;
char name[10];
public:
void input1( )
{
cout<<"Enter register number and name \n";
cin>>reg>>name;
}
void output1( )
{
cout<<“\n Register No. = "<<reg;
cout<<" \n Name= "<<name;
}
};
class mar : public std
{
int m1, m2, t;
public:
void input2( )
{
cout<<”\n Enter sub1 and sub2 marks ";
cin>>m1>>m2;
}
void output2( )
{
t = m1 + m2;
cout<<"\n Total marks = "<<t;
}
};
void main( )
{ clrscr( );
mar m;
m.input1( );
m.input2( );
m.output1( );
m.output2( );
getch( );
}
Page no 12
12. WAP to create a class containing data members Register No, Name and Fees. Also
create a member function to read and display the data using the concept of pointers to
objects.
#include <iostream.h>
#include <conio.h>
class std
{
int reg;
char name[10];
float fees;
public:
void input( );
void output( );
};
void main( )
{
std *s;
clrscr( );
s -> input( );
s -> output( );
getch( );
}
Page no 13
13 & 14. WAP to push an item into the stack and pop an item from the stack.
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#define max 5
class stack
{
private:
int stak[max], top, i;
public:
stack( )
{
top = -1;
}
Page no 14
void pop( )
{
if(top < 0)
cout<<"Stack is empty";
else
{
for(i = top ; i >= 0 ; i--)
cout<<"Element"<<setw(3)<<stak[i]<<" "<<"pop out from stack"<<endl;
}
}
};
void main( )
{
clrscr( );
stack s;
int ele, n, i;
cout<<"Enter no of elements to be pushed in to queue";
cin>>n;
for(i = 0 ; i < n ; i++)
{
cout<<"Enter element to be pushed";
cin>>ele;
s.push(ele);
}
s.pop( );
getch( );
}
Page no 15
15. WAP to perform enqueue and dequeue operations on queue data structure.
#include <iostream.h>
#include <conio.h>
#include <iomanip.h>
#define max 5
class queue
{
private:
int q[max], f, r, i;
public:
queue( )
{
f = 0;
r =- 1;
}
Page no 17
16. WAP to create a linked list and appending nodes.
#include <iostream.h>
#include <conio.h>
class linked_list
{
private:
int ele; struct node
{
int data;
node *link;
}*start, *newnode, *temp;
public:
linked_list( )
{
start=NULL;
}
void append( )
{
cout<<"Enter element to insert"<<endl;
cin>>ele;
newnode = new node;
newnode -> data = ele;
newnode -> link = NULL;
if(start == NULL)
{
start = newnode;
cout<<ele<<" is inserted"<<endl;
}
else
{
temp = start;
while(temp -> link != NULL)
temp = temp-> link;
temp -> link = newnode;
cout<<ele<<" is inserted"<<endl;
}
}
Page no 18
void display( )
{
if(start == NULL)
{
cout<<"Linked list is empty"<<endl;
return;
}
cout<<"Linked list contains"<<endl;
temp = start;
while(temp != NULL)
{
cout<<temp -> data<< "->";
temp = temp -> link;
}
cout<<"NULL"<<endl;
}
};
void main( )
{
char choice;
int option;
linked_list l;
clrscr( );
do
{
cout<<"Enter:"<<endl;
cout<<"1 -> Append"<<endl;
cout<<"2 -> Display"<<endl;
cin>>option;
switch(option)
{
case 1 : l.append( );
break;
case 2 : l.display( );
break;
default : cout<<"Invalid choice"<<endl;
break;
}
cout<<"Do you wish to continue(y/n):";
cin>>choice;
}while(choice == 'y');
getch( );
}
Page no 19
SQL
17. Generate the electricity bill for customers.
1. Create a table for house hold electricity bill with the following fields.
Solution:
SQL > use mysql;
SQL > drop ebill;
SQL > create table ebill(rrno varchar(10), cname varchar(15), billdate date,
units int(3));
SQL > insert into ebill values (‘e101’, ‘arun’, ’ 2024/07/12’, 98);
SQL > insert into ebill values (‘e102’, ‘tarun’, ’ 2024/07/13’, 195);
SQL > insert into ebill values (‘e103’, ‘varun’, ’ 2024/07/14’, 158);
SQL > insert into ebill values (‘e104’, ‘anu’, ’ 2024/07/15’, 86);
SQL > insert into ebill values (‘e105’, ‘manu’, ’ 2024/07/06’, 128);
SQL > desc ebill;
SQL > alter table ebill add (billamt decimal (6, 2), duedate date);
SQL > update ebill set billamt = 50 + units * 4.50 where units <= 100;
SQL > update ebill set billamt = 50 + 100 * 4.50 + (units - 100) * 5.50 where units >100;
SQL > update ebill set duedate = billdate + 15;
SQL> select * from ebill;
Page no 20
18. Create a student database and compute the results.
1. Create a table for class of students with the following fields.
Solution:
SQL > use mysql;
SQL > drop stud;
SQL > create table stud(sid int(4), sname varchar(15), s1 int(3), s2 int(3), s3 int(3), s4 int(3),
s5 int(3), s6 int(3));
SQL > desc std;
SQL > insert into stud values (101, 'ANU', 56, 33, 56, 78, 44, 67);
SQL > insert into stud values (102, 'TANU', 76, 96, 56, 78, 94, 67);
SQL > insert into stud values (103, 'MANU', 56, 76, 56, 58, 44, 67);
SQL > insert into stud values (104, 'DHANU', 56, 30, 56, 78, 74, 77);
SQL > insert into stud values (105, 'BHANU', 56, 36, 56, 78, 94, 99);
SQL > alter table stud add (tot int(3), per decimal(6, 2), res varchar(10));
SQL > update stud set tot = s1 + s2 + s3 + s4 + s5 + s6;
SQL > update stud set per = tot /6;
SQL > update stud set res = ’PASS’ where(s1 >= 35 AND s2 >= 35 AND s3 >= 35
AND s4 >= 35 AND s5 >= 35 AND s6 >= 35);
SQL > update stud set res = ’FAIL’ where(s1 < 35 OR s2 < 35 OR s3 < 35 OR s4 < 35
OR s5 < 35 OR s6 < 35);
SQL > select * from stud where result = ‘PASS’;
SQL > select * from stud where result = ‘FAIL’;
SQL > select * from stud orderby sid;
SQL> select * from stud;
Page no 21
HTML
19. Write a HTML program to create study time table.
<html>
<head>
<body bgcolor = "green">
<center>
<h1> SCPUC </h1>
<h2> Study Time Table </h2>
<table border = "2" bgcolor = "orange" >
<tr>
<th> day </th>
<th> 8:00-9:00 </th>
<th> 9:00-10:00 </th>
<th> 10:30-11:30 </th>
<th> 11:30-12:30 </th>
<th> 12:30-1:30 </th>
</tr>
<tr>
<td> Monday </td>
<td> Computer Science </td>
<td> Maths </td>
<td> Chemistry </td>
<td> Physics </td>
<td> Kannada </td>
</tr>
<tr>
<td> Tuesday </td>
<td> Kannada </td>
<td> Maths </td>
<td> Physics </td>
<td> Chemistry </td>
<td> English </td>
</tr>
Page no 22
<tr>
<td> Wednesday </td>
<td> English </td>
<td> Physics </td>
<td> Computer Science </td>
<td> Maths </td>
<td> Kannada </td>
</tr>
<tr>
<td> Thursday </td>
<td> Kannada </td>
<td> Computer Science </td>
<td> Physics </td>
<td> Chemistry </td>
<td> English </td>
</tr>
<tr>
<td> Friday </td>
<td> English </td>
<td> Physics </td>
<td> Kannada </td>
<td> Computer Science </td>
<td> Chemistry </td>
</tr>
<tr>
<td> Saturday </td>
<td> English </td>
<td> Maths </td>
<td> Physics </td>
<td> Chemistry </td>
<td> Computer Science </td>
</tr>
</table>
</center>
</body>
</html>
Page no 23
20. Create an HTML program with table and form.
<html>
<body bgcolor = "green">
<center>
<h1>Application form</h1>
<table border = "2" bgcolor = "yellow">
<form>
<tr>
<td>Student Name</td>
<td><input type = "text"name = "sname"> </td>
</tr>
<tr>
<td>Contact number</td>
<td><input type = "text"name = "phone"> </td>
</tr>
<tr>
<td>Gender</td>
<td>
<input type = "radio" name = "gender" value = "Male"> male
<input type = "radio" name = "gender" value = "Female"> female
</td>
</tr>
<tr>
<td>select the course</td>
<td align = "left">
<select name = "drop down">
<option value = "CEBA">CEBA</option>
<option value = "PCMC">PCMC</option>
</select>
</td>
</tr>
<tr>
<td align = "right"> <input type = "button "value = "Submit"> </td>
<td align = "left"> <input type = "button "value = "Cancel"> </td>
</tr>
</table>
</form>
</center>
</body>
</html>
*** xxx ***
Page no 24