dk
dk
dk
PROGRAM 3
Write a C++ program to delete an element from an array from a given position.
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Deletion
{
private:
int a[10], n, p, i;
public:
void getdata( );
void del( );
void display( );
};
void Deletion::getdata( )
{
cout<<"Enter the size of the array";
cin>>n;
cout<<"Enter the elements for the array:";
for (i=0; i<n; i++)
cin>>a[i];
cout<<"\n Enter the position to an delete an element";
cin>>p;
}
void Deletion::del( )
{
if(p>n)
{
cout<<"array out of limits...!!!";
exit(0);
}
else
{
for(i=p; i<n; i++)
a[i] = a[i+1];
n = n-1;
}
}
void Deletion::display( )
{
cout<<" Array after deletion ";
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
}
void main( )
{
Deletion d;
clrscr( );
d.getdata( );
d.del( );
d.display( );
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>
class Sort
{
private:
int a[10], n, i;
public:
void getdata ( );
void insertionsort ( );
void display( );
};
void Sort::getdata ( )
{
cout<<"Enter the size of the array:";
cin>>n;
cout<<"Enter the elements of the array:";
for(i=0; i<n; i++)
cin>>a[i];
}
void Sort::insertionsort( )
{
int j, temp;
for(i=1; i<n; i++)
{
j = i;
while(j >= 1)
{
if(a[j] < a[j-1])
{
temp = a[j];
a[j] = a[j-1];
a[j-1] = temp;
}
j = j-1;
}
}
}
void Sort::display( )
{
cout<<"Sorted array is.....";
for(i=0; i<n; i++)
cout<<a[i]<<"\t";
}
void main( )
{
Sort s;
clrscr( );
s.getdata( );
s.insertionsort( );
s.display( );
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 Bsearch
{
private:
int a[10], n, i, ele, loc;
public:
void getdata( );
void search( );
void display( );
};
void Bsearch::getdata( )
{
cout<<"Enter the size of the array:";
cin>>n;
cout<<"\n Enter elements of the array in sorted order: ";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\n Enter the search element ";
cin>>ele;
}
void Bsearch :: search( )
{
int B=0, E=n-1, M;
loc=-1;
while (B <= E)
{
M = (B + E)/2;
if(ele == a[M])
{
loc = M;
break;
}
else
if (ele < a[M])
E = M - 1;
else
B = M + 1;
}
}
void Bsearch::display( )
{
if(loc >= 0)
cout<<"\n Element Found at Location: "<<loc;
else
cout<<"\n Element not found...";
}
void main( )
{
Bsearch s;
clrscr( );
s.getdata( );
s.bsearch( );
s.display( );
getch( );
}
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 SimpleInterest
{
private:
float p, r, t, si;
public:
void getdata( );
void display( );
};
void SimpleInterest::getdata( )
{
cout<<"Enter the Principal, Rate and Time";
cin>>p>>r>>t;
}
void SimpleInterest::display( )
{
si=(p * t * r) / 100;
cout<<"\n Simple Interest is = "<<si;
}
void main( )
{
SimpleInterest si;
clrscr( );
si.getdata( );
si.display( );
getch( );
}
PROGRAM 7
Write a C++ program to create a class with data members a, b, c and member functions to input data,
compute the discriminant 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 Quadratic
{
private:
int a, b, c, d;
double x, x1, x2;
public:
void getdata( );
void display( );
};
void Quadratic::getdata( )
{
cout<<"\n Enter the values for a, b, c";
cin>>a>>b>>c;
}
void Quadratic::display( )
{
d = b*b-4*a*c;
if(d = = 0)
{
cout<<"\n Equal Roots...";
x=-b / (2*a);
cout<<"\n Root is...."<<x;
}
else if (d > 0)
{
cout<<"\n Real and Distinct Roots...";
x1=(-b + sqrt (d)) / (2*a);
x2=(-b - sqrt (d)) / (2*a);
cout<<"\n Root 1 is "<<x1;
cout<<"\n Root 2 is "<<x2;
}
else
cout<<"\n Imaginary Roots...";
}
void main( )
{
Quadratic q;
clrscr( );
q.getdata( );
q.display( );
getch( );
}
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 Funoverload
{
public:
int area(int a)
{
return a*a;
}
float area(float l, float b)
{
return l*b;
}
float area(float a, float b, float c)
{
float s=(a+b+c)/2;
return sqrt(s*(s-a)*(s-b)*(s-c));
}
};
void main()
{
Funoverload f;
clrscr( );
cout<<"\n Area of square is = "<<f.area(5);
cout<<"\n Area of rectangle is = "<<f.area(4.0, 6.0);
cout<<"\n Area of triangle = "<<f.area(3.0, 4.0, 5.0);
getch( );
}
PROGRAM 9
Write a C++ program to find cube of a number using inline function.
#include<iostream.h>
#include<conio.h>
class assign
{
public:
inline int cube(int a)
{
return a*a*a;
}
};
void main( )
{
assign N;
int n;
clrscr( );
cout<<"Enter the input number";
cin>>n;
cout<<"\n The Cube = "<<N.cube(n);
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 sum, x, n;
public:
Series(int a, int b)
{
sum = 1;
x = a;
n = b;
}
int sumseries( );
};
int Series::sumseries( )
{
for(int i=1;i<=n;i++)
sum=sum+pow(x, i);
return sum;
}
void main()
{
int x,y;
clrscr( );
cout<<"Enter the value for Base (x) and Power (y) ";
cin>>x>>y;
Series s1(x, y);
Series s2 = s1;
cout<<"Sum of Series using Parameterized Constructor: "<<s1.sumseries( );
cout<<"Sum of Series using Copy Constructor: "<<s2.sumseries( );
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 readdata( )
{
cout<<"Enter the Register Number: ";
cin>>regno;
cout<<"Enter the Student Name:";
cin>>name;
}
void display( )
{
cout<<"\n Register Number :"<<regno;
cout<<"\n Student Name:"<<name;
}
};
class marks : public Student
{
private:
int m1, m2, total;
public:
void readmarks( )
{
cout<<"\n Enter Two Subjects Marks: ";
cin>>m1>>m2;
}
void compute( )
{
total = m1 + m2;
cout<<"\n Total Marks : "<<total;
}
};
void main( )
{
marks m;
clrscr( );
m.readdata( );
m.display( );
m.readmarks( );
m.compute( );
getch( );
}
PROGRAM 12
Create a class containing the following data members RegisterNo, 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 readdata( );
void display( );
};
void Student::readdata( )
{
cout<<"\n Enter the Register Number, Name and Fees:";
cin>>regno>>name>>fees;
}
void Student::display( )
{
cout<<"\n Register Number : "<<regno;
cout<<"\n Student Name : "<<name;
cout<<"\n Fees : "<<fees;
}
void main( )
{
Student s, *sp;
clrscr( );
sp=&s;
sp -> readdata( );
sp -> display( );
getch( );
}
2. DESC EBILL;
4. ALTER TABLE EBILL ADD (BILL_AMT NUMBER (8, 2), DUE_DATE DATE);
2. Display the description of the fields in the table using DESC command.
3. Add records into the table for 10 students for Student ID, Student Name and marks in 6 subjects using
INSERT command.
4. Alter the table add TOTAL NUMBER (3), PERCENTAGE NUMBER (5, 2) and RESULT
VARCHAR (8).
5. Calculate the TOTAL and PERCENTAGE.
6. Compute the RESULT as “PASS” or “FAIL” by checking if the student has scored more than 35 marks
in each subject.
7. List the contents of the table.
Solution:
1. CREATE TABLE STUDENT (SID NUMBER(4), SNAME VARCHAR(15), S1 NUMBER(3), S2
NUMBER(3), S3 NUMBER(3), S4 NUMBER(3), S5 NUMBER(3), S6 NUMBER(3));
2. DESC STUDENT;
3. INSERT INTO STUDENT VALUES (101, ‘ASHA’, 87, 78, 92, 77, 86, 90);
INSERT INTO STUDENT VALUES (102, ‘BHARATH’, 67, 58, 82, 67, 76, 80);
INSERT INTO STUDENT VALUES (103, ‘CHAITHANYA’, 85, 73, 87, 72, 81, 85);
INSERT INTO STUDENT VALUES (104, ‘DEEPTHI’, 97, 88, 92, 87, 96, 99);
INSERT INTO STUDENT VALUES (105, ‘ESHWAER’, 37, 28, 32, 27, 36, 40);
INSERT INTO STUDENT VALUES (106, ‘GIRISH’, 57, 48, 52, 47, 36, 50);
INSERT INTO STUDENT VALUES (107, ‘HARSHA’, 27, 28, 32, 37, 46, 40);
INSERT INTO STUDENT VALUES (108, ‘INDU’, 88, 78, 93, 78, 86, 92);
INSERT INTO STUDENT VALUES (109, ‘JANU’, 67, 68, 72, 77, 66, 70);
INSERT INTO STUDENT VALUES (110, ‘KUNAL’, 17, 28, 32, 37, 26, 40);
4. ALTER TABLE STUDENT ADD (TOTAL NUMBER (3), PERCENTAGE NUMBER (5, 2),
RESULT VARCHAR (8));
6. UPDATE STUDENT SET RESULT = ’PASS’ WHERE S1 >= 35 AND S2 >= 35 AND S3 >= 35 AND
S4 >= 35 AND S5 >= 35 AND S6 >= 35;
UPDATE STUDENT SET RESULT = ’FAIL’ WHERE S1 < 35 OR S2 < 35 OR S3 < 35 OR S4 < 35
OR S5 < 35 OR S6 < 35;