DS With C++ Spiral
DS With C++ Spiral
(Autonomous)
Reaccredited and Graded ‘A’ by NAAC
(Affiliated to Madurai Kamaraj University)
ARUPPUKOTTAI - 626101
Name :
Reg. No. :
Subject : DATA STRUCTURES WITH C++ LAB
Subject Code : (17CS3R)
DEVANGA ARTS COLLEGE
(Autonomous)
Reaccredited and Graded ‘A’ by NAAC
(Affiliated to Madurai Kamaraj University)
ARUPPUKOTTAI - 626101.
NAME :
REGISTER No. :
PAGE
EX.NO. DATE TITLE SIGN
NO.
C++ Programs
1 18-06-19 Number Checking using Control Structure 02
1
Ex. No.: 01 NUMBER CHECKING USING CONTROL STRUCTURE
2
18-06-19
AIM
To write a C++ program for Number Checking using Control Structure.
PROGRAM:
#include<iostream.h>
#include<conio.h>
void main()
{
int ch;
clrscr();
do
{
clrscr();
cout<<endl<<"***************************************";
cout<<endl<<"NUMBER CHECKING USING CONTROL STRUCTURE";
cout<<endl<<"***************************************";
cout<<endl<<"1:ODD OR EVEN";
cout<<endl<<"2:POSITIVE OR NEGETIVE";
cout<<endl<<"3:FACTORIAL NUMBER";
cout<<endl<<"4:ARMSTRONG OR NOT ARMSTRONG";
cout<<endl<<"5:EXIT";
cout<<endl<<"****************************************";
cout<<endl<<" Enter your choice";
2
cin>>ch;
switch(ch)
{
case 1:
int a;
cout<<"enter a value:";
cin>>a;
if(a%2==0)
cout<<endl<<a<<" is even";
else
cout<<endl<<a<<" is odd";
break;
case 2:
int b;
cout<<"Enter B Value:";
cin>>b;
if(b>0)
cout<<endl<<b<<" is positive";
else if(b<0)
cout<<endl<<b<<" is negative";
else
cout<<endl<<b<<" is zero";
break;
case 3:
int c,i;
long int f=1;
cout<<"Enter C Value:";
3
cin>>c;
for(i=1;i<=c;i++)
f=f*i;
cout<<endl<<" Factorial is:"<<f;
break;
case 4:
int g,d,e,sum=0;
cout<<"enter d value:";
cin>>d;
g=d;
while(d>0)
{
e=d%10;
sum=sum+e*e*e;
d=d/10;
}
if(g==sum)
cout<<endl<<sum<<" is amstrong number ";
else
cout<<endl<<sum<<" is not amstrong number ";
break;
case 5:
cout<<endl<<" exit the program ";
break;
default:
cout<<endl<<" invalid choice ";
}
4
getch();
} while(ch!=5);
getch();
}
5
Output:
6
RESULT:
Thus the above c++ program Number Checking using Control Structure was executed and
the output was verified successfully.
7
Ex. No.: 02
BOOK DETAILS USING CLASS AND OBJECT 8
21-06-19
AIM:
To write a C++ program for Book Details using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class book
{
private:
char bn[100],an[100],pub[100];
int ed,pr;
public:
void getdata()
{
cout<<endl<<"**********************************";
cout<<endl<<"BOOK DETAILS USING CLASS & OBJECT ";
cout<<endl<<"*********************************";
cout<<endl<<"Enter Book Name:";
cin>>bn;
cout<<endl<<"Enter Author Name:";
cin>>an;
cout<<endl<<"Enter The Publication:";
cin>>pub;
8
cout<<endl<<"Enter The Edition:";
cin>>ed;
cout<<endl<<"Enter The Price:";
cin>>pr;
}
void putdata();
};
void book::putdata()
{
cout<<endl<<"Book Name is:"<<bn;
cout<<endl<<"Author Name is:"<<an;
cout<<endl<<"Publication is:"<<pub;
cout<<endl<<"Edition:"<<ed;
cout<<endl<<"Price is:"<<pr;
}
void main()
{
clrscr();
book bk;
bk.getdata();
bk.putdata();
getch();
}
9
OUTPUT:
RESULT:
Thus the above C++ program Book Details using Class and Object was executed and the
output was verified successfully.
10
Ex. No.: 03
PRIME NUMBER USING CLASS AND OBJECT 11
25-06-19
Aim:
To write a C++ program for Prime Number using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class prime
{
private:
int i,k,f;
public:
void get()
{
cout<<endl<<"Enter A Value:";
cin>>k;
}
void check(void);
void put(void);
};
void prime::check()
{
11
f=0;
for(i=2;i<=k/2;i++)
{
if(k%i==0)
f=1;
}
}
void prime::put()
{
if(f==0)
cout<<endl<<"given number is prime:"<<k;
else
cout<<endl<<"given number is not prime:"<<k;
}
void main()
{
clrscr();
cout<<endl<<"************************************";
cout<<endl<<"PRIME NUMBER USING CLASS AND OBJECTS";
cout<<endl<<"************************************";
prime p;
p.get();
p.check();
p.put();
getch();
}
12
Output:
RESULT:
Thus the above C++ program Prime Number using Class and Object was executed and the
output was verified successfully.
13
Ex. No.: 04
STUDENT MARK LIST USING ARRAYS OF OBJECT 14
01-07-19
AIM:
To write a C++ program for Student Mark List using Arrays of Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class student
{
private:
int rno,m1,m2,m3,m4,tot;
float avg;
char n[30],r[30];
public:
void getdata()
{
cout<<endl<<"ENter The Student roll no:";
cin>>rno;
cout<<endl<<"Enter The Student Name:";
cin>>n;
14
cout<<endl<<"Enter The Mark1:";
cin>>m1;
cout<<endl<<"Enter The Mark2:";
cin>>m2;
cout<<endl<<"Enter The Mark3:";
cin>>m3;
cout<<endl<<"Enter The Mark4:";
cin>>m4;
}
void calc();
void putdata();
};
void student::calc()
{
tot=m1+m2+m3+m4;
avg=(float)tot/4;
if(m1>=40 &&m2>=40 &&m3>=40 &&m4>=40)
strcpy(r,"PASS");
else
strcpy(r,"FAIL");
}
void student::putdata()
{
cout<<endl<<rno<<"\t"<<n<<"\t"<<m1<<"\t"<<m2<<"\t"<<m3<<"\t"<<m4<<"\t"<<tot<<"\t"<<av
g<<"\t"<<r<<endl;
}
const int size=2;
15
void main()
{
clrscr();
cout<<endl<<"****************************************";
cout<<endl<<"STUDENT MARK LIST USING ARRAYS OF OBJECTS";
cout<<endl<<"*****************************************";
student s[20];
for(int i=0;i<size;i++)
{
s[i].getdata();
s[i].calc();
}
cout<<endl<<"Rno\tname\tMark1\tMark2\tMark3\tMark4\ttTot\tAvg\tResult\n";
for(i=0;i<size;i++)
{
s[i].putdata();
}
getch();
}
16
OUTPUT:
RESULT:
Thus the above C++ program Student Mark List using Arrays of Object was executed and the
output was verified successfully.
17
Ex. No.: 05
AREA OF SHAPES USING FUNCTION OVERLOADING 18
03-07-19
AIM:
To write a C++ program for Area of Shapes using Function Overloading.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int area(int);
int area(int,int);
double area(double);
void main()
{
int a,l,b,ch;
double r;
clrscr();
do
{
clrscr();
cout<<endl<<"*******************************************";
cout<<endl<<"AREA OF SHAPES USING FUNCTION OVER LOADING";
cout<<endl<<"*******************************************";
cout<<endl<<"1-SQUARE";
cout<<endl<<"2-RECTANGLe";
18
cout<<endl<<"3-CIRCLE";
cout<<endl<<"4-EXIT";
cout<<endl<<"Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<endl<<"Enter The Side Square:";
cin>>a;
cout<<endl<<"Area Of Square is:"<<area(a);
break;
case 2:
cout<<endl<<"Enter The Length Of Rectangle:";
cin>>l;
cout<<endl<<"Enter The Breadth Of Rectangle:";
cin>>b;
cout<<endl<<"Area Of Rectangle is:"<<area(l,b);
break;
case 3:
cout<<endl<<"Enter The Radius Of Circle:";
cin>>r;
cout<<endl<<"Area Of Circle is:"<<area(r);
break;
case 4:
cout<<endl<<"Exit The Program";
break;
default:
19
cout<<endl<<"Invalid Choice";
}
getch();
}
while(ch!=4);
getch();
}
int area(int a)
{
return(a*a);
}
int area(int l,int b)
{
return(l*b);
}
double area(double r)
{
return(3.14286*r*r);
}
20
OUTPUT:
21
RESULT:
Thus the above C++ program Area of Shapes using Function Overloading was executed and
the output was verified successfully.
22
Ex. No.:06
STRING FUNCTIONS USING FUNCTION OVERLOADING 23
09-07-19
AIM:
To write a C++ program for String Functions using Function Overloading.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
void strfun();
int strfun(char a[30]);
void strfun(char a[30],char b[30]);
void strfun(char a[30],char b[30],char c[30]);
void main()
{
int ch;
char a[30],b[30],c[30];
clrscr();
do
{
clrscr();
cout<<endl<<"*******************";
cout<<endl<<"string function";
cout<<endl<<"******************";
cout<<endl<<"1.reverse";
23
cout<<endl<<"2.length";
cout<<endl<<"3.comparison";
cout<<endl<<"4.concatenation";
cout<<endl<<"5.exit";
cout<<endl<<"enter your choice:";
cin>>ch;
switch(ch)
{
case 1:
strfun();
break;
case 2:
cout<<endl<<"enter any string:";
cin>>a;
cout<<endl<<"length of string "<<a<<"is:"<<strfun(a);
break;
case 3:
cout<<endl<<"enter first string:";
cin>>a;
cout<<endl<<"enter second string:";
cin>>b;
strfun(a,b);
break;
case 4:
cout<<endl<<"enter three string:";
cin>>a>>b>>c;
strfun(a,b,c);
24
break;
case 5:
cout<<endl<<"exit the program";
break;
default:
cout<<endl<<"invalid choice";
}
getch();
}while(ch!=5);
getch();
}
void strfun()
{
char a[30];
cout<<endl<<"enter any strings:";
cin>>a;
cout<<endl<<"reverse string is:"<<strrev(a);
}
int strfun(char a[30])
{
return(strlen (a));
}
void strfun(char a[30],char b[30])
{
if(strcmp(a,b)==0)
cout<<endl<<"both strings are equal";
else
25
cout<<endl<<"both strings are not equal";
}
void strfun(char a[30],char b[30],char c[30])
{
strcat(a,b);
strcat(a,c);
cout<<endl<<"concatenation of three strings:"<<a;
}
26
OUTPUT:
27
RESULT:
Thus the above C++ program String Functions using Function Overloading was executed and
the output was verified successfully.
28
Ex. No.: 07
SWAPPING TWO NUMBERS USING FRIEND FUNCTION 29
11-07-19
AIM:
To write a C++ program for Swapping two numbers using Friend Function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class class2;
class class1
{
private:
int v1;
public:
void indata(int a)
{
v1=a;
}
void display(void)
{
cout<<v1<<endl;
}
friend void exchange(class1 &,class2 &);
};
class class2
{
29
private:
int v2;
public:
void indata(int b)
{
v2=b;
}
void display(void)
{
cout<<v2<<endl;
}
friend void exchange(class1 &,class2 &);
};
void exchange(class1 &x,class2 &y)
{
int t=x.v1;
x.v1=y.v2;y.v2=t;
}
void main()
{
int m,n;
clrscr();
cout<<endl<<"***********************************************";
cout<<endl<<"Swapping Of Two numbers Using Friend Function";
cout<<endl<<"***********************************************";
cout<<endl<<"Enter The First Value:";
cin>>m;
30
cout<<endl<<"Enter The Second Value:";
cin>>n;
class1 c1;
class2 c2;
c1.indata(m);
c2.indata(n);
cout<<endl<<"Before Swapping:"<<endl;
c1.display();
c2.display();
exchange(c1,c2);
cout<<endl<<"After Swapping:"<<endl;
c1.display();
c2.display();
getch();
}
31
OUTPUT:
RESULT:
Thus the above C++ program Swapping two numbers using Friend Function was executed
and the output was verified successfully.
32
Ex. No.: 08
MEAN OF FIVE NUMBERS USING FRIEND FUNCTION 33
17-07-19
AIM:
To write a C++ program for Mean of five numbers using Friend Function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class class2;
class class1
{
private:
int a,b,c;
public:
void setvalue()
{
cout<<endl<<"Enter Three Values:";
cin>>a>>b>>c;
}
friend float mean(class1,class2);
};
class class2
{
33
private:int d,e;
public:
void setvalue()
{
cout<<endl<<"Enter Two Values:";
cin>>d>>e;
}
friend float mean(class1,class2);
};
float mean(class1 x,class2 y)
{
return(float)(x.a+x.b+x.c+y.d+y.e)/5;
}
void main()
{
clrscr();
cout<<endl<<"******************************************";
cout<<endl<<"MEAN OF FIVE NUMBER USING FRIEND FUNCTION";
cout<<endl<<"******************************************";
class1 c1;
class2 c2;
c1.setvalue();
c2.setvalue();
cout<<endl<<"Averge Of Five Numbers is:"<<mean(c1,c2);
getch();
}
34
OUTPUT:
RESULT:
Thus the above C++ program Mean of five numbers using Friend Function was executed and
the output was verified successfully.
35
Ex. No.: 09
FILE OPERATIONS 36
19-07-19
AIM:
To write a C++ program for File Operations.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<fstream.h>
void main()
{
char c,name[30];
ofstream of;
clrscr();
cout<<endl<<"****************";
cout<<endl<<"FILE OPERATIONS";
cout<<endl<<"*****************";
cout<<endl<<"Enter The File Name:";
cin>>name;
of.open(name);
cout<<endl<<"Enter The Content To Store In A File:(Enter # at End)";
while((c=getchar())!='#')
{
of<<c;
}
36
of.close();
ifstream ifs;
ifs.open(name);
cout<<endl<<"Contents of file:"<<name<<endl;
do
{
ifs.get(c);
cout<<c;
}
while(ifs);
getch();
}
37
OUTPUT:
RESULT:
Thus the above C++ program File Operations was executed and the output was verified
successfully.
38
Ex. No.: 10
STRINGS SORTING USING COMMAND LINE ARGUMENTS 39
19-07-19
AIM:
To write a C++ program for Strings Sorting using Command Line Arguments.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
#include<string.h>
int main(int argc,char * argv[])
{
char k[30][30],t[30];
int i,j;
clrscr();
cout<<endl<<"********************";
cout<<endl<<"COMMAND LINE ARGUMENTS";
cout<<endl<<"*********************";
if(argc==1)
{
cout<<endl<<"You Must Pass Arguments ";
exit(0);
}
cout<<endl<<"Before Sorting:";
for(i=1;i<argc;i++)
{
39
strcpy(k[i],argv[i]);
cout<<"\t"<<k[i];
}
for(i=1;i<argc;i++)
{
for(j=i+1;j<argc;j++)
{
if(strcmp(k[i],k[j])>0)
{
strcpy(t,k[i]);
strcpy(k[i],k[j]);
strcpy(k[j],t);
}
}
}
cout<<endl<<"After Sorting:";
for(i=1;i<argc;i++)
{
cout<<"\t"<<k[i];
}
return 0;
}
40
OUTPUT:
Alt + F9 – Compile
Ctrl + F9 – Run
Alt F + D – Dos Shell Screen
RESULT:
Thus the above C++ program Strings Sorting using Command Line Arguments was executed
and the output was verified successfully.
41
Ex. No.: 11
FIBONACCI SERIES USING CONSTRUCTOR OVERLOADING 42
25-07-19
AIM:
To write a C++ program for Fibonacci Series using Constructor Overloading.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class fibon
{
int p,q,r,k;
public:
fibon()
{
p=-1;
q=1;
}
fibon(int x);
fibon(fibon &i,fibon &j);
~fibon()
{
cout<<endl<<"Object is Destroyed...";
}
};
42
fibon::fibon(int x)
{
k=x;
}
fibon::fibon(fibon &i,fibon &j)
{
p=i.p;
q=i.q;
k=j.k;
cout<<endl<<"Fibonacci series";
for(int s=1;s<=k;s++)
{
r=p+q;
cout<<endl<<r;
p=q;
q=r;
}
}
int main()
{
int a;
clrscr();
cout<<endl<<"**********************************************";
cout<<endl<<"fibonacci series using constructor overloading";
cout<<endl<<"**********************************************";
cout<<endl<<"Enter The Number Of Series:";
cin>>a;
43
fibon x;
fibon y(a);
fibon z(x,y);
return 0;
}
44
OUTPUT:
Alt + F9 – Compile
Ctrl + F9 – Run
Alt + F5 -- Show the Last Output
RESULT:
Thus the above C++ program Fibonacci Series using Constructor Overloading was executed
and the output was verified successfully.
45
Ex. No.: 12 SUM OF TWO COMPLEX NUMBERS USING BINARY
46
OPERATOR OVERLOADING
27-07-19
AIM:
To write a C++ program Sum of two Complex numbers using Binary Operator Overloading.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class complex
{
float x,y;
public:
complex()
{}
complex(float real,float imag)
{
x=real;
y=imag;
}
complex operator+(complex);
void display(void);
};
complex complex::operator+(complex c)
46
{
complex temp;
temp.x=x+c.x;
temp.y=y+c.y;
return(temp);
}
void complex::display(void)
{
cout<<x<<"+i"<<y<<endl;
}
void main()
{
float r1,r2,i1,i2;
clrscr();
cout<<endl<<"********************";
cout<<endl<<"Two Complex Numbers";
cout<<endl<<"********************";
cout<<endl<<"Enter Real Part Of First Complex Number:";
cin>>r1;
cout<<endl<<"Enter Imaginary Part Of First Complex Number:";
cin>>i1;
cout<<endl<<"Enter Real Part Of Second Complex Number:";
cin>>r2;
cout<<endl<<"Enter Imaginary Part Of Second Complex Number:";
cin>>i2;
complex c1,c2,c3;
c1=complex(r1,i1);
47
c2=complex(r2,i2);
c3=c1+c2;
cout<<endl<<"c1=";c1.display();
cout<<endl<<"c2=";c2.display();
cout<<endl<<"c3=";c3.display();
getch();
}
OUTPUT:
RESULT:
Thus the above C++ program Sum of two Complex numbers using Binary Operator
Overloading was executed and the output was verified successfully.
48
Ex. No.: 13
CONCATENATION OF TWO STRINGS BY OVERLOADING + 49
02-08-19 OPERATOR USING FRIEND FUNCTION
AIM:
To write a C++ program for Concatenation of two strings by overloading + Operator using
friend function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class concat
{
char m[30];
public:
concat(char v[30])
{
strcpy(m,v);
}
friend void operator+(concat,concat);
};
void operator+(concat x,concat y)
{
strcat(x.m,y.m);
cout<<endl<<"CONCATENATION OF TWO STRINGS is:"<<x.m;
}
49
void main()
{
char a[30],b[30];
clrscr();
cout<<endl<<"***************************";
cout<<endl<<"concatenation of twostrings";
cout<<endl<<"***************************";
cout<<endl<<"Enter two strings:";
cin>>a>>b;
concat cat1(a);
concat cat2(b);
cat1+cat2;
getch();
}
50
OUTPUT:
RESULT:
Thus the above C++ program Concatenation of two strings by overloading + Operator using
friend function was executed and the output was verified successfully.
51
Ex. No.: 14
BIGGEST AMONG TWO NUMBERS BY OVERLOADING > 52
02-08-19 OPERATOR USING FRIEND FUNCTION
AIM:
To write a C++ program for Biggest among two numbers by overloading > Operator using
friend function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class biggest
{
int m;
public:
biggest(int v)
{
m=v;
}
friend void operator>(biggest,biggest);
};
void operator>(biggest x,biggest y)
{
if(x.m>y.m)
cout<<endl<<x.m<<" Is Biggest";
else
52
cout<<endl<<y.m<<" Is Biggest";
}
void main()
{
int a,b;
clrscr();
cout<<endl<<"*************************";
cout<<endl<<"BIGGEST AMONG TWO NUMBERS";
cout<<endl<<"*************************";
cout<<endl<<"Enter Any Two Number:";
cin>>a>>b;
biggest big1(a);
biggest big2(b);
big1>big2;
getch();
}
53
OUTPUT:
RESULT:
Thus the above C++ program Biggest among two numbers by overloading > Operator using
friend function was executed and the output was verified successfully.
54
Ex. No.: 15
EB BILL CALCULATION USING SINGLE INHERITANCE 55
09-08-19
AIM:
To write a C++ program for EB Bill Calculation using Single Inheritance.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class customer
{
protected:
int cno,lmr,tmr;
char cname[50];
public:
void input()
{
cout<<endl<<"Enter Customer Number:";
cin>>cno;
cout<<endl<<"Enter customer name:";
cin>>cname;
cout<<endl<<"Enter Last Reading:";
55
cin>>lmr;
cout<<endl<<"Enter month Reading:";
cin>>tmr;
}
};
class ebbill:public customer
{
private:
int uc;
float bill;
public:
void calc()
{
if(tmr>lmr)
{
uc=tmr-lmr;
if(uc<=100)
{
bill=0;
}
else if(uc>100 && uc<=200)
{
bill=(uc-100)*1.5+100*0;
bill=bill+10;
}
else if(uc>200 && uc<500)
{
56
bill=(uc-200)*3+100*2+100*0;
bill=bill+15;
}
else
{
bill=(uc-500)*6.6+300*4.6+100*3.5+100*0;
bill=bill+50;
}
}
else
{
cout<<endl<<"Please Enter Correct Reading.\n tmr should be greater than lmr.";
getch();
exit(0);
}
}
void output()
{
cout<<endl<<"CNO\tCNAME\tLMR\tTMR\tUC\t BILL\t\n";
cout<<endl<<cno<<"\t"<<cname<<"\t"<<lmr<<"\t"<<tmr<<"\t"<<uc<<"\t"<<bill;
}
};
void main()
{
clrscr();
cout<<endl<<"********************";
cout<<endl<<"EB BILL Calculation";
57
cout<<endl<<"********************";
ebbill eb;
eb.input();
eb.calc();
eb.output();
getch();
}
58
OUTPUT:
RESULT:
Thus the above C++ program EB Bill Calculation using Single Inheritance was executed and
the output was verified successfully.
59
Ex. No.: 16 BIGGEST AMONG TWO FRACTIONS USING
60
MULTIPLE INHERITANCE
17-08-19
AIM:
To write a C++ program for Biggest among two fractions using Multiple Inheritance.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class fraction1
{
protected:
int n1,d1;
public:
void input1()
{
cout<<endl<<"Enter The Numerator Value For First Fraction:";
cin>>n1;
cout<<endl<<"Enter The Denominator Value For First Fraction:";
cin>>d1;
}
void disp1()
{
cout<<endl<<"First Fraction is:"<<n1<<"/"<<d1;
}
};
60
class fraction2
{
protected:
int n2,d2;
public:
void input2()
{
cout<<endl<<"Enter The Numerator Value For Second Fraction:";
cin>>n2;
cout<<endl<<"Enter The Denominator Value For Second Fraction:";
cin>>d2;
}
void disp2()
{
cout<<endl<<"Second Fraction is:"<<n2<<"/"<<d2;
}
};
class biggest:public fraction1,public fraction2
{
private:
int d;
public:
void big()
{
if(d1==0||d2==0)
cout<<endl<<"Please Enter Non-Zero Denominator Value.";
else
61
{
d=d1*d2;
if((d/d1)*n1>=(d/d2)*n2)
cout<<endl<<"First Fraction Is Biggest:"<<n1<<"/"<<d1;
else
cout<<endl<<"Second Fraction Is Biggest:"<<n2<<"/"<<d2;
}
}
};
void main()
{
clrscr();
cout<<endl<<"******************************************************";
cout<<endl<<"Biggest Among Two Fraction Using Multiple Inheritance";
cout<<endl<<"*******************************************************";
biggest b;
b.input1();
b.input2();
b.disp1();
b.disp2();
b.big();
getch();
}
62
OUTPUT:
RESULT:
Thus the above C++ program Biggest among two fractions using Multiple Inheritance was
executed and the output was verified successfully.
63
Ex. No.: 17
PAY BILL CALCULATION USING MULTILEVEL INHERITANCE 64
20-08-19
AIM:
To write a C++ program for Pay Bill Calculation using Multilevel Inheritance.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class employee
{
protected:
float eno,bp,lic;
char ename[30];
public:
void get()
{
cout<<endl<<"Enter Employee Number:";
cin>>eno;
cout<<endl<<"Enter Employee Name:";
cin>>ename;
cout<<endl<<"Enter Basic Pay Amount:";
cin>>bp;
cout<<endl<<"Enter LIC Amount:";
cin>>lic;
}
};
64
class gross:public employee
{
protected:
float da,hra,gp;
public:
void calc_gp()
{
da=bp*10/100;
hra=bp*4/100;
gp=bp+da+hra;
}
};
class net:public gross
{
private:
float pf,np;
public:
void calc_np()
{
pf=bp*12/100;
np=gp-(pf+lic);
}
void put()
{
cout<<endl<<endl<<"EMPLOYEE NUMBER is:"<<eno;
cout<<endl<<"EMPLOYEE NAME is:"<<ename;
cout<<endl<<"BASIC PAYis:"<<bp;
65
cout<<endl<<"DEARNESS ALLOWANCE is:"<<da;
cout<<endl<<"HOUSE RENT ALLOWANCE is:"<<hra;
cout<<endl<<endl<<"GROSS PAYis:"<<gp;
cout<<endl<<"PROVIDAL FUND is:"<<pf;
cout<<endl<<"LIC AMOUNT is:"<<lic;
cout<<endl<<endl<<"NET PAY is:"<<np;
}
};
void main()
{
clrscr();
cout<<endl<<"************************************";
cout<<endl<<"PAYBILL USING MULTILEVEL INHERITANCE";
cout<<endl<<"************************************";
net n;
n.get();
n.calc_gp();
n.calc_np();
n.put();
getch();
}
66
OUTPUT:
RESULT:
Thus the above C++ program Pay Bill Calculation using Multilevel Inheritance was executed
and the output was verified successfully.
67
Ex. No.: 18
MATH FUNCTIONS USING HIERARCHICAL INHERITANCE 68
20-08-19
AIM:
To write a C++ program for Math Functions using Hierarchical Inheritance.
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class number
{
protected:
int k;
public:
void get()
{
cout<<endl<<endl<<"Enter Any Number:";
cin>>k;
}
};
class absolute:public number
{
public:
void calc_abs()
{
68
cout<<endl<<"ABSOLUTE VALUE OF"<<k<<" is:"<<abs(k);
}
};
class sqroot:public number
{
public:
void calc_sq()
{
cout<<endl<<":SQUARE ROOT VALUE OF"<<k<<" is:"<<sqrt(k);
}
};
class cosine:public number
{
public:
void calc_co()
{
cout<<endl<<"COS VALUE OF"<<k<<" is:"<<cos(k);
}
};
void main()
{
clrscr();
cout<<endl<<"*********************************************";
cout<<endl<<"Math Functions Using Hierarchical Inheritence";
cout<<endl<<"*********************************************";
absolute ab;
ab.get();
69
ab.calc_abs();
sqroot sq;
sq.get();
sq.calc_sq();
cosine co;
co.get();
co.calc_co();
getch();
}
70
OUTPUT:
RESULT:
Thus the above C++ Math Functions using Hierarchical Inheritance was executed and the
output was verified successfully.
71
Ex. No.: 19
CONVERSION USING VIRTUAL FUNCTION 72
27-08-19
AIM:
To write a C++ program for Conversion using Virtual Function.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class conversion
{
int c;
public:
virtual void convert()
{
cout<<endl<<"Enter The Celcius Value:";
cin>>c;
float f=(c*1.8)+32;
cout<<endl<<"Farenhient value is:"<<f;
}
};
class km:public conversion
{
int k1;
public:
72
void convert()
{
cout<<endl<<endl<<"Enter The Number Of KiloMeter:";
cin>>k1;
int meter=k1*1000;
cout<<endl<<k1<<"KiloMeter="<<meter<<"meters";
}
};
class sec:public conversion
{
int hr;
public:
void convert()
{
cout<<endl<<endl<<"Enter The Number Hours:";
cin>>hr;
int s=hr*60*60;
cout<<endl<<hr<<"hour="<<s<<"seconds";
}
};
void main()
{
clrscr();
cout<<endl<<"**********************************";
cout<<endl<<"conversion using virtual function";
cout<<endl<<"**********************************";
conversion co,*ptr;
73
km k;
sec se;
ptr=&co;
ptr->convert();
ptr=&k;
ptr->convert();
ptr=&se;
ptr->convert();
getch();
}
74
OUTPUT:
RESULT:
Thus the above C++ program Conversion using Virtual Function was executed and verified
successfully created.
75
Ex. No.: 20
‘THIS’ POINTER 76
29-08-19
AIM:
To write a C++ program for ‘this’ pointer.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class student
{
char name[100];
int age,roll;
float percent;
public:
void getdata()
{
cout<<endl<<"Enter Data";
cout<<"Name:";
cin>>name;
cout<<"Age:";
cin>>age;
cout<<"Roll:";
cin>>roll;
cout<<"Percent:";
cin>>percent;
76
cout<<endl;
}
void display()
{
cout<<"Name:"<<name<<endl;
cout<<"Age:"<<age<<endl;
cout<<"Roll:"<<roll<<endl;
cout<<"percent:"<<percent;
}
student &max(student & s1,student & s2);
};
student &student::max(student & s1,student & s2)
{
if(s1.percent>percent && s1.percent>s2.percent)
return s1;
else if(s2.percent>percent && s2.percent>s1.percent)
return s2;
else
return*this;
}
int main()
{
clrscr();
cout<<endl<<"************";
cout<<endl<<"this pointer";
cout<<endl<<"************";
student s,s1,s2,s3;
77
s1.getdata();
s2.getdata();
s3.getdata();
s=s3.max(s1,s2);
cout<<"Student With Highest Percentage"<<endl;
s.display();
getch();
return 0;
}
78
OUTPUT
RESULT:
Thus the above C++ program ‘this’ pointer was executed and verified successfully
completed.
79
DATA
STRUCTURES
PROGRAMS
80
Ex. No.: 21
LINEAR SEARCH USING CLASS AND OBJECT 81
05-09-19
AIM:
To write a Data Structure program for Linear Search using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class linear
{
int k[100],i,s,n,f;
public:
void get();
void find();
};
void linear::get()
{
cout<<endl<<"Enter How Many Elements:";
cin>>n;
cout<<endl<<"Enter The Elements:";
for(i=0;i<n;i++)
cin>>k[i];
}
81
void linear::find()
{
f=0;
cout<<endl<<"\n Enter The Search Element:";
cin>>s;
for(i=0;i<n;i++)
{
if(s==k[i])
{
f=1;
break;
}
}
if(f==1)
cout<<endl<<"Given Element is present";
else
cout<<endl<<"Given Element is not present";
}
void main()
{
clrscr();
cout<<endl<<"************************************";
cout<<endl<<"Linear Search Using Class And Object";
cout<<endl<<"************************************";
linear lr;
lr.get();
82
lr.find();
lr.find();
getch();
}
OUTPUT:
RESULT:
Thus the above Data Structure program Linear Search using Class and Object was executed
and verified successfully completed.
83
Ex. No.: 22
BINARY SEARCH USING CLASS AND OBJECT 84
05-09-19
AIM:
To write a Data Structure program for Binary Search using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class binary
{
int k[100],i,j,t,s,n;
int beg,mid,end;
void sort();
public:
void get();
void find();
};
void binary::get()
{
cout<<endl<<"Enter How Many Elements:";
cin>>n;
cout<<endl<<"Enter The Elements:";
for(i=1;i<=n;i++)
cin>>k[i];
84
sort();
}
void binary::sort()
{
for(i=1;i<=n;i++)
{
for(j=i+1;j<=n;j++)
{
if(k[i]>k[j])
{
t=k[i];
k[i]=k[j];
k[j]=t;
}
}
}
}
void binary::find()
{
cout<<endl<<"After Sorting:";
for(i=1;i<=n;i++)
cout<<endl<<k[i];
cout<<endl<<"Enter The Search Element:";
cin>>s;
beg=1;
end=n;
mid=int((beg+end)/2);
85
while(beg<=end&&k[mid]!=s)
{
if(s<k[mid])
end=mid-1;
else
beg=mid+1;
mid=int((beg+end)/2);
}
if(k[mid]==s)
cout<<endl<<"Given Element is Found:"<<s;
else
cout<<endl<<"Given Element is not found:"<<s;
}
void main()
{
clrscr();
cout<<endl<<"************************************";
cout<<endl<<"BINARY SEARCH USING CLASS AND OBJECT";
cout<<endl<<"************************************";
int ch;
binary by;
by.get();
do
{
clrscr();
by.find();
cout<<endl<<"If You Want To Continue(1/0):";
86
cin>>ch;
}
while(ch!=0);
cout<<endl<<"Exit The Program";
getch();
}
87
OUTPUT:
RESULT:
Thus the above Data Structure program Binary Search using Class and Object was executed
and the output was verified successfully.
88
Ex. No.: 23
BUBBLE SORT USING CLASS AND OBJECT 89
13-09-19
AIM:
To write a Data Structure program for Bubble Sort using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class bubble
{
int a[50],n,i,j,t;
public:
void input();
void sort();
void output();
};
void bubble::input()
{
cout<<endl<<"Enter How Many Elements:";
cin>>n;
cout<<endl<<"Enter the Elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
89
cout<<endl<<"Before Bubble Sorting:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
void bubble::sort()
{
for(i=0;i<(n-1);i++)
{
for(j=0;j<(n-i-1);j++)
{
if(a[j]>a[j+1])
{
t=a[i];
a[j]=a[j+1];
a[j+1]=t;
}
}
}
}
void bubble::output()
{
cout<<endl<<"After Bubble Sorting:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
void main()
{
90
clrscr();
bubble bub;
bub.input();
bub.sort();
bub.output();
getch();
}
OUTPUT:
RESULT:
Thus the above Data Structure program Bubble Sort using Class and Object was executed
and the output was verified successfully.
91
Ex. No.: 24
SELECTION SORT USING CLASS AND OBJECT 92
19-09-19
AIM:
To write a Data Structure program for Selection Sort using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class selection
{
private:
int a[50],n,i,j,t,p;
public:
void getdata();
void sort();
void putdata();
};
void selection::getdata()
{
cout<<endl<<"Enter How Many Elements:";
cin>>n;
cout<<endl<<"Enetr The elements:\n";
for(i=0;i<n;i++)
cin>>a[i];
92
cout<<endl<<"Before Selection Sorting:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
void selection::sort()
{
for(i=0;i<(n-1);i++)
{
p=i;
for(j=i+1;j<n;j++)
{
if(a[p]>a[j])
p=j;
}
{
t=a[i];
a[i]=a[p];
a[p]=t;
}
}
}
void selection::putdata()
{
cout<<endl<<"After Selection Sorting:\n";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
93
void main()
{
clrscr();
cout<<endl<<"******************************************";
cout<<endl<<"selection sorting using class and objects";
cout<<endl<<"*******************************************";
selection sel;
sel.getdata();
sel.sort();
sel.putdata();
getch();
}
94
OUTPUT
RESULT
Thus the above Data Structure program Selection Sort using Class and Object was executed
and the output was verified successfully.
95
Ex. No.: 25
INSERTION SORT USING CLASS AND OBJECT 96
21-09-19
AIM:
To write a Data Structure program for Insertion Sort using Class and Object.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class insertion
{
int a[50],t,j,n,i;
public:
void getdata();
void sort();
void putdata();
};
void insertion::getdata()
{
cout<<endl<<"Enter How Many Elements:";
cin>>n;
cout<<endl<<"Enter The Elements;\n";
for(i=0;i<n;i++)
cin>>a[i];
96
cout<<endl<<"Before Insertion sorting:";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
void insertion::sort()
{
for(i=1;i<n;i++)
{
t=a[i];
j=i-1;
while((t<a[j])&&(j>=0))
{
a[j+1]=a[j];
j--;
}
a[j+1]=t;
}
}
void insertion::putdata()
{
cout<<endl<<"After Insertion Soring:";
for(i=0;i<n;i++)
cout<<"\t"<<a[i];
}
97
void main()
{
clrscr();
cout<<endl<<"**************************************";
cout<<endl<<"Insertion Sort Using Class And Objects";
cout<<endl<<"**************************************";
insertion ins;
ins.getdata();
ins.sort();
ins.putdata();
getch();
}
98
OUTPUT
RESULT
Thus the above Data Structure program Insertion Sort using Class and Object was executed
and the output was verified successfully.
99
Ex. No.: 26 STACK OPERATIONS
100
21-09-19
AIM:
To write a Data Structure program for Stack Operations.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stack
{
private:
int a[100],i,j,n,v,t;
public:
void create();
void push();
void pop();
void disp();
void full();
void empty();
void top();
};
100
void stack::create()
{
cout<<endl<<"Enter The Size Of Stack:";
cin>>n;
t=0;
cout<<endl<<"Stack Is Created";
getch();
}
void stack::push()
{
if(t>=n)
cout<<endl<<"Stack Is Full";
else
{
cout<<endl<<"Enter The Elements For Insert Into Stack:";
cin>>v;
t++;
a[t]=v;
cout<<endl<<"Inserted.....";
disp();
}
}
101
void stack::pop()
{
if(t==0)
cout<<endl<<"Stack Is Empty";
else
{
j=a[t];
t--;
cout<<endl<<"Delete Elewment Is:"<<j;
}
}
void stack::disp()
{
if(t==0)
cout<<endl<<"Stack Is Emotuy";
else
{
cout<<endl<<"Element In Stack:";
for(i=t;i>=1;i--)
cout<<endl<<a[i];
}
}
102
void stack::full()
{
if(t>=n)
cout<<endl<<"Stack Is full";
else
cout<<endl<<"Stack Is Not Full";
}
void stack::empty()
{
if(t==0)
cout<<endl<<"Stack Is Empty";
else
cout<<endl<<"Stack Is Not Empty";
}
void stack::top()
{
if(t==0)
cout<<endl<<"Stack Is Empty";
else
{
cout<<endl<<"Top Position Is:"<<t<<endl<<"Top Elements Is:"<<a[t];
}
}
103
void main()
{
int ch;
clrscr();
cout<<endl<<"****************";
cout<<endl<<"Stack Operation";
cout<<endl<<"***************";
stack sk;
sk.create();
do
{
clrscr();
cout<<endl<<"MENUS"<<endl<<"1-Push"<<endl<<"2-Pop"<<endl<<"3-Display"<<endl<<"4-Full
or Not"<<endl<<"5-Empty or not"<<endl<<"6-Top"<<endl<<"7-exit";
cout<<endl<<"Enter YOur Choice:";
cin>>ch;
switch(ch)
{
case 1:
sk.push();
break;
case 2:
sk.pop();
break;
case 3:
sk.disp();
break;
104
case 4:
sk.full();
break;
case 5:
sk.empty();
break;
case 6:
sk.top();
break;
case 7:
cout<<endl<<"Exit The Program";
break;
default:
cout<<endl<<"Invalid Choice";
break;
}
getch();
}
while(ch!=7);
getch();
}
105
OUTPUT
106
RESULT
Thus the above Data Structure program Stack Operations was executed and the output
was verified successfully.
107
Ex. No.: 27
QUEUE OPERATIONS 108
26-09-19
AIM:
To write a Data Structure program for Queue Operations.
PROGRAM:
#include<iostream.h>
#include<conio.h>
class queue
{
private:
int n,a[100],i,j,v,front,rear;
public:
void create();
void insert();
void del();
void disp();
void full();
void empty();
void showfr();
};
108
void queue::create()
{
cout<<endl<<"Enter The Size Of Queue:";
cin>>n;
front=rear=0;
cout<<endl<<"Queueis created...";
getch();
}
void queue::insert()
{
if(rear>=n)
cout<<endl<<"Queue is Full";
else
{
cout<<endl<<"Enter The Element For Insert Into Queue:";
cin>>v;
rear++;
a[rear]=v;
cout<<endl<<"Inserted..";
disp();
}
}
109
void queue::del()
{
if(front==0 && rear==0)
cout<<endl<<"Queue is Empty";
else
{
j=a[front+1];
cout<<endl<<"Deleted Element is:"<<j;
front++;
for(i=1;i<=rear;i++)
a[i]=a[i+1];
front=0;
rear--;
}
}
void queue::disp()
{
if(front==0 && rear==0)
cout<<endl<<"Queue is empty";
else
{
cout<<endl<<"Element in Queue";
for(i=1;i<=rear;i++)
cout<<endl<<a[i];
}
}
110
void queue::full()
{
if(rear>=n)
cout<<endl<<"Queue is full";
else
cout<<endl<<"Queue is Not Full";
}
void queue::empty()
{
if(front==0 && rear==0)
cout<<endl<<"Queue is empty";
else
cout<<endl<<"Queue is not empty";
}
void queue::showfr()
{
if(front==0 && rear==0)
cout<<endl<<"Queue is empty";
else
{
cout<<endl<<"Front Element is:"<<a[front+1];
cout<<endl<<"Rear Element is:"<<a[rear];
}
}
111
void main()
{
int ch;
clrscr();
cout<<endl<<"****************";
cout<<endl<<"Queue Operation";
cout<<endl<<"****************";
queue q;
q.create();
do
{
clrscr();
cout<<endl<<"MENUS"<<endl<<"1-INSERT"<<endl<<"2-DELETE"<<endl<<"3-
DISPLAY"<<endl<<"4-FULL OR NOT FULL"<<endl<<"5-EMPTY OR NOT "<<endl<<"6-
SHOWFRONTREAR"<<endl<<"7-EXIT";
cout<<endl<<"Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
q.insert();
break;
case 2:
q.del();
break;
case 3:
q.disp();
112
break;
case 4:
q.full();
break;
case 5:
q.empty();
break;
case 6:
q.showfr();
break;
case 7:
cout<<endl<<"Exit The Program";
break;
default:
cout<<endl<<"Invalid Choice";
break;
}
getch();
}
while(ch!=7);
getch();
}
113
OUTPUT
114
RESULT
Thus the above Data Structure program Queue Operations was executed and the output
was verified successfully.
115
Ex. No.: 28
BINARY TREE TRAVERSAL 116
30-09-19
AIM
To write a Data Structure program for Binary Tree Traversal.
PROGRAM
#include<iostream.h>
#include<conio.h>
class node
{
public:
int data;
node *left;
node *right;
node *insert(node *tree,int data);
void preorder(node *tree);
void inorder(node *tree);
void postorder(node *tree);
}*tree=NULL;
116
node *node::insert(node *tree,int data)
{
if(tree==NULL)
{
tree=new node();
tree->left=tree->right=NULL;
tree->data=data;
}
else if(data<tree->data)
{
if(tree->left==NULL)
tree->left=insert(tree->left,data);
else
insert(tree->left,data);
}
else if(data>tree->data)
{
if(tree->right==NULL)
tree->right=insert(tree->right,data);
else
insert(tree->right,data);
}
return(tree);
}
117
void node::preorder(node *tree)
{
if(tree!=NULL)
{
cout<<tree->data<<"\t";
preorder(tree->left);
preorder(tree->right);
}
}
void node::inorder(node *tree)
{
if(tree!=NULL)
{
inorder(tree->left);
cout<<tree->data<<"\t";
inorder(tree->right);
}
}
void node::postorder(node *tree)
{
if(tree!=NULL)
{
postorder(tree->left);
postorder(tree->right);
cout<<tree->data<<"\t";
}
}
118
void main()
{
node n;
int ch,data;
do
{
clrscr();
cout<<endl<<"*********************";
cout<<endl<<"Binary Tree Traversal";
cout<<endl<<"*********************";
cout<<endl<<"1-insert"<<endl<<"2-preorder"<<endl<<"3-inorder"<<endl<<"4-
postorder"<<endl<<"5-exit";
cout<<endl<<"Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1:
cout<<endl<<"Enter The Elment:";
cin>>data;
tree=n.insert(tree,data);
cout<<endl<<"Inserted.....";
break;
case 2:
cout<<endl<<"Preorder Traversal is(root,left,right):"<<endl;
n.preorder(tree);
break;
case 3:
119
cout<<endl<<"Inorder traversal is(left,root,right):"<<endl;
n.inorder(tree);
break;
case 4:
cout<<endl<<"Postorder Traversal is(left,right,root):"<<endl;
n.postorder(tree);
break;
case 5:
cout<<endl<<"Exit The Program";
break;
default:
cout<<endl<<"Invalid Choice";
break;
}
getch();
}
while(ch!=5);
getch();
}
120
OUTPUT
121
RESULT
Thus the above Data Structure Program Binary Tree Traversal was executed and the output
was verified successfully.
122