0% found this document useful (0 votes)
32 views17 pages

II PU - C.S. Lab Programs 2024

Uploaded by

chetanpadhiar7
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)
32 views17 pages

II PU - C.S. Lab Programs 2024

Uploaded by

chetanpadhiar7
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/ 17

PROGRAM 1:

Write a C++ program to find the frequency of presence of an element in an array.

#include<iostream.h>
#include<conio.h>
class freq
{
private:
int a[50], n, ele, f, i;
public:
void getdata();
void process();
void putdata();
};

void freq :: getdata()


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

void freq :: process()


{
f=0;
for(i=0; i<n; i++)
{
if(ele==a[i])
f++; void main()
}} {
clrscr();
void freq :: putdata() freq obj;
{ obj.getdata( );
if(f > 0) obj.process();
cout<<"Frequency = "<<f ; obj.putdata();
else getch( );
cout<<" Element not found"; }
}
PROGRAM 2:
Write a C++ program to insert an element into an array at a given position.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>

class insertion
{
private:
int a[50], n, pos, ele, i;
public:
void getdata();
void process();
void putdata();
};

void insertion :: getdata()


{
cout<<"Enter array size"<<endl;
cin>>n;
cout<<"Enter array elements"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<"Enter element and position"<<endl;
cin>>ele>>pos;
}
void insertion :: process() void insertion :: putdata()
{ {
if(pos > n-1) cout<<"Array after insertion"<<endl;
{ for(i=0; i<n; i++)
cout<<"Invalid index"; cout<<a[i]<<setw(3);
getch(); }
exit(0);
} void main()
for(i=n-1; i>=pos; i--) {
a[i+1]=a[i]; clrscr();
a[pos]=ele; insertion obj;
obj.getdata();
n=n+1;
obj.process();
cout<<ele<< “is added to array”; obj.putdata();
} getch( );
}
PROGRAM 3:
Write a C++ program to delete an element from an array from a given position.
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
#include<process.h>
class deletion
{
private:
int a[50], n, pos, ele, i;
public:
Void getdata();
void process();
void putdata();
};

void deletion :: getdata()


{
cout<<"Enter array size"<<endl;
cin>>n;
cout<<"Enter array elements"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<"Enter position"<<endl;
cin>>pos;
}

void deletion :: process() void deletion :: putdata()


{ {
if(pos>n-1) cout<<"Array after deletion"<<endl;
{ for(i=0; i<n; i++)
cout<<"Invalid index"; cout<<a[i]<<setw(4);
getch( ); }
exit(0); void main()
} {
clrscr();
ele=a[pos]; deletion obj;
for(i=pos; i<n; i++) obj.getdata( );
a[i] = a[i+1]; obj.process();
n=n-1; obj.putdata();
cout<<ele<<”got deleted”<<endl; getch( );
} }
PROGRAM 4:
Write a C++ program to sort the element of an array in ascending order using insertion
sort.

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

void searching :: getdata()


{
cout<<"Enter array size"<<endl;
cin>>n;
cout<<"Enter array elements in sorted order"<<endl;
for(i=0; i<n; i++)
cin>>a[i];
cout<<"Enter search element"<<endl;
cin>>ele;
}
void sorting :: putdata()
void searching :: process()
{
{
if (loc>=0)
loc=-1;
b=0; cout<<ele<<”found at index” <<loc;
e = n-1; else
while(b<=e) cout<<ele<<”not found”;
{ }
m = (b+e)/2;
if(ele==a[m])
void main()
{
{
loc = m;
break; clrscr();
} searching obj;
else
{ obj.getdata( );
If(ele<a[m]) obj.process();
e=m-1; obj.putdata();
else getch( );
b=m+1; }
}}}
PROGRAM 6:
Write a C++ program to create a class with data members principal, time and rate.
Create a member function to accept data values, to compute simple interest and to display
the result.

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

cout<<"Simple Interest ="<<s<<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:

Write a C++ program to find cube of a number using inline function.

#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>

Study Time Table


Morning Evening Question
Day Subject College Time
Study Time Study paper solution
Time Time
Monday Computer Science 5.00 - 6.30 a.m. 8.00 - 4.00 p.m. 6.00 -9.00 p.m. 10.00-11.00p.m.
Tuesday Physics 5.00 - 6.30 a.m. 8.00 - 4.00 p.m. 6.00 -9.00 p.m. 10.00-11.00p.m.
Wednesday Chemistry 5.00 -6.30 a.m. 8.00 - 4.00 p.m. 6.00 -9.00 p.m. 10.00-11.00p.m.
Thursday Mathematics 5.00 - 6.30 a.m. 8.00 - 4.00 p.m. 6.00 -9.00 p.m. 10.00-11.00p.m.
Friday English 5.00 - 6.30 a.m. 8.00 - 4.00 p.m. 6.00 -9.00 p.m. 10.00-11.00p.m.
Saturday Language 5.00 - 6.30 a.m. 8.00 - 4.00 p.m. 6.00 -9.00 p.m. 10.00-11.00p.m.
Create an html program with table and form.
<html>
<head>
<title>Application Form</title>
</head>
<body bgcolor="pink">
<form>
<center><h1>Application Form</h1></center>
<table border="2"cellpadding="8">
<tr>
<td>Name</td>
<td><input type=“text” name=“sname” size=“25”></td>
</tr>
<tr>
<td>Date of Birth</td>
<td><input type=“text” name=“dob” size=“25”></td>
</tr>
<tr>
<td>Gender</td>
<td><input type="radio" name="gender">Male
<input type="radio" name="gender">Female</td>
</tr>
<tr>
<td>Languages</td>
<td><input type="checkbox" name="Eng">English
<input type="checkbox" name="san">Sanskrit
<input type="checkbox" name="kan">Kannada
<input type="checkbox" name="hin">Hindi</td>
</tr>
<tr>
<td>Courses</td>
<td><select>
<option>CEBA</option>
<option>SEBA</option>
<option>PCMC</option>
<option>PCMB</option>
</select></td>
</tr>
<tr>
<td><input type="submit" name="submit" value="Submit"></td>
<td><input type="reset" name="reset" value="Reset"></td>
</tr>
</table>
</form></body></html>
Electricity Bill table
Create Electricity Bill table:
1. Create a table by name elect_bill with the following fields:
Fieldname Type
rr_number varchar(10)
consumer_name varchar(25)
date_billing date
units number(4)
2. Insert 5 records into the table.
3. Check the structure of the table.
4. Add two fields to the table:
 amount number(8,2)
 due_date date
5. Compute the amount as:
 minimum amount Rs.50
 first 100 units Rs.4.50/unit
 >100 units Rs.5.50/unit
6. Compute due date as billing_date + 15days.
7. List the contents of the table.

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;

4) Command to add two more columns to table


alter table elect_bill add(amount number(8,2), due_date date);
5) Command to calculate amount
update elect_bill set amount=50;
update elect_bill set amount=amount+(units*4.50) where units<=100;
update elect_bill set amount=amount+(100*4.50)+(units-100)*5.50 where units>100;
Select * from elect_bill;
6) Command to calculate due_date
Update elect_bill set due_date=date_billing+15;

7) Select * from elect_bill;


Student Table

Create Students marks table: Name Type


1. Create a table by name student with the following details: id number(2)
2. Insert 5 records into the table. s1 number(2)
3. Check the structure of the table. s2 number(2)
4. Add three fields to the table: s3 number(2)
 t number(3)
 p number(3)
 r varchar(1)
5. Compute total and percentage.
6. Compute result as ‘p’ if student has scored 35 and above in all three subjects.
7. Compute result as ‘f’ if student has scored less than 35 in anyone of the three subjects.
8. List the contents of the table.
9. Retrieve only id of all the students.
10. List the students who have result as pass.
11. List the students who have result as fail.
12. Count the number of students who have passed.
13. Count the number of students who have failed.
14. List the students who have percentage more than 60%.
15. Sort the table in ascending order of id.

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;

7) Command to calculate result as fail


update student set r= ‘f’ where(s1<35ors2<35ors3<35);
select * from student;
8) select * from student;
9) Command to retrieve only id of all the students
select id from student;

10) Command to list the students who have result as “pass”


select * from student where r= ’p’;

11) Command to list the students who have result as “fail”


select * from student where r= ’f’;

12) Count the number of students who have passed


select count(*) from student where r= ’p’;
count(*)
4

13) Command to count the number of students who have failed


select count(*) from student where r= ’f’;
count(*)
1

14) Command to count the number of students who have percentage more than 60
select * from student where p>=60;

15) Command to sort the student list according to s_id


select * from student order by id;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy