0% found this document useful (0 votes)
4K views24 pages

II PU Lab Programs 2024-25 Final

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

II PU Lab Programs 2024-25 Final

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

SESHADRIPURAM COMPOSITE PU COLLEGE

DEPARTMENT OF COMPUTER SCIENCE

II PU COMPUTER SCIENCE
PRACTICAL MANUAL

Page no 1
TABLE OF CONTENTS

1) Program to find the frequency of presence of an element in an array.

2) Program to insert an element into an array at a given position.

3) Program to delete an element from an array from a given position.

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.

6) Program to compute simple interest.

7) Program to compute the roots of quadratic equation.

8) Program to find the area of square, rectangle and triangle using function overloading.

9) Program to find cube of a number using inline function.

10) Program to find sum of the series 1 + x + x2 + x3 + …. + xn using constructor.

11) Program to illustrate the concept of single level inheritance.

12) Program to illustrate the concept of pointers.

13) Program to perform push operations on stack.

14) Program to perform pop operations on stack.

15) Program to perform enqueue and dequeue operations on queue data structure.

16) Program to create a linked list and appending nodes.

17) Generate the electricity bill for customers using database.

18) Create a student database and compute the results.

19) Write a HTML program to create study time table.

20) Write a HTML program with table and form.

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 freq :: input( )


{
cout<<“Enter array size\n ”;
cin>>n;
cout<<” Enter array elements \n”;
for(i=0 ; i<n ; i++)
cin>>a[i];
cout<<” Enter search element \n”;
cin>>ele;
}

void freq :: logic( )


{
c = 0;
for(i = 0 ; i < n ; i++)
if(a[i] == ele)
c++;
}

void freq :: output( )


{
if(c > 0)
cout<<” Frequency of the given element is \n“<<c;
else
cout<<” Element is not found \n”;
}

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( );
}; };

void ins :: input( ) void del :: input( )


{ cout<<” Enter array size\n ”; { cout<<” Enter array size\n ”;
cin>>n; cin>>n;
cout<<” Enter array elements\n ”; cout<<” Enter array elements\n ”;
for(i = 0 ; i < n ; i++) for(i = 0 ; i < n; i++)
cin>>a[i]; cin>>a[i];
cout<<”Enter element to be inserted \n ”; cout<<” Enter the position \n”;
cin>>ele; cin>>pos;
cout<<”Enter the position\n ”; }
}
void ins :: logic( ) void del :: logic( )
{ if(pos >= n) { if(pos >= n)
{ cout<<” Position is invalid\n ”; { cout<<” Position is invalid\n”;
getch( ); getch( );
exit(0); exit(0);
} }
for(i = n-1 ; i >= pos ; i--) for(i = pos ; i < n-1 ; i++)
a[i + 1] = a[i]; a[i] = a[i + 1];
a[pos] = ele; n--;
n++; }
}

void ins :: output( ) void del::output( )


{ cout<<”Array elements after insertion\n”; { cout<<”Array elements after deletion\n“;
for(i = 0 ; i < n ; i++) for(i = 0 ; i < n ; i++)
cout<<”\t”<<a[i]; cout<<”\t”<<a[i];
} }

void main( ) void main( )


{ clrscr( ); { clrscr( );
ins o; del o;
o.input( ); o.input( );
o.logic( ); o.logic( );
o.output( ); o.output( );
getch( ); getch( );
} }
Page no 4
4. WAP to sort the elements of an array in ascending order using insertion sort
#include <iostream.h>
#include <conio.h>
class sort
{
int n, i, j, t, a[10];
public: void input( );
void logic( );
void output( );
};
void sort :: input( )
{
cout<<”Enter array size \n ”;
cin>>n;
cout<<”Enter unsorted elements \n”;
for(i = 0 ; i < n ; i++)
cin>>a[i];
}
void sort :: logic( )
{
for(i = 1 ; i < n ; i++)
{
j = i;
while(j >= 1)
{
if(a[j] < a[j-1])
{ t = a[j];
a[j] = a[j-1];
a[j-1] = t;
}
j--;
}
}
}
void sort :: output( )
{
cout<<”Sorted array \n “;
for(i = 0 ; i < n ; i++)
cout<<”\t”<<a[i];
}
void main( )
{
clrscr( );
sort o;
o.input( );
o.logic( );
o.output( );
getch( );
}

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);
}

float area(float a, float b)


{
return(a * b);
}

float area(float a, float b, float c)


{
s = (a + b + c) / 2; //’s’ is semi perimeter
return(sqrt(s * (s - a) * (s - b) * (s - c)));
}
};

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 std :: input( )


{
cout<<" Enter Register number, Name and Fees \n";
cin>>reg>>name>>fees;
}

void std :: output( )


{
cout<<"\n Register number = “<<reg;
cout<<" \n Name = "<<name;
cout<<"\n Fees = "<<fees;
}

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;
}

void push(int ele)


{
if(top == max - 1)
{
cout<<"Stack is full"<<endl;
return;
}
else
{
top++;
stak[top] = ele;
cout<<ele<<"is pushed in to stack"<<endl;
}
cout<<"Stack contains following elements"<<endl;
for(i = 0 ; i <= top ; i++)
cout<<stak[i]<<endl;
}

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;
}

void enqu(int ele)


{
if(r == max-1)
{
cout<<"Queue is full"<<endl;
return;
}
else
{ r++;
q[r] = ele;
cout<<ele<<"is enque in to queue"<<endl;
}
cout<<"Queue contains following elements"<<endl;
for(i = 0 ; i <= r ; i++)
cout<<q[i]<<endl;
}
void dequ( )
{
if(f == r)
{
cout<<"queue is empty";
return;
}
else
{
for(f = 0 ; f <= r ; f++)
cout<<"Element"<<setw(3)<<q[f]<<" "<<"dequede from queue"<<endl;
}
}
};
Page no 16
void main( )
{
clrscr( );
queue q;
int ele,n,i;
cout<<"Enter no of elements to be inserted into
queue";
cin>>n;
for(i = 0; i < n; i++)
{
cout<<"Enter element to be inserted into queue" ;
cin>>ele;
q.enqu(ele);
}
q.dequ( );
getch( );
}

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.

Field Name Type


RRNO VARCHAR(5)
CNAME VARCHAR(15)
BILLDATE DATE
UNITS INT(4)

2. Insert 5 records into the table.


3. Check the structure of table and note your observation.
4. Add two fields to the table.
a. BILLAMT DECIMAL(6, 2)
b. DUEDATE DATE
5. Compute the bill amount for each customer as per the following rules.
a. MIN_AMT Rs. 50
b. First 100 units Rs 4.50/- per unit
c. > 100 units Rs. 5.50/- per unit
6. Compute due date as BILLDATE + 15 days
7. List all the bills generated.

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.

Field Name Type


SID INT(4)
SNAME VARCHAR(15)
S1 INT(3)
S2 INT(3)
S3 INT(3)
S4 INT(3)
S5 INT(3)
S6 INT(3)

2. Add 5 records into student table using above fields.


3. Display the description of the fields in the table using DESC command.
4. Alter the table and calculate TOTAL and PERC_MARKS.
5. Compute the RESULT as “PASS or “FAIL” by checking if the student has scored more
than 35 marks in each subject.
6. List the students who have result as “PASS”.
7. List the students who have result as “FAIL”.
8. Sort the table according to the order of ID_NO.
9. Retrieve all the records of the table.

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

You might also like