C++ Final
C++ Final
Core Lab 2:
2023 - 2024
NAME : ____________________________
CLASS : ____________________________
SRI VASAVI COLLEGE
[Reaccredited with ‘B’ Grade by NAAC]
ERODE – 638 316
DEPARTMENT OF COMPUTER SCIENCE
______________________Register Number of
I – B.Sc Computer Science of Sri Vasavi College during EVEN Semester of the
Date:
Place: Erode.
PAGE
S.NO DATE PARTICULARS
NO
1 Stack Operation
2 Arithmetic Operator
3 Sum of Digits
4 Operator Overloading
5 Concatenation of String
6 Employee Details
7 Shape
8 Friend Function
9 Matrix Addition
10 Palindrome
12 File Merging
Ex. No 1
Date STACK OPERATION
AIM :
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
class stack
{
int top;
int s[5];
public:
stack(){top=-1;}
~stack(){}
void push(int x);
void pop();
void show();
};
void stack::push(int x)
{
if(top==5)
cout<<"Stack if FULL \n";
else
s [++top]=x;
cout<<x<<"is Pushed \n";
}
void stack::pop()
{
if(top==-1)
{
cout<<"Stack is EMPTY \n";
}
else
{
cout<<s[top--]<<"is Poped \n";
}
}
void stack::show()
{
if(top==1)
cout<<"Stack is EMPTY \n";
else
for(int i=top;i>=0;i--)
cout<<s[i]<<"\n";
}
void main()
{
int num,cho;
stack st;
do
{
clrscr();
cout<<"\n\t\t STACK OPERATIONS \n\n";
cout<<"1.Push\n";
cout<<"2.Pop\n";
cout<<"3.Show\n";
cout<<"4.Exit\n";
cout<<"\nEnter your Choice : ";
cin>>cho;
switch(cho)
{
case 1:
cout<<"\nEnter a value to Push :";
cin>>num;
st.push(num);
getch();
break;
case 2:
st.pop();
getch();
break;
case 3:
st.show();
getch();
break;
case 4:
cout<<"End of Stack Operations \n";
getch();
return;
default :
cout<<"Invalid Operation \n ";
}
}
while (cho>=1&&cho<=4);
getch();
}
OUTPUT:
1.Push
2.Pop
3.Show
4.Exit
Enter your Choice : 1
Enter a value to Push : 40
40is Pushed
Enter your Choice : 1
Enter a value to Push : 50
50is Pushed
Enter your Choice : 1
Enter a value to Push : 60
60is Pushed
Enter your Choice : 2
60is POPED
Enter your Choice : 2
50is POPED
Enter your Choice : 3
40
Enter your Choice : 4
End of Stack Operations
Enter your Choice : 5
Invalid Operation
RESULT:
Ex. No 2
Date ARITHMETIC OPERATION
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h> Class
arith
{
int i;
float f;
public:
void set()
{
cout<<"\t\t ARITHMETIC OPERATIONS ";
cout<<"\n\nEnter the INTEGER and FLOAT no :";cin>>i>>f;
}
void show()
{
cout<<" \nThe INTEGER No : "<<i<<"\n";cout<<"
\nThe FLOAT No : "<<f<<"\n";
}
void add()
{
cout<<" \nAddition is : "<<float(i+f)<<"\n";
}
void sub()
{
cout<<" \nSubtraction is : "<<float(i-f)<<"\n";
}
void mul()
{
cout<<" \nMultiplication is :
"<<float(i*f)<<"\n";
}
void div()
{
cout<<" \nDivision is : "<<float(i/f)<<"\n";
}
};
void main()
{
clrscr();
arith a;
a.set();
a.show();
a.add();
a.sub();
a.mul();
a.div();
getch();
}
OUTPUT:
RESULT:
Ex. No 3
Date SUM OF DIGITS
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
class sum
{
int num,sum;
public:
sum(int x)
{
num=x;
}
int sumofdigits();
};
int sum::sumofdigits()
{
int r,sum=0;
do
{
while(num!=0)
{
r=num%10;
sum+=r;
num/=10;
}
num=sum;
sum=0;
}
while(num>9);
return num;
}
void main()
{
int x,y;
clrscr();
cout<<”\n\t\t Sum of Digits”;
cout<<”\n Enter the number : ";
cin>>y;
sum s(y);
x=s.sumofdigits();
cout<<"Sum of Digits = "<<x<<"\n";
getch();
}
OUTPUT:
RESULT:
Ex. No 4
Date OPERATOR OVERLOADING
AIM:
ALGORITHM
PROGRAM:
#include<iostream.h>
#include<conio.h>
class FLOAT
{
float x;
public:
FLOAT(){}
FLOAT (float m){x=m;}void
show(char*s)
{
cout<<s<<x<<"\n";
}
friend FLOAT operator+(FLOAT F1,FLOAT F2) friend FLOAT
operator-(FLOAT F1,FLOAT F2) friend FLOAT
operator*(FLOAT F1,FLOAT F2) friend FLOAT
operator/(FLOAT F1,FLOAT F2)
};
FLOAT operator+(FLOAT F1,FLOAT F2)
{
FLOAT F3;
F3.x=F1.x+F2.x;
return F3;
}
FLOAT operator-(FLOAT F1,FLOAT F2)
{
FLOAT F3; F3.x=F1.x-
F2.x;
return F3;
}
FLOAT operator*(FLOAT F1,FLOAT F2)
{
FLOAT F3;
F3.x=F1.x*F2.x;
return F3;
}
FLOAT operator/(FLOAT F1,FLOAT F2)
{
FLOAT F3;
F3.x=F1.x/F2.x;
return F3;
}
void main()
{
clrscr();
FLOAT p(2.2),q(2.2),r;
Cout<<”\n\t\t Operator Overloading\n”;
p.show("P=");
q.show("Q="); r=p+q;
r.show("P+Q=");r=p-
q; r.show("P-Q=");
r=p*q;
r.show("P*Q=");
r=p/q;
r.show("P/Q=");
getch();
}
OUTPUT:
P =2.2
Q =2.2
P+Q=4.4
P-Q=0
P*Q=4.84
P/Q=1
RESULT:
Ex. No 5
Date CONCATENATION OF STRING
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
class string
{
char *s;
public:
string();
string(char *a);
string operator+(string a);
int operator==(string a);
void show(char *p);
};
string::string(){}
string::string(char *a)
{
s=new char [strlen(a)+1];
strcpy(s,a);
}
string string::operator+(string a)
{
string s3;
s3.s=new char[strlen(s)+strlen(a.s)+1];
strcpy(s3.s,s);
strcat(s3.s,a.s);
return s3;
}
int string::operator==(string a)
{
if(strcmp(s,a.s)==0)
return 1;
else
return 0;
}
void string::show(char *p)
{
cout<<p<<s<<'\n';
}
void main()
{
clrscr();
cout<<”\n\t\t Concatenation of String\n”;
string s1("Bharathiyar");
string s2("University");
string s3("Bharathiyar");
string s4;
s1.show("S1= ");
s2.show("S2= ");
s3.show("S3= ");
s4=s1+s2;
s4.show("S1+S2= ");
if(s1==s3)
{
cout<<"S1 and S3 are EQUAL\n";
}
else
cout<<"S1 and S3 are NOT EQUAL\n";
if(s1==s2)
cout<<"S1 and S2 are EQUAL\n";
else
cout<<"S1 and S2 are NOT EQUAL\n";
getch();
}
OUTPUT:
S1= Bharathiyar
S2= University
S3= Bharathiyar
S1+S2= BharathiyarUniversity
S1 and S3 are EQUAL
S1 and S2 are NOT EQUAL
RESULT:
Ex. No 6
Date EMPLOYEE DETAILS
AIM :
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
class employee
{
protected:
int eno;
char ename[20];
char edept[20];
char eg;
int ebs;
public:
void get();
};
class pay:public employee
{
float da,hra,pf;
public:
void display();
void calculate();
};
void employee::get()
{
cout<<"Enter Employee Number : ";
cin>>eno;
cout<<"Enter Employee Name : ";
cin>>ename;
cout<<"Enter Employee Department : ";
cin>>edept;
cout<<"Enter Employee Grade : ";
cin>>eg;
cout<<"Enter Employee Basic Salary : ";
cin>>ebs;
}
void pay ::calculate()
{
if(eg=='a')
{
da=ebs*0.8;
hra=ebs*0.20;
pf=700;
}
if(eg=='b')
{
da=ebs*0.6;
hra=ebs*0.18;
pf=600;
}
if(eg=='c')
{
da=ebs*0.4;
hra=ebs*0.16;
pf=500;
}
}
void pay::display()
{
float g,n;
cout<<"The Employee Number : "<<eno<<'\n';
cout<<"The Employee Name : "<<ename<<'\n';
cout<<"The Employee Department : "<<edept<<'\n';
cout<<"The Employee Grade : "<<eg<<'\n';
cout<<"The Employee Salary : "<<ebs<<'\n';
g=ebs+da+hra;
cout<<"Gross = "<<g<<'\n';
n=g-pf;
cout<<"Net = "<<n<<'\n';
}
void main()
{
clrscr();
cout<<”\n\t\t Employee Details\n”;
pay s;
s.get();
s.calculate();
s.display();
getch();
}
OUTPUT:
RESULT:
Ex. No. 7
Date SHAPE
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<math.h>
class shape
{
protected:
int d1,d2,d3;
public:
virtual void area()=0;
virtual void peri()=0;
void set(int x,int y=0,int z=0)
{
d1=x;d2=y;d3=z;
}
};
class circle:public shape
{
public:
void set(int x){d1=x;}
void area()
{
cout<<"Area of Circle = "<<5*3.14*d1<<"\n";
}
void peri()
{
cout<<"Peri of Circle = "<<2*3.14*d1<<"\n";
}
};
class rectangle:public shape
{
public:
void set(int x,int y)
{
d1=x;d2=y;
}
void area()
{
cout<<"Area of rectangle = "<<d1*d2<<"\n";
}
void peri()
{
cout<<"Peri of Rectangle = "<<2*(d1+d2)<<"\n";
}
};
class triangle:public shape
{
public:
void get(int x,int y,int z){d1=x,d2=y,d3=z;}
void area()
{
float s;
s=d1+d2+d3/2;
cout<<"Area of triangle = "<<sqrt(s*(s-d1)*(s-
d2)*(s-d3))<<"\n";
}
void peri()
{
cout<<"Peri of triangle = "<<d1+d2+d3<<"\n\n";
}
};
void main()
{
clrscr();
cout<<”\n\t\t Shapes\n”;
shape *s;
circle cob;
rectangle rob;
triangle tob;
s=&cob;
s->set(5);
s->area();
s->peri();
s=&rob;
s->set(10,20);
s->area();
s->peri();
s=&tob;
s->set(10,20,30);
s->area();
s->peri();
getch();
}
OUTPUT:
RESULT:
Ex. No 8
Date FRIEND FUNCTION
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h> class
second;
class first
{
int v1;
float v2;
public:
void get()
{
cout<<"\nEnter the INTEGER and FLOAT Number : ";
cin>>v1>>v2;
}
friend void sum(first,second)
};
class second
{
int v3;
float v4;
public:
void get()
{
cout<<"\nEnter the INTEGER and FLOAT Number : ";
cin>>v3>>v4;
}
friend void sum(first,second)
};
void sum(first a,second b)
{
cout<<"\n\n The Sum of INTEGER Number is : "<<a.v1+b.v3;
cout<<"\n\n The Sum of FLOAT Number is : "<<a.v2+b.v4;
}
void main()
{
clrscr();
cout<<"\n\t\t\t***FRIEND FUNCTION ***";
first p;
second m;
p.get();
m.get();
sum(p,m);
getch();
}
OUTPUT:
RESULT:
Ex. No 9
Date MATRIX ADDITION
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<iomanip.h>
class matrix
{
int a[10][10],r,c;
public:
matrix();
void set();
matrix operator+(matrix m2);
void show();
};
matrix::matrix()
{
r=c=3;
}
void matrix::set()
{
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
cin>>a[i][j];
}
matrix matrix::operator+(matrix m2)
{
matrix m3;
for(int i=0;i<r;i++)
for(int j=0;j<c;j++)
m3.a[i][j]=a[i][j]+m2.a[i][j];
return m3;
}
void matrix::show()
{
for(int i=0;i<r;i++)
{
for(int j=0;j<c;j++)
cout<<setw(j)<<a[i][j]<<”\t”;
cout<<"\n";
}
}
void main()
{
clrscr();
cout<<”\n\t\t Matrix Addition\n”;
matrix m1,m2,m3;
cout<<"Enter the 1st Matrix : \n\n";
m1.set();
cout<<"Enter the 2nd Matrix : \n\n";
m2.set();
m3=m1+m2;
cout<<"First Matrix : \n\n";
m1.show();
cout<<"Second Matrix : \n\n";
m2.show();
cout<<"Result : \n\n";
m3.show();
getch();
}
OUTPUT:
Enter the 1st Matrix :
1 2 3
1 2 3
1 2 3
Enter the 2nd Matrix :
1 2 3
1 2 3
1 2 3
First Matrix :
1 2 3
1 2 3
1 2 3
Second Matrix :
1 2 3
1 2 3
1 2 3
Result :
2 4 6
2 4 6
2 4 6
RESULT:
Ex. No 10
Date PALINDROME
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
char *preprocess(char *a)
{
[
RESULT:
Ex. No 11
Date READING FILE CONTENT
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
void main()
{
clrscr();
ifstreamin("prog11.cpp");
char line[80];
int lno=0;
while(!in.eof())
{
in.getline(line,80);
if(in)
cout<<++lno<<" "<<line<<'\n';
}
in.close();
getch();
}
OUTPUT:
1 #include<iostream.h>
2 #include<conio.h>
3 #include<fstream.h>
4 void main()
5 {
6 clrscr();
7 ifstream in("prog11.cpp");
8 char line[80];
9 int lno=0;
10 while(!in.eof())
11 {
12 in.getline(line,80);
13 if(in)
14 cout<<++lno<<" "<<line<<'\n';
15 }
16 in.close();
17 getch();
18 }
RESULT:
Ex. No 12
Date FILE MERGING
AIM:
ALGORITHM:
PROGRAM:
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
void main()
{
clrscr();
ifstream ifiles1, ifiles2;
ofstream ifilet;
char ch, fname1[20], fname2[20], fname3[30];
cout<<"Enter first file name (with extension like file1.txt) : ";
gets(fname1);
cout<<"Enter second file name (with extension like file2.txt) : ";
gets(fname2);
cout<<"Enter name of file (with extension like file3.txt) which will store the contents of the two files
(fname1 and fname1) : ";
gets(fname3);
ifiles1.open(fname1);
ifiles2.open(fname2);
if(ifiles1==NULL || ifiles2==NULL)
{
perror("Error Message ");
cout<<"Press any key to exit...\n";
getch();
exit(EXIT_FAILURE);
}
ifilet.open(fname3);
if(!ifilet)
{
perror("Error Message ");
cout<<"Press any key to exit...\n";
getch();
exit(EXIT_FAILURE);
}
while(ifiles1.eof()==0)
{
ifiles1>>ch;
ifilet<<ch;
}
while(ifiles2.eof()==0)
{
ifiles2>>ch;
ifilet<<ch;
}
cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
ifiles1.close();
ifiles2.close();
ifilet.close();
getch();
OUTPUT:
RESULT: