Professional Documents
Culture Documents
KOMARAPALAYAM - 637303.
PROGRAMMING IN C++
(17UCAP04 )
PRACTICAL RECORD
Name : _____________________________
Register No : _____________________________
PRACTICAL - IV
BONAFIDE CERTIFICATE
PROGRAMMING IN C++
10
Page No.
EX : NO 1
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
#include<iostream.h>
#include<process.h>
#include<conio.h>
class Arith
{
private:
int x,y;
float a,b;
public:
int add();
int sub();
int mul();
int div();
};
int Arith::add()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x+y);
}
int Arith::sub()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x-y);
}
int Arith::mul()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x*y);
}
int Arith::div()
{
cout<<"Enter the x value"<<endl;
cin>>x;
cout<<"Enter the y value"<<endl;
cin>>y;
return(x/y);
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
}
main()
{
Arith obj;
int ch;
clrscr();
while(1)
{
cout<<"1:Addition"<<endl;
cout<<"2:Subtraction"<<endl;
cout<<"3:Multiply"<<endl;
cout<<"4:Division"<<endl;
cout<<"5:exit"<<endl;
cout<<"Enter your Choice"<<endl;
cin>>ch;
switch(ch)
{
case 1:
int a=obj.add();
cout<<"Addition Result="<<a<<endl;
break;
case 2:
int s=obj.sub();
cout<<"Subtract Result="<<s<<endl;
break;
case 3:
int m=obj.mul();
cout<<"Multiply Result="<<m<<endl;
break;
case 4:
int d=obj.div();
cout<<"Division Result="<<d<<endl;
break;
case 5:
exit(0);
}
}
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 1
Enter the x value
10
Enter the y value
20
Addition Result : 20
1. Addition
2. Subtraction
3. Multiplication
4. Division
5. Exit
Enter Your Choice : 3
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO 2
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
class FLOAT
{
float no;
public:
FLOAT(){}
void getdata()
{
cout<<"\n ENTER AN FLOATING NUMBER :";
cin>>no;
}
void putdata()
{
cout<<"\n\nANSWER IS :"<<no;
}
FLOAT operator+(FLOAT);
FLOAT operator*(FLOAT);
FLOAT operator-(FLOAT);
FLOAT operator/(FLOAT);
};
FLOAT FLOAT::operator+(FLOAT a)
{
FLOAT temp;
temp.no=no+a.no;
return temp;
}
FLOAT FLOAT::operator*(FLOAT b)
{
FLOAT temp;
temp.no=no*b.no;
return temp;
}
FLOAT FLOAT::operator-(FLOAT b)
{
FLOAT temp;
temp.no=no-b.no;
return temp;
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
{
FLOAT temp;
temp.no=no/b.no;
return temp;
}
main()
{
clrscr();
FLOAT a,b,c;
a.getdata();
b.getdata();
c=a+b;
cout<<"\n\n Overloading Arithmetic Operators \n\n";
cout<<"************************************* \n\n";
cout<<"\n\n ADDITION OF TWO OBJECTS";
c.putdata();
cout<<"\n\n MULTIPLICATION OF TWO OBJECTS";
c=a*b;
c.putdata();
cout<<"\n\n SUBSTRACTION OF TWO OBJECTS";
c=a-b;
c.putdata();
cout<<"\n\n DIVISION OF TWO OBJECTS";
c=a/b;
c.putdata();
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 3
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
#include<iostream.h>
#include<conio.h>
#include<string.h>
class String
{
public:
char str[20];
public:
void getstr()
{
cout<<"\n Enter String : ";
cin>>str;
}
void displaystr()
{
cout<<str;
}
String operator+(String x) //Concatenating String
{
String s;
strcat(str,x.str);
strcpy(s.str,str);
return s;
}
int operator ==(String s)
{
if(!strcmp(str,s.str))
return 1;
return 0;
}
};
int main()
{
String str1, str2, str3,str4,str5;
str1.getstr();
str2.getstr();
cout<<"\n ----------------------------------------------";
cout<<"\n\n First String is : ";
str1.displaystr(); //Displaying First String
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
cout<<"\n ----------------------------------------------";
str3=str1+str2; //String is concatenated. Overloaded '+' operator
cout<<"\n\n Concatenated String is : ";
str3.displaystr();
cout<<"\n String Compare \n";
cout<<"\n ----------------------------------------------";
str4.getstr();
str5.getstr();
if(str4==str5)
{
cout<<"\nStrigs are Equal\n";
}
else
{
cout<<"\nStrings are Not Equal\n";
}
cout<<"\n ----------------------------------------------";
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 4
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
class emp
{
public:
int eno;
char name[20], des[20];
int basicpay;
void get()
{
cout << "Enter the employee number:";
cin>>eno;
cout << "Enter the employee name:";
cin>>name;
cout << "Enter the designation:";
cin>>des;
cout << "Enter the Basicpay:";
cin>>basicpay;
}
};
void calculate()
{
np = basicpay + hra + da - pf;
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
void display()
{
cout << eno << "\t" << name << "\t" << des << "\t" << basicpay << "\t" << hra << "\t"
<< da << "\t" << pf << "\t" << np << "\n";
}
};
void main()
{
int i, n;
char ch;
salary s[10];
clrscr();
cout << "Enter the number of employee:";
cin>>n;
for (i = 0; i < n; i++)
{
s[i].get();
s[i].get1();
s[i].calculate();
}
cout << "\ne_no \t e_name\t des \t bp \t hra \t da \t pf \t np \n";
for (i = 0; i < n; i++)
{
s[i].display();
}
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:-
Enter the number of employee : 2
Enter the employee number : 101
Enter the employee name : siva
Enter the employee designation : Manager
Enter the employee Basicpay : 25000
Enter the House rent Allowance: 2000
Enter the Dearness Allowance:1500
Enter the Profitablity Fund : 500
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 5
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
/* Calculate the Area and Perimeter of Different shapes using Virtual functions */
#include <iostream.h>
#include <conio.h>
class shape
{
protected:
double x,y,b,h;
public:
virtual void get_data()=0;
virtual void display_area()=0;
virtual void perimeter()=0;
};
class triangle : public shape
{
public:
void get_data(void)
{
cout<<"\n\n=====Data Entry for Triangle=====\n\n";
cout<<"Enter base, opposite, hypotenuse and height respectively : ";
cin>>b>>x>>y>>h;
}
void display_area(void)
{
cout<<"\n\n=====Area of Triangle=====\n\n";
double aot;
aot = 0.5 * b * h;
cout<<"Area of Triangle is "<<aot;
}
void perimeter(void)
{
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
cout<<"\n\n=====Perimeter of Triangle=====\n\n";
double pot;
pot=b+x+y;
cout<<"Perimeter of Triangle is "<<pot;
} };
class square : public shape
{
public:
void get_data(void)
{
cout<<"\n\n=====Data Entry for Square =====\n\n";
cout<<"Enter a side of a square : ";
cin>>x;
}
void display_area(void)
{
cout<<"\n\n=====Area of Square=====\n\n";
double aos;
aos = x * x;
cout<<"Area of Square is "<<aos;
}
void perimeter(void)
{
cout<<"\n\n=====Perimeter of Square=====\n\n";
double pos;
pos=2*x;
cout<<"Perimeter of Square is "<<pos;
}
};
class rectangle : public shape
{
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
public:
void get_data(void)
{
cout<<"\n\n=====Data Entry for Rectangle=====\n\n";
cout<<"Enter length of two sides : ";
cin>>x>>y;
}
void display_area(void)
{
cout<<"\n\n=====Area of rectangle=====\n\n";
double aor;
aor = x * y;
cout<<"Area of Rectangle is "<<aor;
}
void perimeter(void)
{
cout<<"\n\n=====Perimeter of Rectangle=====\n\n";
double por;
por=2*(x+y);
cout<<"Perimeter of Rectangle is "<<por;
}
};
void main()
{
clrscr();
triangle tri;
rectangle rect;
square sq;
shape *list[3];
list[0]=&tri;
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
list[1]=▭
list[2]=&sq;
int choice;
while(1)
{
clrscr();
cout<<"\n=====MEASURES OF DIFFERENT SHAPE=====\n";
cout<<"\nChoose your choice\n";
cout<<"1) Area & Perimeter of Triangle\n";
cout<<"2) Area & Perimeter of Rectangle\n";
cout<<"3) Area & Perimeter of Square \n";
cout<<"4) Exit\n";
cout<<"Enter your choice:-";
cin>>choice;
switch(choice)
{
case 1 : list[0]->get_data();
list[0]->display_area();
list[0]->perimeter();
getch();
break;
case 2 : list[1]->get_data();
list[1]->display_area();
list[1]->perimeter();
getch();
break;
case 3 : list[2]->get_data();
list[2]->display_area();
list[2]->perimeter();
getch();
break;
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 6
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
cout<<"Enter Element : ";
cin>>a[i][j];
}
}
}
void main()
{
int m1[10][10],m2[10][10],m3[10][10],row,col;
float x[10][10],y[10][10],z[10][10];
clrscr();
cout<<"Matrix Addition :\n";
cout<<"\n**************** :";
cout<<"Enter number of rows :";
cin>>row;
cout<<"Enter number of columns :";
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
cin>>col;
cout<<"Read integer first Matrix\n";
read_arr(m1,row,col);
cout<<"Read integer second Matrix\n";
read_arr(m2,row,col);
add_arr(m1,m2,m3,row,col);
cout<<"Additon of two Integer Matrix\n";
print_arr(m3,row,col);
add_arr(x,y,z,row,col);
cout<<"Addition of two FLOAT Matrix\n";
print_arr(z,row,col);
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
Matrix Addition
**************
Enter number of rows : 2
Enter number of columns : 2
Read integer first Matrix
Enter Element : 2
Enter Element : 2
Enter Element : 2
Enter Element : 2
Read integer second Matrix
Enter Element : 3
Enter Element : 3
Enter Element : 3
Enter Element : 3
Addition of two Integer Matrix
5 5
5 5
Read FLOAT first Matrix
Enter Element : 2.0
Enter Element : 2.0
Enter Element : 2.0
Enter Element : 2.0
Read FLOAT second Matrix
Enter Element : 3.0
Enter Element : 3.0
Enter Element : 3.0
Enter Element : 3.0
Addition of two FLOATMatrix
5.0 5.0
5.0 5.0
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 7
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
clrscr();
cout<<"\n\t Infix to Postfix Conversion";
cout<<"\n --------------------------------------------";
cout<<"\n\nEnter Infix Expression : ";
cin>>infix;
push('#');
while( (ch=infix[i++]) != '\0')
{
if( ch == '(')
push(ch);
else if(isalnum(ch))
postfix[k++]=ch;
else if( ch == ')')
{
while( s[top] != '(')
postfix[k++]=pop();
elem=pop(); /* Remove ( */
}
else
{
while( pr(s[top]) >= pr(ch) )
postfix[k++]=pop();
push(ch);
}
}
while( s[top] != '#')
postfix[k++]=pop();
postfix[k]='\0';
cout<<"\nPostfix Expression = \n"<<postfix;
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
----------------------------------
a+b-c
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 8
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
/* Stack Operations */
#include<iostream.h>
#include<conio.h>
#include<process.h>
#define SIZE 10
static int top;
class stack
{
private:
int ar[SIZE];
public:
stack(void)
{
top=-1;
}
void push(int item);
void pop();
void display();
};
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
cout<<ar[i]<<"\n ";
}
void main()
{
char choice;
int ch,num;
stack ob;
do
{
clrscr();
cout<<"\n\n\t\t\tS T A C K O P E R A T I O N S";
cout<<"\n\t\t\t-------------------------------";
cout<<"\n\n1.PUSH";
cout<<"\n2.POP";
cout<<"\n3.DISPLAY";
cout<<"\n4.EXIT";
cout<<"\n\nEnter your choice:";
cin>>ch;
switch(ch)
{
case 1: cout<<"\nEnter the Element you want to Push:";
cin>>num;
ob.push(num);
break;
case 2: ob.pop(); break;
case 3: ob.display(); break;
case 4: exit(0);
default: cout<<"\nPlease Enter a Valid Choice(1-4)!!!";
}
cout<<"\nDo you want to Continue(Y/N):";
cin>>choice;
}while(choice=='y' || choice=='Y');
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:-
STACK OPERATIONS
1.PUSH
2.POP
3.DISPLAY
4.EXIT
33
23
Do you want to continue(Y/N):N
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 9
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
#include<iostream.h>
#include<string.h>
#include<conio.h>
int main()
{
char string[40];
char *ptr,*rev;
clrscr();
cout<<"\nEnter a string : ";
cin>>string;
ptr=string;
while(*ptr!=NULL)
{
++ptr;
}
--ptr;
for(rev=string; ptr>=rev;){
if(*ptr == *rev)
{
--ptr;
rev++;
}
else
break;
}
if(rev>ptr)
cout<<"\nThe given string is Palindrome\t"<<string;
else
cout<<"\nThe given string is not Palindrome\t"<<string;
getch();
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
EX : NO : 10
DATE:
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
fin1.open("c:\\turboc3\\projects\\file1.txt");
fin2.open("c:\\turboc3\\projects\\file2.txt");
if(fin1==NULL || fin2==NULL)
{
cout<<"\n Invalid File Name. \n There is no such File or Directory ...";
exit(EXIT_FAILURE);
}
fout.open("c:\\turboc3\\projects\\file3.txt");
if(!fout)
{
cout<<"\n Invalid File Name. \n There is no such File or Directory ...";
exit(EXIT_FAILURE);
}
while(fin1.eof()==0)
{
fin1>>ch;
fout<<ch;
}
while(fin2.eof()==0)
{
fin2>>ch;
fout<<ch;
}
cout<<"\n Two Files have been Merged into "<<file_name3<<" File Successfully...!!!";
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
fin1.close();
fin2.close();
fout.close();
getch();
}
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.
OUTPUT:-
Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.