You are on page 1of 56

(Affiliated to Periyar University, Salem-11)

KOMARAPALAYAM - 637303.

DEPARTMENT OF COMPUTER APPLICATIONS

BACHELOR OF COMPUTER APPLICATIONS

PROGRAMMING IN C++

(17UCAP04 )

PRACTICAL RECORD

Name : _____________________________

Register No : _____________________________

FIRST YEAR (IV-SEMESTER)

PRACTICAL - IV

MAR / APR- 2020


(Affiliated to Periyar University, Salem-11)
KOMARAPALAYAM - 637303.

DEPARTMENT OF COMPUTER APPLICATIONS

BACHELOR OF COMPUTER APPLICATIONS

BONAFIDE CERTIFICATE

Certified that this is a bonafide record of practical work done by the

Candidate Mr / Ms ___________________________________________ with

Reg No. ______________________ in the Computer Laboratory at Excel

College for Commerce and Science, Komarapalayam.

Staff in-charge Head of the Department

Submitted for the Practical Examination held on _________________

Internal Examiner External Examiner


CONTENTS
PAGE
S.NO DATE NAME OF THE PROGRAM SIGN
NO.

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.

/* Perform Addition, Subtraction, Multiplication, Division using Class */

#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

Enter the x value


10
Enter the y value
20
Multiply Result : 200

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.

/*Program to Overload Arithmetic Operators*/


#include<iostream.h>
#include<conio.h>

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

FLOAT FLOAT :: operator /(FLOAT b)

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:

ENTER AN FLOATING NUMBER


20.00
ENTER AN FLOATING NUMBER
20.00

Overloading Arithmetic Operators


***************************
ADDITION OF TWO OBJECTS
ANSWER IS : 40.00
MULTIPLICATION OF TWO OBJECTS
ANSWER IS : 400.00
SUBSTRACTION OF TWO OBJECTS
ANSWER IS : 0.00
DIVISION OF TWO OBJECTS
ANSWER IS : 1

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.

/* String Compare and Concatenation using ++ and == operators */

#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

cout<<"\n\n Second String is : ";


str2.displaystr(); //Displaying Second 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:

Enter a String : computer


Enter a String : Languages
--------------------------------------------------------
First String is : computer
Second String is : Languages
Concatenated String is : computerLanguages
--------------------------------------------------------

Enter a String : computer


Enter a String : computer

Strings are Equal


--------------------------------------------------------

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.

/* Salary Calculation using Inheritance */


#include<iostream.h>
#include<conio.h>

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

class salary : public emp


{
float hra, da, pf, np;
public:
void get1()
{
cout << "Enter the House Rent Allowance:";
cin>>hra;
cout << "Enter the Dearness Allowance :";
cin>>da;
cout << "Enter the Profitablity Fund:";
cin>>pf;
}

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

Enter the employee number : 101


Enter the employee name : sri
Enter the employee designation : Asst. Manager
Enter the employee Basicpay : 22000
Enter the House rent Allowance: 2000
Enter the Dearness Allowance:1500
Enter the Profitablity Fund : 500

Emp_no name basicpay hra da pf np


101 siva 25000 2000 1500 500 28000
102 sri 22000 2000 1500 500 25000

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]=&rect;
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.

case 4 : goto end;


default: cout<<"\n\nInvalid choice\nTry again\n";
getch();
}
}
end:
}

Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.
Page No.

OUTPUT:

=====MEASURES OF DIFFERENT SHAPE=====


Choose your choice
1) Area & Perimeter of Triangle
2) Area & Perimeter of Rectangle
3) Area & Perimeter of Square
4) Exit
Enter your choice:- 2
=====Data Entry for Rectangle=====
Enter the length Two Sides :
10
20
=====Area of rectangle=====
Area of Rectangle is 120
=====Perimeter of Rectangle=====\
Perimeter of Rectangle is 24

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.

/*Addition of two matrices using function overloading */


#include<iostream.h>
#include<conio.h>
void read_arr(int a[10][10],int row,int col)
{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
cout<<"Enter Element : ";
cin>>a[i][j];
}
}
}

void add_arr(int m1[10][10],int m2[10][10],int m3[10][10],int row,int col)


{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
m3[i][j] = (m1[i][j] + m2[i][j]);
}
}
}

void print_arr(int m[10][10],int row,int col)


{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
cout<<m[i][j];
}
cout<<"\n";
}
}
void read_arr(float a[10][10],int row,int col)
{
int i,j;

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 add_arr(float x[10][10],float y[10][10],float z[10][10],int row,int col)


{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
z[i][j] = (x[i][j] + y[i][j]);
}
}
}

void print_arr(float m[10][10],int row,int col)


{
int i,j;
for(i=1;i<=row;i++)
{
for(j=1;j<=col;j++)
{
cout<<m[i][j];
}
cout<<"\n";
}
}

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

cout<<"Read FLOAT first Matrix\n";


read_arr(x,row,col);
cout<<"Read FLOAT second Matrix\n";
read_arr(y,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.

/*Infix to Postfix Conversion */


#include<iostream.h>
#include<conio.h>
#define SIZE 50
#include <ctype.h>
char s[SIZE];
int top=-1;
push(char elem)
{
s[++top]=elem;
}
char pop()
{
return(s[top--]);
}
int pr(char elem)
{
switch(elem)
{
case '#': return 0;
case '(': return 1;
case '+':
case '-': return 2;
case '*':
case '/': return 3;
}
}
void main()
{
char infix[50],postfix[50],ch,elem;
int i=0,k=0;

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:

Infix to Postfix Conversion

----------------------------------

Enter the Infix Expression

a+b-c

Postfix Expression = ab+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();
};

void stack::push(int item)


{
if(top==SIZE-1)
cout<<"\nThe Stack is Full!!! Stack Overflow";
else
{
ar[++top]=item;
cout<<"\nElement succesfully pushed in the Stack!!!";
}
}
void stack::pop()
{
if(top<0)
cout<<"\nStack Under flow!!!";
else
{
top--;
cout<<"\nElement sucessfully popped from the Stack!!!";
}
}
void stack::display()
{
if(top<0)
cout<<"\nThe Stack is Empty it cannot be displayed!!!";
else
for(int i=top;i>=0;i--)

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

Enter your choice:1

Enter the element you want to push:23

Element successfully pushed in the stack!!!

Do you want to continue(Y/N):y

Enter your choice:1

Enter the element you want to push:33

Elements successfully pushed in the stack!!!

Do you want to continue(Y/N):y

Enter your choice:3

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.

/* Check Whether given string is Palindrome or not using pointers */

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

Enter a Sting: madam


The given string is palindrome madam

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.

/*Merging Two Files */


#include<iostream.h>
#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
int main()
{
ifstream fin1, fin2;
ofstream fout;
char ch, file_name1[20], file_name2[20], file_name3[30];
clrscr();
cout<<"\n Enter First File Name with Extension '.txt' : ";
gets(file_name1);
cout<<"\n Enter Second File Name with Extension '.txt' : ";
gets(file_name2);
cout<<"\n Enter Third File Name with Extension '.txt' ";
cout<<"\n (which will Store the Contents of \n First File and Second File) : ";
gets(file_name3);

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

Enter First File Name with Extension '.txt' : file1.txt


Enter Second File Name with Extension '.txt' : file2.txt
Enter Third File Name with Extension '.txt' ": file3.txt
(which will Store the Contents of \n First File and Second File)

Two Files have been Merged into file3.txt File Successfully…!!!

Department Of Computer Applications – Excel College for Commerce and Science , Komarapalayam.

You might also like