II PU - C.S. Lab Programs 2024
II PU - C.S. Lab Programs 2024
#include<iostream.h>
#include<conio.h>
class freq
{
private:
int a[50], n, ele, f, i;
public:
void getdata();
void process();
void putdata();
};
class insertion
{
private:
int a[50], n, pos, ele, i;
public:
void getdata();
void process();
void putdata();
};
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class sorting
{
private:
int a[50], n, temp, i, j;
public:
void getdata();
void process();
void putdata();
};
void sorting::getdata()
{
cout<<"Enter array size"<<endl;
cin>>n;
cout<<"Enter array elements"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
}
void sorting :: putdata()
void sorting :: process() {
{ cout<<“Array after sorting”<<endl;
for(i=1; i<n; i++) for(i=0; i<n; i++)
{ cout<<a[i]<<setw(5);
j=i; }
while(j>=1)
{ void main()
if(a[j]<a[j-1]) {
{ clrscr();
temp=a[j]; sorting obj;
a[j]=a[j-1];
obj.getdata( );
a[j-1]=temp; obj.process();
} obj.putdata();
j =j-1; getch( );
}}} }
PROGRAM 5:
Write a C++ program to search for a given element in an array using binary search method.
#include<iostream.h>
#include<conio.h>
class searching
{
private:
int a[50], n, ele, loc, b, e, m, i;
public:
void getdata();
void process();
void putdata();
};
#include<iostream.h>
#include<conio.h>
class si
{
private:
float p, t, r, s;
public:
void getdata()
{
cout<<"Enter Principal, Time and Rate"<<endl;
cin>>p>>t>>r;
}
void process()
{
s=(p*t*r)/100;
}
void putdata()
{
cout<<"Principal ="<<p<<endl;
cout<<"Time ="<<t<<endl;
cout<<"Rate ="<<r<<endl;
};
void main()
{
clrscr();
si obj;
obj.getdata();
obj.process();
obj.putdata();
getch( );
}
PROGRAM 7: Quadratic Equation
#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<process.h>
class quadratic
{
private:
double a, b, c, d, r1, r2;
public:
void getdata()
{
cout<<"Enter a, b, c values"<<endl;
cin>>a>>b>>c;
}
void putdata()
{
cout<<"Root1 =”<<r1<<endl;
cout<<"Root2 =”<<r2<<endl;
}
void process();
};
void quadratic :: process()
{
d=(b*b)–(4*a*c);
if(d == 0)
{
cout<<"roots are equal"<<endl;
r1=-b/(2*a);
r2=r1;
} void main()
else if (d>0) {
{ clrscr();
cout<<"Roots are different"<<endl;
quadratic obj;
r1=(-b+sqrt(d))/(2*a); obj.getdata();
r2=(-b-sqrt(d))/(2*a); obj.process();
}
obj.putdata();
else
getch();
{
}
cout<<"Roots are imaginary"<<endl;
getch( );
exit(0);
}}
PROGRAM 8:
Write a C++ program to find the area of square/ rectangle/ triangle using function
overloading.
#include<iostream.h>
#include<conio.h>
#include<math.h>
class functionoverload
{
public:
double area (double a)
{
return(a*a);
}
double area (double a, double b)
{
return(a*b); if(ch==1)
} {
double area (double a, double b, double c) cout<<"Enter one value"<<endl;
{
double s=(a+b+c)/2; cin>>x;
return (sqrt(s*(s-a)*(s-b)*(s-c))); cout<<"Area of Square="<<obj.area(x)<<endl;
} }
}; else if(ch==2)
void main() {
{ cout<<"Enter two values"<<endl;
clrscr( ); cin>>x>>y;
double x, y, z; cout<<"Area of rectangle="<<obj.area(x,y)<<endl;
int ch; }
else if(ch==3)
functionoverload obj;
{
cout<<"Enter 1 for square"<<endl;
cout<<"Enter three values"<<endl;
cout<<"Enter 2 for rectangle"<<endl;
cin>>x>>y>>z;
cout<<"Enter 3 for triangle"<<endl;
cout<<"Area of triangle="<<obj.area(x,y,z)<<endl;
cout<<"Enter your choice"<<endl; }
cin>>ch; else
cout<<"Invalid input";
getch( );
}
PROGRAM 9:
#include<iostream.h>
#include<conio.h>
class cube
{
private:
int n;
public:
void getdata()
{
cout<<"Enter one number"<<endl;
cin>>n;
}
void process();
};
inline void cube:: process()
{
cout<<”Cube=“<<(n*n*n);
}
void main()
{
clrscr();
cube obj;
obj.getdata( );
obj.process();
getch( );
}
PROGRAM 10:
Write a C++ program to find sum of the series 1+x+x2+x3+ ….xn using constructors.
#include<iostream.h>
#include<conio.h>
#include<math.h>
class series
{
private:
int s, x, n;
public:
series()
{
s=1;
}
void getdata()
{
cout<<"Enter x and n values"<<endl;
cin>>x<<n;
}
void process();
};
void series::process()
{
for( int i=1; i<=n; i++)
s=s+ pow(x,i);
cout<<”Sum= “<<sum;
}
void main()
{
clrscr();
series obj;
obj.getdata();
obj.process();
getch( );
}
PROGRAM 11:
Create a base class containing the data member 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 the data
members.
#include<iostream.h>
#include<conio.h>
class student
{
private:
int regno;
char name[20];
public:
void getdata1()
{
cout<<"Enter Register Number"<<endl;
cin>>regno;
cout<<"Enter Name"<<endl;
cin>>name;
}
void putdata1()
{
cout<<"Register Number ="<<regno<<endl;
cout<<"Student Name ="<<name<<endl;
}
}; void putdata2()
class marks: public student
{
{ cout<<"Marks 1 ="<<m1<<endl;
private: cout<<"Marks 2 ="<<m2<<endl;
int m1, m2, tot; cout<<"Total ="<<tot<<endl;
public: }};
void getdata2()
void main()
{
{
cout<<"Enter marks1"<<endl;
clrscr();
cin>>m1;
marks obj;
cout<<"Enter marks2"<<endl; obj.getdata1();
cin>>m2; obj.getdata2();
tot=m1+m2; obj.putdata1();
} obj.putdata2();
getch( );
}
PROGRAM 12:
Create a class containing the following 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 student
{
private::
int regno;
char name[20];
float fees;
public:
void getdata()
{
cout<<"Enter Register Number"<<endl;
cin>>regno;
cout<<"Enter Name"<<endl;
cin>>name;
cout<<"Enter Fees:"<<endl;
cin>>fees;
}
void putdata()
{
cout<<"Register Number ="<<regno<<endl;
cout<<"Name ="<<name<<endl;
cout<<"Fees ="<<fees<<endl;
}
};
void main()
{
clrscr();
student s,*sp;
sp = &s;
sp->getdata();
sp->putdata();
getch( );
}
Write a HTML program to create Study Time Table.
<html>
<head>
<title>My study time table</title>
</head>
<body bgcolor= “pink”>
<center><h1>My study time table</h1>
<table border= “2”>
<tr>
<th>Day</th>
<th>Subject</th>
<th>Morning study time</th>
<th>College time</th>
<th>Evening study time</th>
<th>Question paper solution Time</th>
</tr>
<tr>
<td>Monday</td>
<td>Computer Science</td>
<td>5.00-6.30a.m.</td>
<td>8.00-4.00p.m.</td>
<td>6.00-9.00p.m.</td>
<td>10.00-11.00p.m.</td>
</tr>
………
</table>
<marquee>All the best</marquee>
</body></html>
Solution:
1) Command to create a table
create table elect_bill (rr_number varchar(5), consumer_name varchar(25), date_billing date,
units number(4));
desc elect_bill;
2) Command to insert records into the table
insert into elect_bill values(‘A1001’,‘Nishanth’,’01-Nov-23’,58);
Select * from elect_bill;
3) desc elect_bill;
Solution:
1) Command to create a table
create table student(id number(2),s1 number(2),s2 number(2),s3 number(2));
desc student;
2) Command to insert records into the table
insert into student values(1,98, 88, 58);
select * from student;
3) desc student;
4) Command to add three more columns to table
alter table student add (t number(3),p number(3),r varchar(1));
5) Command to calculate total
update student set t=s1+s2+s3;
select * from student;
Command to calculate percentage
update student set p=t/6;
select*from student;
6) Command to calculate result as pass
update student set r=‘p’ where(s1>=35ands2>=35ands3>=35);
select * from student;
14) Command to count the number of students who have percentage more than 60
select * from student where p>=60;