0% found this document useful (0 votes)
14 views36 pages

ADALAB

The document contains multiple programming exercises demonstrating various algorithms and data structures, including Tower of Hanoi, binary search tree traversals, stack using linked lists, circular queue, quick sort, heap sort, and the knapsack problem using a greedy method. Each exercise includes the aim, program code, output, and a result statement confirming successful execution. The programs are written in C++ and cover fundamental concepts in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views36 pages

ADALAB

The document contains multiple programming exercises demonstrating various algorithms and data structures, including Tower of Hanoi, binary search tree traversals, stack using linked lists, circular queue, quick sort, heap sort, and the knapsack problem using a greedy method. Each exercise includes the aim, program code, output, and a result statement confirming successful execution. The programs are written in C++ and cover fundamental concepts in computer science.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 36

EX. NO. : 1 Tower of Hanoi using recursion Pg.

No : 1

Date : 18-07-2024

Aim

To write a program to solve the tower of Hanoi using recursion.

Program

#include<iostream.h>
#include<conio.h>
void hanoi(int n,char sour, char ext,char des)
{
if(n==1)
{
cout<<"Move Disk "<<n<<" from "<<sour<<" to "<<des<<endl;
return;
}
hanoi(n-1,sour,des,ext);
cout<<"Move Disk "<<n<<" from "<<sour<<" to "<<des<<endl;
hanoi(n-1,ext,sour,des);
}
int main()
{
int n;
clrscr();
cout<<"Enter no. of disks:";
cin>>n;
hanoi(n,'A','B','C');
getch();
return 0;
}
OUTPUT:

Move disk 1 from rod A to rod C


Move disk 1 from rod A to rod B
Move disk 1 from rod C to rod B
Move disk 1 from rod A to rod C
Move disk 1 from rod B to rod A
Move disk 1 from rod B to rod C
Move disk 1 from rod A to rod C

Result
Thus the program was executed successfylly and the output was verified.
Ex. NO. : 2 Traverse through binary search tree Pg. No : 2

Date : 25-07-24 using traversals

Aim
To write a program to traverse through binary search tree using traversals.

Program

#include<iostream.h>
#include<conio.h>
class node
{
public:
int data;
node *lchild;
node *rchild;
};
class binarytree
{
public:
binarytree()
{
node *root=NULL;}
void insert(node *&root, int val)
{
if(root ==NULL)
{
root=new node;
root->data=val;
root->lchild=root->rchild=NULL;
}
else if(val< root->data)
{
insert(root->lchild,val);
}
else
{
insert(root->rchild,val);
}
}
void preorder(node *root)
{
if(root !=NULL)
{
cout<<root->data<<" "; Pg. No : 4
preorder(root->lchild);
preorder(root->rchild);

}
}
void inorder(node *root)
{
if(root !=NULL)
{
inorder(root->lchild);
cout<<root->data<<" ";
inorder(root->rchild);
}
}
void postorder(node *root)
{
if(root!=NULL)
{
postorder(root->lchild);
postorder(root->rchild);
cout<<root->data<<" ";
}
}
};
int main()
{
clrscr();
int ch,val;
node *root=NULL;
binarytree b;
do
{
cout<<"\n1.INSERTION";
cout<<"\n2.Preorder";
cout<<"\n3.Postorder";
cout<<"\n4.Inorder";
cout<<"\n5.EXIT";
cout<<"\nEnter your Choice : ";
cin>>ch;
switch(ch)
{
case 1:
cout<<"\nEnter the value for insertion : ";
cin>>val;
b.insert(root,val);
break;
case 2:
cout<<"\nPreorder Traversal";
b.preorder(root); Pg. No : 5
break;
case 3:
cout<<"\nPostorder Traversal";
b.postorder(root);
break;
case 4:
cout<<"\nInorder Traversal";
b.inorder(root);
break;
default:
cout<<"\nInvalid";
return 0;
}
}while(ch!=4);
getch();

OUTPUT:

1.Insert
2.Preorder
3.Inorder
4.Postorder
5.Exit

Enter your choice: 1


Enter the value: 20

Enter your choice: 1


Enter the value: 10

Enter your choice: 1


Enter the value: 40

Enter your choice: 1


Enter the value: 5

Enter your choice: 1


Enter the value: 15

Enter your choice: 1


Enter the value: 30

Enter your choice: 1


Enter the value: 45
Pg. No : 6
Enter your choice: 2
Preorder:
20 10 5 15 40 30 45

Enter your choice: 3


Inorder:
5 10 15 20 30 40 45

Enter your choice: 4


Postorder:
5 15 10 30 45 40 20

Enter your choice:5

Result
Thus the program was executed successfully and the output was verified.
EX. No. : 3 Stack Using Linked List Pg. No : 7

Date :

Aim
To write a program to perform various operation on stack using linked list.

Program
#include<iostream.h>
#include<conio.h>
struct node
{
int data;
int top;
node* link;
};
struct node *top=NULL;
struct node *temp=new node;
void push( int val)
{
temp=new node;
temp->data=val;
temp->link=top;
top=temp;
}
void pop(int val)
{
if(top==0)
{
cout<<"Stack is empty";
}
else
{
val=top->data;
cout<<"Deleted Value is : "<<val;
temp=top;
top=top->link;
}
}
void main()
{
clrscr();
int ch,val;
cout<<"\n\t\tSTACK USING LINKED LIST ";
cout<<"\n1.PUSH OPERATION";
cout<<"\n2.POP OPERATION";
cout<<"\n3.DISPLAY";
cout<<"/nEnter Your Choice :";
cin>>ch;
switch(ch) Pg. No : 8
{
case 1:
push(val);
break;
case 2:
pop(val);
break;
case 3:
display();
default:
cout<<"Invalid Choice";
}
getch();
}

Output
STACK USING LINKED LIST
1.PUSH OPERATION
2.POP OPERATION
3.DISPLAY
Enter your choice: 1
Enter the value: 55
Stack elements are: 55

Enter your choice: 1


Enter the value: 66
Stack elements are: 66 55

Enter your choice: 1


Enter the value: 77
Stack elements are: 77 66 55

Enter your choice: 1


Enter the value: 88
Stack elements are: 88 77 66 55
Enter your choice: 1 Pg. No : 9
Enter the value: 99
Stack elements are: 99 88 77 66 55

Enter your choice: 2


Enter the value: 88 77 66 55

Enter your choice: 2


Enter the value: 77 66 55

Enter your choice: 2


Enter the value: 66 55

Enter your choice: 2


Enter the value: 55
Enter your choice: 2
Stack is Empty.
Enter your choice:3
Exit.

Result
Thus the program was executed successfully and the output was verified.
EX.NO. : 4 Circular Queue Pg. No : 10
Date :

Aim
To write a program to perform various operation in Circular Queue.

Program
#include<iostream.h>
#include<conio.h>
int f=-1,r=-1;
class circularqueue
{
int queue[50];
public:
void insert(int val,int n)
{
if (((r+1)%n)==f)
{
cout<<"Queue is full\n";
}
else if((f==-1)&&(r==-1))
{
f=0;r=0;
queue[r]=val;
}
else
{
r=((r+1)%n);
queue[r]=val;
}
}
void del(int n)
{
if((f==-1)&& (r==-1))
{
cout<<"Queue is empty \n";
}
else if(f==r)
{
int x=queue[f];
cout<<"Deleted value is : "<<x;
cout<<"\nQueue is empty\n";
f=-1;r=-1;
}
else
{ Pg. No : 11
int x=queue[f];
cout<<"Deleted value is : "<<x;
f=(f+1) % n;
}
}
void display(int n) {
int i;
if (f==-1&&r==-1) {
cout << endl
<< "Empty Queue" << endl;
} else {
cout<<"Displaying"<<endl;

cout << endl


<< "Circular queue Items -> ";
for (i = f; i != r; i = (i + 1) % n)
cout << queue[i]<<" ";
cout << queue[i]<<" ";
cout << endl;
}
}
};
int main()
{
clrscr();
circularqueue q;
int ch,item,n;
cout<<"\nEnter Queue size: ";
cin>>n;
do
{
cout<<"\n1.INSERT \n";
cout<<"2.DELETE\n";
cout<<"3.DISPLAY\n";
cout<<"4.EXIT\n";
cout<<"Enter Choice : "<<endl;
cin>>ch;
switch(ch)
{
case 1:
cout<<"Input for insertion :"<<endl;
cin>>item;
q.insert(item,n);
break;
case 2:
cout<<"Deleting"<<endl;
q.del(n);
break;
case 3:
Pg. No : 12
q.display(n);
break;
default:
cout<<"Incorrect";
break;
}
}
while(ch!=4);
return 0;
getch();

Output
Enter Queue size : 3
1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 1
Input for insertion : 20

1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 1
Input for insertion : 30

1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 1
Input for insertion : 89

1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 1
Input for insertion : 75
Queue is full
Pg. No : 13
1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 2
Deleting
Deleted value is : 20

1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 2
Deleting
Deleted value is : 30

1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 3
Displaying
Circular Queue items -> 89

1.Insert
2.Delete
3.Display
4.Exit

Enter choice : 2
Deleting
Deleted value is 89
Queue is empty

Result

Thus the program was executed successfully and the output was verified.
EX. NO. : 5 Quick Sort
Date : Pg. No : 14

Aim
To write a program to perform sort an array of an elements using quick sort.

Program

#include<iostream.h>
#include<conio.h>
int i,j,p;
void swap(int*i,int*j)
{
int temp=*i;
*i=*j;
*j=temp;
}
partition(int a[],int l,int h)
{
i=l,j=h;
p=a[i];
while(i<j)
{
while(p>=a[i])
{
i++;
}
while(p<a[j])
{
j--;
}
if(i<j)
swap(&a[i],&a[j]);
}
swap(&a[l],&a[j]);
return j;
}
void quicksort(int a[],int l,int h)
{
if(l<h)
{
int loc=partition(a,l,h);
quicksort(a,l,loc-1);
quicksort(a,loc+1,h);
}
}
void main()
{
int n,i,a[50]; Pg. No : 15
clrscr();
cout<<"Enter the size:"<<endl;
cin>>n;
cout<<"Enter the elements:"<<endl;
for(i=0;i<n;i++)
cin>>a[i];
quicksort(a,0,n-1);
cout<<"Sorted elements are:"<<endl;
for(i=0;i<n;i++)
cout<<a[i]<<"\t";
getch();
}

OUTPUT
Enter the Size ; 5
Enter the elements
2
3
16
89
32
Sorted elements are :
2 3 16 32 89

Result

Thus the program was executed successfully and the output was verified.

EX. NO. : 6 Heap Sort


Date : Pg. No : 12

Aim
To write a program to sort number of elements in ascending order using heap sort.

Program

#include <iostream.h>
#include <conio.h>
void maxHeapify(int arr[], int n, int i)
{
int h = i;
int l = 2 * i + 1;
int r = 2 * i + 2;
if (l < n && arr[l] > arr[h])
h = l;
if (r < n && arr[r] > arr[h])
h = r;
if (h != i)
{
int temp = arr[i];
arr[i] = arr[h];
arr[h] = temp;
maxHeapify(arr, n, h);
}
}
void heapSort(int arr[], int n)
{
for (int i = n / 2 - 1; i >= 0; i--)
maxHeapify(arr, n, i);
for (i = n - 1; i >= 0; i--)
{

int temp = arr[0];


arr[0] = arr[i];
arr[i] = temp;
maxHeapify(arr, i, 0);
}
}
Pg. No : 12
int main()
{
clrscr();
int n;
cout << "Enter the number of elements: ";
cin >> n;
int arr[100];
cout << "Enter " << n << " elements: ";
for (int i = 0; i < n; i++)
cin >> arr[i];
heapSort(arr, n);
cout << "Sorted array in ascending order: ";
for (i = 0; i < n; i++)
cout << arr[i] << " ";
getch();
return 0;
}

Output

Enter array size : 5


Enter array elements
70
50
80
90
60
Sorted array is
50 60 70 80 90

Result
Thus the program was executed successfully and the output was verified.

EX. NO. : 7 Knap Sack using greedy method Pg. No : 12


Date :

Aim
To write a program to perform the knap sack problem using greedy method.

Program

#include<conio.h>
#include <iostream.h>
int main()
{
float weight[50], profit[50], ratio[50], totalValue = 0, capacity, temp;
int n,i;
cout << "Enter the number of items: ";
cin >> n;
for (i = 0; i < n; i++)
{
cout << "Enter Weight and Profit for item[" << i << "]:\n";
cin >> weight[i] >> profit[i];
}
cout << "Enter the capacity of the knapsack:\n";
cin >> capacity;
for (i = 0; i < n; i++)
ratio[i] = profit[i] / weight[i];
for (i = 0; i < n; i++) {
for (int j = i + 1; j < n; j++) {
if (ratio[i] < ratio[j]) {
temp = ratio[j];
ratio[j] = ratio[i];
ratio[i] = temp;

temp = weight[j];
weight[j] = weight[i];
weight[i] = temp;

temp = profit[j];
profit[j] = profit[i];
profit[i] = temp;
}
}
}
cout << "Knapsack problem using Greedy Algorithm:\n";
for (i = 0; i < n; i++) {
if (weight[i] > capacity)
break; Pg. No : 12
else {
totalValue += profit[i];
capacity -= weight[i];
}
}
if (capacity > 0 && i < n) {
totalValue += ratio[i] * capacity;
}
cout << "\nThe maximum value is: " << totalValue << endl;
return 0;
getch();
}

Output
Enter the number of items: 4
Enter Weight and Profit for item [0]:
40 280
Enter Weight and Profit for item [1]:
10 100
Enter Weight and Profit for item [2]:
20 120
Enter Weight and Profit for item [3]:
24 120
Enter the capacity of the knapsack :
60
Knapsack problem using Greedy Algorithm:
The maximum value is: 440

Result
Thus the program was executed successfully and the output was verified.
EX.NO : 8 Search an element using Pg. No : 12
Date : Divide & Conquer

Aim
To write a program to search for an element using divide and conquer strategy.

Program

#include<iostream.h>
#include<conio.h>
int bsearch(int a[],int l,int h,int x)
{
while(l<=h)
{
int m=(l+h)/2;
if(a[m]==x)
return m;
if(x<a[m])
h=m-1;
else
l=m+1;
}
return -1;
}
void main()
{
int a[50],n,i,x,p;
clrscr();
cout<<"Enter the size:";
cin>>n;
cout<<"Enter the elements\n";
for(i=0;i<n;i++)
cin>>a[i];
cout<<"\nEnter the x element:";
cin>>x;
p=bsearch(a,0,n-1,x);
if(p==-1)
cout<<"\nNot found";
else
cout<<"\nThe element position is:"<<p;
getch();
}

Output Pg. No : 12
Enter the array size: 5
Enter 5 elements:
33
45
23
56
12
Enter the X element : 23
The element Position is: 3

Result
Thus the program was executed successfully and the output was verified.
EX.NO. : 9 N Queens Problem Pg. No : 12
Date :

Aim
To write a program to place the N Queens on an n*n matrix so that no two queens attack.

Program

#include<iostream.h>
#include<conio.h>
#define MAX 10
int board[MAX][MAX];
void printSolution(int N) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (board[i][j] == 1)
cout << "Q ";
else
cout << ". ";
}
cout << endl;
}
cout << endl;
}
int isSafe(int row, int col, int N) {
for (int i = 0; i < row; i++)
if (board[i][col] == 1)
return 0;
for (int i1 = row, j1 = col; i1 >= 0 && j1 >= 0; i1--, j1--)
if (board[i1][j1] == 1)
return 0;

for (int i2 = row, j2 = col; i2 >= 0 && j2 < N; i2--, j2++)


if (board[i2][j2] == 1)
return 0;
return 1;
}
int solveNQueens(int row, int N) {
if (row == N) {
printSolution(N);
return 1;
}
int hasSolution = 0;
for (int col = 0; col < N; col++) {
if (isSafe(row, col, N)) {
board[row][col] = 1 Pg. No : 12
hasSolution = solveNQueens(row + 1, N) || hasSolution;
board[row][col] = 0;
}
}
return hasSolution;
}
int main() {
clrscr();
int N;
cout << "Enter the value of N (size of the chessboard): ";
cin >> N;
for (int i = 0; i < N; i++)
for (int j = 0; j < N; j++)
board[i][j] = 0;

if (!solveNQueens(0, N))
cout << "No solutions exist for " << N << "-Queens problem." << endl;

return 0;
getch();
}

Output
Enter the value of N (size of the chessboard): 4
. Q. .
. . . Q
Q . . .
. . Q .

. . Q .
Q . . .
. . . Q
. Q. .

Result

Thus the program was executed successfully and the output was verified.
Pg. No : 12
EX. NO. : 10 Virtual Function
Date :

Aim

To write a program to perform Virtual function.

Program

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

class passenger
{
public:
virtual void ticket()=0;//pure virtual function and also abstrac class because no definition

virtual void info()


{
cout<<"\n\nLet's Fly Away in Style";
}

};
class economyclass:public passenger
{
public:
void ticket()
{
int amount=100;
cout<<"\nCOST FOR BOOKING ECONOMY CLASS : $"<<amount;
}
void info()
{
cout<<"\n\nSTOP Searching START Travelling";
}
};
void main()
{
clrscr();
cout<<"\t\t**WELCOME TO AIRLINE**"<<endl;
economyclass e;//creating object for derived class without pointer
e.ticket();
passenger *p;
p=&e; Pg. No : 12
p->info();
cout<<"\n\n\t\tTHANK YOU";

getch();
}

Output

** WELCOME TO AIRLINE**
COST FOR BOOKING ECONOMY CLASS : $100
STOP Searching START Travelling
THANK YOU

Result
Thus the program was executed successfully and the output was verified.
Pg. No : 12
EX. NO. : 11 Parameterized Constructor

Date :

Aim
To write a program to perform parameterized constructor.

Program

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

class mall
{
public:
mall(char name[],int floors,int countemp)
{
cout<<"\nName of the Mall : "<<name;
cout<<"\nNumber of floors in mall : "<<floors;
cout<<"\nNumber of employee : "<<countemp;
}
};
void main()
{
clrscr();
char mname[20];
int floor,emp;
cout<<"Enter name of the mall : ";
cin>>mname;
cout<<"Enter number of floors : ";
cin>>floor;
cout<<"Enter number of employee : ";
cin>>emp;

cout<<"\n\t\t_______MALL MANAGEMENT________";
mall m(mname,floor,emp);
getch();
}
Output Pg. No : 12

Enter name of the mall : SKYWALL


Enter number of floors : 11
Enter number of employee : 200

_______MALL MANAGEMENT________

Name of the Mall : SKYWALL


Number of floors in mall : 11
Number of employee : 200

Result
Thus the program was executed successfully and the output was verified.
EX.NO. : 12 Friend Function Pg. No : 12

Date :

Aim
To write a program to perform friend function.

Program

#include<iostream.h>
#include<conio.h>
class shop
{
private:
int i,a;

public:
void get()
{
cout<<"\nEnter number of items buy : ";
cin>>i;
}
friend int amount(shop);
};

int amount(shop c)
{
c.a=c.i*10;
cout<<"\nTotal Amount : "<<c.a;
return 0;
}
void main()
{
clrscr();
shop s;
cout<<"\n\t***Ten Rupee Shop***";
s.get();
amount(s);
getch();
}

Pg. No : 12
Output

***Ten Rupee Shop***


Enter number of items buy : 3
Total Amount : 30

Result
Thus the program was executed successfully and the output was verified.

Pg. No : 12
EX. NO. : 13 Function Overloading
Date :

Aim
To write a program to perform Function Overloading.

Program

#include<iostream.h>
#include<conio.h>
class math
{
public:
int area(int s)
{
return (s*s);
}
int area(int l,int b)
{
return (l*b);
}
float area(float r)
{
return (3.14*r*r);
}
float area(float b,float h)
{
return(((b*h)/2));
}
};
void main()
{
clrscr();
int s,l,b;
float r,tb,h;
math m;
cout<<"Calculating area by using function overloading";
cout<<"\nEnter Side of a square : ";
cin>>s;
cout<<"Area of square is : "<<m.area(s);
cout<<"\nEnter length and breadth of the rectangle : ";
Pg. No : 12
cin>>l>>b;
cout<<"Area of rectangle is : "<<m.area(l,b);
cout<<"\nEnter radius of circle : ";
cin>>r;
cout<<"Area of circle is " <<m.area(r);
cout<<"\nEnter base and height of triangle : ";
cin>>tb>>h;
cout<<"Area of triangle is : " <<m.area(tb,h);
getch();
}

Output

Calculating Area using function overloading


Enter side of a square: 3
Area of square is 9
Enter length and breadth of rectangle: 3 6
Area of rectangle is 18
Enter radius of circle: 3
Area of circle is 28.26
Enter base and height of triangle: 4 4
Area of triangle is 8

Result

Thus the program was executed successfully and the output was verified.
EX. NO. : 14 Single Inheritance Pg. No : 12
Date :

Aim

To write a program to perform Single inheritance.

Program
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<string.h>
class school
{

public:
char sname[20];
char place[20];
void getsch()
{
cout<<"Enter School Name : ";
gets(sname);
cout<<"Enter Place : ";
gets(place);
}
};
class student:public school
{
public:
char name[20], regno[20];
int m1,m2,m3,m4,m5,t;
void getstud()
{

cout<<"Enter Your Name :";


gets(name);
cout<<"Enter Your Register no. : ";
gets(regno);
cout<<"Enter Your Mark1 : ";
cin>>m1;

cout<<"Enter Your Mark 2 : ";


cin>>m2;
cout<<"Enter Your Mark 3: ";
cin>>m3;
Pg. No : 12
cout<<"Enter Your Mark 4 : ";
cin>>m4;
cout<<"Enter Your Mark 5 : ";
cin>>m5;
t=m1+m2+m3+m4+m5;
}
void display()
{
cout<<"\n\n\t\t____________STUDENT DETAILS_____________"<<endl;
cout<<"Student Name : "<<name<<endl;
cout<<"Register Number : "<<regno<<endl;
cout<<"Mark1: "<<m1<<"\tMark2: "<<m2<<"\tMark3: "<<m3<<"\tMark4: "<<m4<<"\
tMark5: "<<m5<<endl;
cout<<"Total Mark : "<<t<<endl;
}
};
void main()
{
clrscr();
cout<<"\t\tPRINTING STUDENT DETAILS USING SINGLE INHERITANCE"<<endl;

student s;
s.getsch();
s.getstud();
s.display();
getch();
}

Pg. No : 12
Output
PRINTING STUDENT DETAILS USING SINGLE INHERITANCE
Enter School Name : SHARON MATRICULATION SCHOOL
Enter Place : VILATHIKULAM
Enter Your Name : POOJA R
Enter Your Register no. : 1134
Enter Your Mark1 : 100
Enter Your Mark 2 : 100
Enter Your Mark 3: 100
Enter Your Mark 4 : 100
Enter Your Mark 5 : 100

____________STUDENT DETAILS_____________
Student Name : SHARON MATRICULATION SCHOOL
Register Number : 1134
Mark 1 : 100 Mark 2 : 100 Mark 3 : 100 Mark 4 : 100 Mark 5 : 100
Total Mark : 500

Result
Thus the program was executed successfully and the output was verified.

EX. NO. : 15 Files Pg. No : 12


Date :

Aim
To write a program to perform employee details using files.

Program
#include <iostream.h>
#include <fstream.h>
#include<conio.h>
void addEmployee() {
ofstream outFile("employees.txt", ios::app);
if (!outFile) {
cout<< "Unable to open file for writing."<< endl;
return;
}
int id;
char name[50];
double salary;
cout << "Enter Employee ID: ";
cin >> id;
cin.ignore();
cout << "Enter Employee Name: ";
cin.getline(name, 50);
cout << "Enter Employee Salary: ";
cin >> salary;
outFile << id << endl;
outFile << name << endl;
outFile << salary << endl;
outFile.close();
cout << "Employee details added successfully." << endl;
}
void displayEmployees() {
ifstream inFile("employees.txt");
if (!inFile) {
cout<< "Unable to open file for reading." << endl;
return;
}
int id;
char name[50];
double salary;

cout << "Employee Details:\n";


cout << "ID\t\t "<<"Name\t\t"<<"Salary " <<endl;
while (inFile >> id) {
Pg. No : 12
inFile.ignore();
inFile.getline(name, 50);
inFile >> salary;
inFile.ignore()
cout<<id<<"\t\t"<<name<<"\t\t"<<salary<<endl;
}

inFile.close();
}
void main() {
clrscr();
int choice;
do {
cout << "\nEmployee Management System\n";
cout << "1. Add Employee\n";
cout << "2. Display Employees\n";
cout << "3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addEmployee();
break;
case 2:
displayEmployees();
break;
case 3:
cout << "Exiting program." << endl;
break;
default:
cout << "Invalid choice. Please try again." << endl;
}
} while (choice != 3);
getch();
}

Output

Employee Management System


1.Add Employee
2.Display Employee
3.Exit

Enter Your Choice : 1


Enter Employee ID : 1001
Enter Employee Name : Malathi
Enter Employee Salary : 50000 Pg. No : 12
Employee details added successfully.
Employee Management System
1.Add Employee
2.Display Employee
3.Exit

Enter Your Choice : 1


Enter Employee ID : 1002
Enter Employee Name : Pooja
Enter Employee Salary : 50000
Employee details added successfully.

Employee Management System


1.Add Employee
2.Display Employee
3.Exit

Enter Your Choice : 2


ID : Name: Salary:
1001 Malathi 50000
1002 Pooja 50000

Employee Management System


1.Add Employee
2.Display Employee
3.Exit

Enter Your Choice : 3


Exiting Program.

Result
Thus the program was executed successfully and the output was verified.

You might also like