You are on page 1of 55

INDEX

S. NO. TOPIC PAGE NO. SIGNATURE


1 Electricity Units
2 Complex Number
3 Student; Highest-Lowest Marks
4 Percentage and Streams
5 Telephone Directory
6 Students in BIS
7 Bubble Sort; Binary Search
8 Uppercase/Lowercase-Vowels/Consonants
9 Hospital Registration
10 Push & Pop Operations on Stack
11 Addition/Deletion on Queue
12 Class - Append & Read Records
13 Class – Append & Delete Records
14 Add/Delete Elements from Queue
15 Push/Pop Elements from Stack
16 2-D Array Integers
17 String Length; Compare & Concatenate
18 Function Overloading
19 Addition anndsubtraction of matrices.
A menu driven program to perform
following operation on strings without using
built in functions.
20
a. Find the length of the string
b. Compare the strings
c. Concatenate two strings
21 SQL Statements
Program 1: To input the details of the customer and number of units
consumed and to calculate the electricity bill. (Electricity Bill)
#include<iostream.h>
#include<conio.h>
struct bill
{
int customerno;
int units;
float bill;
}b[15];

void main()
{
clrscr();
int n;
cout<<"\nEnter the number of customers:\t";
cin>>n;
for(int i=0; i<n; i++)
{
cout<<"\nEnter Customer Number:\t";
cin>>b[i].customerno;
cout<<"\nEnter number of units consumed: ";
cin>>b[i].units;

if(b[i].units<=100)
b[i].bill=(b[i].units*0.40);
if((b[i].units>100)&&(b[i].units<=300))
b[i].bill=(b[i].units-100)*0.50+40;
if((b[i].units>300)&&(b[i].units<=600))
b[i].bill=(b[i].units-300)*0.75+140;
if((b[i].units>600)&&(b[i].units<=1000))
b[i].bill=(b[i].units-600)*1+365;
if(b[i].units>1000)
b[i].bill=(b[i].units-1000)*1.5+765;
}

cout<<"\nInformation of all customers:\t";


for(i=0; i<n; i++)
{
cout<<"\n\tCustomer number: "<<b[i].customerno;
cout<<"\n\tNumber of units consumed: "<<b[i].units;
cout<<"\n\tThe bill is: "<<b[i].bill;
}
getch();
}
Program 2: Declare a structure to represent a complex number. WAP to
add, subtract, multiply and divide two complex numbers.
#include<iostream.h>
#include<conio.h>
struct complex
{
float re, im;
};
complex add(complex a, complex b);
complex subtract(complex a, complex b);
complex multiply(complex a, complex b);
complex div(complex a, complex b);
void main()
{
clrscr();
complex c1, c2, c3;
char ch;
cout<<"\nChoose option (1/2/3/4)";
cout<<"\n1.ADD"<<"\n2.SUBTRACT"<<"\n3.MULTIPLY"<<"\n4.DIVIDE"<<;
cin>>ch;
cout<<"Enter real part of the first complex number:\t";
cin>>c1.re;
cout<<"Enter imaginary part of the first complex number:\t";
cin>>c1.im;
cout<<"Enter real part of the second complex number:\t";
cin>>c2.re;
cout<<"Enter imaginary part of the second complex number:\t";
cin>>c2.im;
switch(ch)
{
case '1': c3=add(c1,c2); break;
case '2': c3=subtract(c1,c2); break;
case '3': c3=multiply(c1,c2); break;
case '4': c3=divide(c1,c2); break;
default:cout<<"Invalid Entry";
}
cout<<"\nResult=\t"<<c3.re<<"+i"<<c3.im;
getch();
}
complex add(complex a, complex b)
{
complex c;
c.re=a.re+b.re;
c.im=a.im+b.im;
return(c);
}
complex subtract(complex a, complex b)
{
complex c;
c.re=a.re-b.re;
c.im=a.im-b.im;
return(c);
}
complex multiply(complex a, complex b)
{
complex c;
c.re=a.re*b.re-a.im*b.im;
c.im=a.re*b.im+a.im*b.re;
return(c);
}
complex div(complex a, complex b)
{
complex c;
c.re=(a.re*b.re+a.im*b.im)/(b.re*b.re+b.im*b.im);
c.im=(a.im*b.re-a.re*b.im)/(b.re*b.re+b.im*b.im);
return(c);
}

Program 3: WAP to accept a name and total marks of 20 students in an


array. Display the names of the students including marks, securing highest
and lowest, using structures.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
struct student
{
char name[30];
float marks;
};

void main()
{
student s[100];
float max, min;
int jmax, jmin;
clrscr();
for(int i=0; i<10; i++)
{
cout<<"\nEnter name of student"<<(i+1)<<":";
gets(s[i].name);
cout<<"Enter total marks of the student: ";
cin>>s[i].marks;
}
max=min=s[0].marks;
jmax=jmin=0;
for(i=0; i<10; i++)
{
if(s[i].marks>max)
{
max=s[i].marks;
jmax=i;
}
if(s[i].marks<min)
{
min=s[i].marks;
jmin=i;
}
}
cout<<"\n"<<s[jmax].name<<" scored the highest marks: "<<s[jmax].marks;
cout<<"\n"<<s[jmin].name<<" scored the lowest marks: "<<s[jmin].marks;
getch();
}
Program 4: WAP to enter marks in 5 subjects and to a lot streams
accordingly.
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
#include<string.h>
class student
{
char name[30];
int rno;
char sub[20];
float m1,m2,m3,m4,m5;
float perc;
void calc()
{
perc=(m1+m2+m3+m3+m4+m5)/5;
}
public:
void getdata();
void display();
};

void student::getdata()
{
cout<<"\n Enter roll no: ";
cin>>rno;
cout<<"\n Enter name: ";
gets(name);
cout<<"\n Enter marks for 5 subjects ";
cin>>m1>>m2>>m3>>m4>>m5;
calc();
if(perc>=96)
strcpy(sub,"Computer Science");
else if(perc>=91)
strcpy(sub,"Electronics");
else if(perc>=86)
strcpy(sub,"Mechanical");
else if(perc>=81)
strcpy(sub,"Electrical");
else if(perc>=76)
strcpy(sub,"Chemistry");
else if(perc>=71)
strcpy(sub,"Civil");
}

void student::display()
{
cout<<"Alloted subjects is "<<sub;
}

void main()
{
clrscr();
student s;
s.getdata();
s.display();
getch();
}
Program 5: WAP to read a telephone directory with name, phone and write
it to a file in the format “Hi <name>, your number is <phone>”.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>

struct tel
{
char name[50];
long pno;
};

void main()
{
clrscr();
tel t; fstream f1,f2; char ch;
f1.open("teledir.txt",ios::out);
do
{
cout<<"\n Enter name: ";
cin>>t.name;
cout<<"\n Enter telephone no :";
cin>>t.pno;
f1.write((char*)&t,sizeof(t));
cout<<"\n Want to enter more?(Y/N): ";
cin>>ch;
}
while(ch=='y'||ch=='Y');
f1.close();
f1.open("teledir.txt",ios::in);
f2.open("newfile.txt",ios::out);
while(f1.read((char*)&t,sizeof(t)))
{
f2<<"Hi "<<t.name<<", your number is "<<t.pno<<"\n";
}
f1.close();
f2.close();
cout<<"\n\t New file created";
cout<<"\n\n\t Showing new file :";
f2.open("newfile.txt",ios::in);
char st[80];
while(!f2.eof())
{
cout<<"\n";
f2.getline(st,80,'\n');
cout<<st;
}
f1.close();
f2.close();
getch();
}

Program 6: WAP to store the date of students registering in BIS –


admission no., name, address, DOB, class and section. For every new
registration, assign a roll number in sequential order. Read this file and
display roll no., name, class and section.
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include<string.h>

struct date
{
int dd,mm,yy;
};

class student
{
private:
int admn;
char name[20];
char address[100];
date dob;
int clas;
char section;
public:
void getdata()
{
cout<<"\n Enter admission number: ";
cin>>admn;
cout<<"\n Enter name: ";
cin>>name;
cout<<"\n Enter address: ";
cin>>address;
cout<<"\n Enter date of birth (dd-mm-yy): ";
cin>>dob.dd>>dob.mm>>dob.yy;
cout<<"\n Enter clas: ";
cin>>class;
cout<<"\n Enter section: ";
cin>>section;
}
void showdata()
{
cout<<"\n Name: "<<name;
cout<<"\n Class: "<<clas;
cout<<"\n Section: "<<section;
}
}ob[20];

void main()
{
clrscr();
fstream file("admission.txt",ios::in|ios::out);
int n;
cout<<"\n How many entries? ";
cin>>n;
for(int i=0; i<n; i++)
{
ob[i].getdata();endl;endl;
file.write((char*)&ob,sizeof(ob));
}
cout<<"\n\t Student Information "<<endl;
for(int k=0; k<n; k++)
{
file.read((char*)&ob,sizeof(ob));
cout<<"\n Your roll no: "<<k+1;
ob[k].showdata();
}
getch();
}

Program 7: Write a menu driven program to:


1.Read and integer array
2.Sort it using bubble sort
3.Search an element using binary search
#include<iostream.h>
#include<conio.h>
int bsearch(int a[], int n, int num);
void main()
{
clrscr();
int a[80],n,no,i,pos;
cout<<"\n Enter the number of elements in the array.";
cin>>n;
cout<<"\n Enter elements (IN ASCENDING ORDER)";
for(i=0;i<n;i++)
{
cin>>a[i];
}
cout<<"\n Now enter the numbers to be searched for:";
cin>>no;
pos=bsearch(a,n,no);
if(pos==-1)
cout<<"\n\t Number not found||";
else
cout<<"\n\t Element was found at position="<<pos+1;
getch();
}

int bsearch(int a[], int n, int num)


{
int beg=0,end=n-1,mid;
while(beg<=end);
{
mid=(beg+end)/2;
if(num==a[mid])
return(mid);
else
if(num>a[mid])
beg=mid+1;
else
end=mid-1;
}
return(-1);
}

Program 8: Write a menu driven program to:


1. Create a text file
2. Read the file and display uppercase vowels, lowercase vowels, uppercase
consonants and lowercase consonants.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#include<process.h>
void main()
{
clrscr();
int n,j;
fstream ofile,afile;
char str[100];
char ch,ch1;
do
{
cout<<"\n\t1.Create Text File\n\t2.Read and display\n\t";
cout<<"3.Exit\n";
cout<<"Enter your choice\n";
cin>>ch;
switch(ch)
{ case '1' :
ofile.open("smp.txt",ios::out);
cout<<"\nEnter The Text \n";
gets(str);
ofile<<str;
ofile.close();
break;
case '2' :
char tmp1;
afile.open("smp.txt",ios::in);
while(!afile.eof())
{
afile.get(tmp1);
if(isalpha(tmp1))
{
if (islower(tmp1))
{
if (tmp1=='a'||tmp1=='e'||tmp1=='i'||tmp1=='o'||tmp1=='u')
cout<< "\n Lower case vowel "<<tmp1;
else
cout<<"\n Lower case consonants "<<tmp1;
}
if (isupper(tmp1))
{ if (tmp1=='A'||tmp1=='E'||tmp1=='I'||tmp1=='O'||tmp1=='U')
cout<< "\n Upper case vowel "<<tmp1;
else
cout<<"\n Upper case consonants "<<tmp1;
} } }
afile.close();
break;
case '3':
exit(0);
}
cout<<"\nDo you wish to continue ";
cin>>ch1;
}
while(ch1=='Y'||ch1=='y');
getch();
}
Program 9: Create a link list of hospital registrations of patients. Perform
addition of new patients at the end of the list. Allow update based on
registration number.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdlib.h>

// define maximum number of patients in a queue


#define MAXPATIENTS 100

// define structure for patient data


struct patient
{
char FirstName[50];
char LastName[50];
char regisno[20];
char ID[10];
};
// define class for queue
class queue
{
public:
queue (void);
int AddPatientAtEnd (patient p);
int AddPatientAtBeginning (patient p);
int RemoveDeadPatient (patient * p);
patient GetNextPatient (void);
int RemovePatient (patient * p);
void OutputList (void);
char DepartmentName[50];
private:
int NumberOfPatients;
patient List[MAXPATIENTS];
};
// declare member functions for queue
queue::queue ()
{
// constructor
NumberOfPatients = 0;
}
int queue::AddPatientAtEnd (patient p)
{
// adds a normal patient to the end of the queue.
// returns 1 if successful, 0 if queue is full.
if (NumberOfPatients >= MAXPATIENTS)
{
// queue is full
return 0;
}
// put in new patient
else
List[NumberOfPatients] = p; NumberOfPatients++;
return 1;
}

int queue::AddPatientAtBeginning(patient p)
{
// adds a critically ill patient to the beginning of the queue.
// returns 1 if successful, 0 if queue is full.
int i;
if (NumberOfPatients >= MAXPATIENTS)
{
// queue is full
return 0;
}
// move all patients one position back in queue
for (i = NumberOfPatients-1; i >= 0; i--)
{
List[i+1] = List[i];
}
// put in new patient
List[0] = p; NumberOfPatients++;
return 1;
}
patient queue::GetNextPatient (void)
{
// gets the patient that is first in the queue.
// returns patient with no ID if queue is empty

int i; patient p;
if (NumberOfPatients == 0) {
// queue is empty
strcpy(p.ID,"");
return p;}
// get first patient
p = List[0];
// move all remaining patients one position forward in queue
NumberOfPatients--;
for (i=0; i<NumberOfPatients; i++)
{
List[i] = List[i+1];
}
// return patient
return p;
}
int queue::RemoveDeadPatient (patient * p)
{
// removes a patient from queue.
// returns 1 if successful, 0 if patient not found
int i, j, found = 0;
// search for patient
for (i=0; i<NumberOfPatients; i++)
{
if (stricmp(List[i].ID, p->ID) == 0)
{
// patient found in queue
*p = List[i]; found = 1;
// move all following patients one position forward in queue
NumberOfPatients--;
for (j=i; j<NumberOfPatients; j++)
{
List[j] = List[j+1];
}
}
}
return found;
}
void queue::OutputList (void)
{
// lists entire queue on screen
int i;
if (NumberOfPatients == 0)
{
cout << "\nQueue is empty";
}
else
{
for (i=0; i<NumberOfPatients; i++)
{
cout << " \n" << List[i].FirstName;
cout << " \n" << List[i].LastName;
cout << " \n" << List[i].ID;
}
}
}

// declare functions used by main:

patient InputPatient (void)


{
// this function asks user for patient data.
patient p;
cout << "\n\nPlease enter data for new patient First name: ";
cin.getline(p.FirstName, sizeof(p.FirstName));
cout << "Last name: ";
cin.getline(p.LastName, sizeof(p.LastName));
cout << "Social security number: ";
cin.getline(p.ID, sizeof(p.ID));
// check if data valid
if (p.FirstName[0]==0 || p.LastName[0]==0 || p.ID[0]==0)
{
// rejected
strcpy(p.ID,"");
cout << "\n\n\tError: Data not valid. Operation cancelled.";
getch();
}
return p;
}
void OutputPatient (patient * p)
{
// this function outputs patient data to the screen
if (p == NULL || p->ID[0]==0)
{
cout <<"\nNo patient";
return;
}
else
cout << "\nPatient data:";
cout << "\nFirst name: " << p->FirstName;
cout << "\nLast name: " << p->LastName;
cout << "\nSocial security number: " << p->ID;
};
int ReadNumber()
{
// this function reads an integer number from the keyboard.
// it is used because input with cin >> doesn't work properly!
char buffer[20];
cin.getline(buffer, sizeof(buffer));
return atoi(buffer);
}
void DepartmentMenu (queue * q)
{
// this function defines the user interface with menu for one department
int choice = 0, success; patient p;
while (choice != 6)
{
// clear screen
clrscr();
// print menu
cout << "\n\t\tWelcome to department: " << q->DepartmentName;
cout << "\nPlease enter your choice:";
cout << "\n1: Add normal patient";
cout << "\n2: Add critically ill patient";
cout << "\n3: Take out patient for operation";
cout << "\n4: Remove dead patient from queue";
cout << "\n5: List queue";
cout << "\n6: Change department or exit";
// get user choice
choice = ReadNumber();
// do indicated action
switch (choice)
{
case 1: // Add normal patient
p = InputPatient();
if (p.ID[0])
{
success = q->AddPatientAtEnd(p);
clrscr();
if (success)
{
cout << "\n\n\tPatient added:";
}
else
{
// error
cout << "\n\n\tError: The queue is full. Cannot add patient:";
}
OutputPatient(&p);
cout << "\nPress any key";
getch();
}
break;
case 2: // Add critically ill patient
p = InputPatient();
if (p.ID[0])
{
success = q->AddPatientAtBeginning(p);
clrscr();
if (success)
{
cout << "\n\n\tPatient added:";
}
else
{
// error
cout << "\n\n\tError: The queue is full. Cannot add patient:";
}
OutputPatient(&p);
cout << "\nPress any key";
getch();
}
break;

case 3: // Take out patient for operation


p = q->GetNextPatient();
clrscr();
if (p.ID[0])
{
cout << "\nPatient to operate:";
OutputPatient(&p);}
else
{
cout << "\n\n\tThere is no patient to operate.";
}
cout << "\nPress any key";
getch();
break;
case 4: // Remove dead patient from queue
p = InputPatient();
if (p.ID[0])
{
success = q->RemoveDeadPatient(&p);
clrscr();
if (success)
{
cout << "\n\n\tPatient removed:";
}
else
{
// error
cout << "\n\n\tError: Cannot find patient:";
}
OutputPatient(&p);
cout << "\nPress any key";
getch();
}
break;
case 5: // List queue
clrscr();
q->OutputList();
cout << "\nPress any key";
getch();
break;
}
}
}
// main function defining queues and main menu
void main ()
{
int i, MenuChoice = 0;
// define three queues
queue departments[3];
// set department names
strcpy (departments[0].DepartmentName, "Heart clinic");
strcpy (departments[1].DepartmentName, "Lung clinic");
strcpy (departments[2].DepartmentName, "Plastic surgery");
while (MenuChoice != 4)
{
// clear screen
clrscr();
// print menu
cout << "\t\tWelcome to Software City Hospital";
cout << "\nPlease enter your choice:";
for (i = 0; i < 3; i++)
{
// write menu item for department i
cout << " \n" << (i+1) << ": " << departments[i].DepartmentName;
}
cout << "\n4: Exit";
// get user choice
MenuChoice = ReadNumber();
// is it a department name?
if (MenuChoice >= 1 && MenuChoice <= 3)
{
// call submenu for department
// (using pointer arithmetics here:)
DepartmentMenu (departments + (MenuChoice-1));
}
}
}
Program 10: Create a link stack and perform push & pop operation.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

// Declares a stack structure

struct node
{
int year;
int houseno;
node *link;
};

// Function prototype declaration for add stack, delete stack, and show stack

node *push(node *top, int val, int thouseno); // Add stack


node *pop(node *top); // Delete stack
void show_Stack(node *top); // Show stack

// Main programming logic

void main()
{
node *top;
int tyear, thouseno, choice;
char opt = 'Y'; // To continue the do loop in case
top = NULL; // Initialization of Stack
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "“\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above ";
cin >> choice;
switch (choice)
{

case 1:
do
{
cout << "Enter the year of birth: ";
cin >> tyear;
cout << "Enter house number : ";
cin >> thouseno;
top = push(top, tyear, thouseno);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 2:
opt = 'Y'; // Initialize for the second loop
do
{
top = pop(top);
if (tyear != -1)
cout << "Value deleted from Stack is " << tyear;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 3:
show_Stack(top);
break;

case 4:
exit(0);
}
}
while (choice != 4);
}

// Function body for adds stack elements

node *push(node *top, int val, int thouseno)


{
node *temp;
temp = new node;
temp->year = val;
temp->houseno = thouseno;
temp->link = NULL;
if(top ==NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
return(top);
}

// Function body for delete stack elements

node *pop(node *top)


{
node *temp;
int thouseno, tyear;
clrscr();
if (top == NULL )
{
cout << "Stack Empty ";
tyear = -1;
}
else
{
temp = top;
top = top->link;
tyear = temp->year;
thouseno = temp->houseno;
temp->link = NULL;
cout << "\n\tPopped year of birth is : " << temp->year;
cout << "\n\tPopped house number is : " << temp->houseno;
delete temp;
}
return (top);
}

// Function body for show stack elements

void show_Stack(node *top)


{
node *temp;
temp = top;
clrscr();
cout << "The values are \n";
while (temp != NULL)
{
cout << "\n" << temp->year << "\t" << temp->houseno;
temp = temp->link;
}
}
Program 11: Create a link queue and perform addition and deletion
operations.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

// Declares a queue structure

struct node
{
int Eno;
float Salary;
node *link;
};

// Functions prototype to add queue, delete queue and show queue

node *add_Q(node *rear, int val,float val1); // Add queue


node *del_Q(node *front, int &val, float &val1);// Delete queue
void show_Q(node *front); // Show queue

// Main programming logic

void main()
{
node *front, *rear;
int val;
float val1;
int choice;
char opt = 'Y'; // To continue the do loop in case
front = rear = NULL; // Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;

switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the queue ";
cin >> val;
cin >> val1;
rear = add_Q(rear, val,val1);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 2:
opt = 'Y'; // Initialize for the second loop
do
{
front = del_Q(front, val, val1);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 3:
show_Q(front);
break;

case 4:
exit(0);
}
}
while (choice != 4);
}

// Function body to add queue elements

node *add_Q(node *rear, int val, float val1)


{
node *temp;
temp = new node;
temp->Eno = val;
temp->Salary = val1;
temp->link = NULL;
rear->link = temp;
rear = temp;
return (rear);
}

// Function body to delete queue elements

node *del_Q(node *front, int &val, float &val1)


{
node *temp;
clrscr();
if (front == NULL)
{
cout << "Queue Empty ";
val = -1;
}
else
{
temp = front;
front = front->link;
val = temp->Eno;
val1 = temp->Salary;
temp->link = NULL;
delete temp;
}
return (front);
}

// Function body to show queue elements

void show_Q(node *front)


{
node *temp;
temp = front;
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\nENO : "<< temp->Eno;
cout <<"\nSalary : "<<temp->Salary;
temp = temp->link;
}
Program 12: Declare a class containing name, address and telephone
number. Write a menu driven program to:
1. Append record in a file
2. Display name and address for a given telephone number
#include <fstream.h>
#include <conio.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>

class telephone
{
char name[30];
char address[50];
double tno;
public :
void input()
{
cout<<"\n Enter the name ";
gets(name);
cout << "\n Enter address ";
gets(address);
cout<<"\n Enter the telephone number ";
cin>>tno;
}
void show()
{
cout << "\n Name "<<name;
cout << "\n Address "<<address;
}
double rt_tno()
{
return tno;
}
}tele;

// Function to append the records in file

void append()
{
ofstream tfile;
telephone tele;
tfile.open("tele.dat", ios :: app);
int n,i;
cout<< "Enter how many customers ";
cin>>n;
for (i =0; i<n ;i++)
{
tele.input();
tfile.write((char *)& tele,sizeof(tele));
}
tfile.close();
}

// Function to search a record in the file

void display()
{
ifstream tfile;
tfile.open("tele.dat",ios :: binary);
int no,flag;
flag = 0;
cout<< "\n Enter telephone number to be searched ";
cin>>no;
while(tfile)
{
tfile.read((char *)&tele , sizeof(tele));
if(!tfile)
break;
if (tele.rt_tno() == no)
{
tele.show();
flag = 1;
}
}
if (flag == 0)
cout<< "\n Record does not exist ";
}

void main()
{
clrscr();
int ch;
cout << " 1. For append record ";
cout <<"\n 2. For search ";
cout << "\n 3. For exit";
cin >> ch;
switch (ch)
{
case 1:
append();
break;
case 2:
display();
break;
case 3:
exit(0);
}
getch();
}
Program 13: Declare a class having S.No. , SName , Fees and required
functions. Write a menu driven program to:
1. Append record in a file
2. Delete the record a given S. No.
#include<fstream.h>
#include<stdio.h>
#include<conio.h>
#include<process.h>
class Students
{
char sname[30];
int sno;
float fees;
public:
void input()
{
cout<<"\nEnter Student Name: ";
gets(sname);
cout<<"Enter Roll No.: ";
cin>>sno;
cout<<"Enter Fees: ";
cin>>fees;
}

void display()
{
cout<<"\nStudent Name: "<<sname<<"\tRoll No.: "<<sno<<"\tFees: "<<fees<<"\t";
}
int getsno()
{
return sno;
}
};

void main()
{
clrscr();
students s;
int n,i,j;
fstream ofile,afile;
char ch,ch1;
do
{
cout<<"\n\t1.Add records\n\t2.Search Records\n\t3.Delete Records\n\t4.Exit";
cin>>ch;
switch(ch)
{
case '1' :
ofile.open("std.dat",ios::out|ios::binary);
cout<<"\nEnter no. of records to be Entered: ";
cin>>n;
for(i=0;i<n;i++)
{
s.input();
ofile.write((char*)&s,sizeof(students));
}
ofile.close();
break;

case '2' :
cout<<"\nEnter Roll No. to be searched: ";
int sn,flag=0;
cin>>sn;
afile.open("std.dat",ios::in);
while(afile)
{
afile.read((char *)&s,sizeof(students));
if(!afile)
break;
if (sn==s.getsno())
{
s.display();
flag=1;
break;
}
}
if(flag==0)
cout<<"\n No record Found";
afile.close();
break;

case '3' :
cout<<"\nEnter Roll No. to be Deleted ";
int sn1,flag1=0;
cin>>sn1;
afile.open("std.dat",ios::in|ios::binary);
ofile.open("tmpstd.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(students));
if(!afile)
break;
if (sn1==s.getsno())
{
flag1=1;
}
else
{
ofile.write((char *)&s,sizeof(students));
}
}
if(flag1==0)
cout<<"\n No record Found";
afile.close();
ofile.close();

afile.open("tmpstd.dat",ios::in|ios::binary);
ofile.open("std.dat",ios::out|ios::binary);
while(afile)
{
afile.read((char *)&s,sizeof(students));
ofile.write((char *)&s,sizeof(students));
}
afile.close();
ofile.close();
break;

case '4' :
exit(0);
}
cout<<"\n\t DO U want to continue ";
cin>>ch1;
}
while(ch1=='Y'||ch1=='y');
getch();
}

Program 14: Each node of a queue contains ENo., salary, pointer field.
Front is the first node and rear is the last node. Write a menu driven program
to:
1. Add elements in the queue
2. Delete elements from queue
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

// Declares a queue structure

struct node
{
int Eno;
float Salary;
node *link;
};

// Functions prototype to add queue, delete queue, and show queue

node *add_Q(node *rear, int val,float val1); // Add queue


node *del_Q(node *front, int &val, float &val1);// Delete queue
void show_Q(node *front); // Show queue

// Main programming logic

void main()
{
node *front, *rear;
int val;
float val1;
int choice;
char opt = 'Y'; // To continue the do loop in case
front = rear = NULL; // Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the queue ";
cin >> val;
cin >> val1;
rear = add_Q(rear, val,val1);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 2:
opt = 'Y'; // Initialize for the second loop
do
{
front = del_Q(front, val, val1);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more element <Y/N>? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 3:
show_Q(front);
break;

case 4:
exit(0);
}
}
while (choice != 4);
}

// Function body to add queue elements

node *add_Q(node *rear, int val, float val1)


{
node *temp;
temp = new node;
temp->Eno = val;
temp->Salary = val1;
temp->link = NULL;
rear->link = temp;
rear = temp;
return (rear);
}

// Function body to delete queue elements

node *del_Q(node *front, int &val, float &val1)


{
node *temp;
clrscr();
if (front == NULL)
{
cout << "Queue Empty ";
val = -1;
}
else
{
temp = front;
front = front->link;
val = temp->Eno;
val1 = temp->Salary;
temp->link = NULL;
delete temp;
}
return (front);
}

// Function body to show queue elements

void show_Q(node *front)


{
node *temp;
temp = front;
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\nENO : "<< temp->Eno;
cout <<"\nSalary : "<<temp->Salary;
temp = temp->link;
}

Program 15: Each node of a stack contains Roll no., Age, pointer fields. Top
is the first node of the stack. Write a menu driven program to:
1. Push an element
2. Pop an element
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>

// Declares a stack structure

struct node
{
int roll;
int age;
node *link;
};

// Function prototype declaration for add stack, delete stack, and show stack

node *push(node *top, int val, int tage); // Add stack


node *pop(node *top); // Delete stack
void show_Stack(node *top); // Show stack

// Main programming logic

void main()
{
node *top;
int troll, tage, choice;
char opt = 'Y'; // To continue the do loop in case
top = NULL; // Initialization of Stack
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "“\n\t4. Exit from Menu";
cout << "\n\nEnter your choice from above ";
cin >> choice;
switch (choice)
{

case 1:
do
{
cout << "Enter the roll no. : ";
cin >> troll;
cout << "Enter age : ";
cin >> tage;
top = push(top, troll, tage);
cout << "\nDo you want to add more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
top = pop(top);
if (troll != -1)
cout << "Value deleted from Stack is " << troll;
cout << "\nDo you want to delete more elements <Y/N> ? ";
cin >> opt;
}
while (toupper(opt) == 'Y');
break;

case 3:
show_Stack(top);
break;

case 4:
exit(0);
}
}
while (choice != 4);
}

// Function body for adds stack elements

node *push(node *top, int val, int tage)


{
node *temp;
temp = new node;
temp->roll = val;
temp->age = tage;
temp->link = NULL;
if(top ==NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
return(top);
}

// Function body for delete stack elements

node *pop(node *top)


{
node *temp;
int tage, troll;
clrscr();
if (top == NULL )
{
cout << "Stack Empty ";
troll = -1;
}
else
{
temp = top;
top = top->link;
troll = temp->roll;
tage = temp->age;
temp->link = NULL;
cout << "\n\tPopped Roll Number is : " << temp->roll;
cout << "\n\tPopped Age is : " << temp->age;
delete temp;
}
return (top);
}

// Function body for show stack elements

void show_Stack(node *top)


{
node *temp;
temp = top;
clrscr();
cout << "The values are \n";
while (temp != NULL)
{
cout << "\n" << temp->roll << "\t" << temp->age;
temp = temp->link;
}
}

Program 16: WAP which accepts a 2-D array of integers and its size as
arguments and display elements which are exactly 2-digit number.
#include<iostream.h>
#include<conio.h>

void twodigit(int a[10][10],int m,int n)


{
int i,j,flag=0;
cout<<"The two digit Numbers are: ";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
{
if((a[i][j]>=10)&&(a[i][j]<=99))
{
cout<<a[i][j]<<endl;
flag=1;
}
}
if(flag==0)
cout<<"None";
}

void main()
{
clrscr();
int a[10][10],i,j,m,n;
cout<<"\nEnter no. of rows:\n";
cin>>m;
cout<<"\nEnter the no. of columns:\n";
cin>>n;
cout<<"\nEnter the Elements of the array:\n";
for(i=0;i<m;i++)
for(j=0;j<n;j++)
cin>>a[i][j];
twodigit(a,m,n);
getch();
}

Program 17: Write a menu driven program to perform the following


operations on a string without using inbuilt function:
1. Find length of a string
2. Compare 2 strings
3. Concatenate 2 strings
#include<iostream.h>
#include<conio.h>
#include<stdio.h>
int mystrlen(char st[]);
int mystrcmp(char st1[], char st2[]);
void mystrcat(char st1[], char st2[]);

void main()
{
clrscr();
char str1[500],str2[500];
cout<<"\n\n1. Length of string"<<endl;
cout<<"2.Compare two strings"<<endl;
cout<<"3.Concatenate two strings"<<endl;
char ch;
cout<<"\n\nEnter your choice:";
cin>>ch;

switch(ch)
{
case '1':cout<<"\n\nEnter a string-";
gets(str1);
cout<<"\nLength of string:"<<mystrlen(str1)<<endl;
break;
case '2':cout<<"\n\nEnter string 1-";
gets(str1);
cout<<"\nEnter string 2-";
gets(str2);
if(mystrcmp(str1,str2))
{
cout<<"\nStrings do not match"<<endl;
} else
{
cout<<"\nStrings match"<<endl;
} break;

case '3':cout<<"\n\nEnter string 1-";


gets(str1);
cout<<"\nEnter string 2-";
gets(str2);
cout<<"\nConcatenated string-";
mystrcat(str1,str2);
cout<<"\n"<<str1;
break;

}
getch();
}
int mystrlen(char st[])
{
int I=0;
for(I=0;st[I]!='\0';I++);
return I;
}

int mystrcmp(char st[],char st2[])


{
int i,n;
for(i=0;st[i]|st2[i];i++)
{
n=st[i]-st2[i];
if(n!=0)
return(n);
}
return(0);
}

void mystrcat(char st[],char st2[])


{
int i,j;
for (i=0;st[i]!='\0';i++)
for(j=0;st2[j]!='\0';j++)
st[i++]=st2[j];
st[i]='\0';
}
Program 18: WAP to illustrate the working of a function overloading.
#include<iostream.h>
#include<conio.h>
#include<math.h>

float area(float radius)


{
return(3.14*radius*radius);
}

float area(float length, float breadth)


{
return(length*breadth);
}

float area(float side1, float side2, float side3)


{
float S=(side1+side2+side3)/2;
float ar=sqrt(S*(S-side1)*(S-side2)*(S-side3));
return(ar);
}
void main()
{
clrscr();
char ch;
float a,b,c,ar;
cout<<"\n\t\tAREA MENU";
cout<<"\n\t1.Circle";
cout<<"\n\t2.Rectangle";
cout<<"\n\t3.Triangle";
cout<<"\n\t4.Enter choice:";
cin>>ch;

switch(ch)
{
case '1': cout <<"\n\n Enter radius:";
cin>>a;
ar=area(a); break;

case '2': cout<<"\n\n Enter length and breadth:";


cin>>a>>b;
ar=area(a,b); break;

case '3': cout<<"\n\n Enter sides: ";


cin>>a>>b>>c;
ar=area(a,b,c); break;

default: cout<<"\n\nWrong choice!";


}
cout<<"\n\nArea= "<<ar;
getch();}
Program 19: Program to perform addition and subtraction of matrices.
#include<iostream.h>
#include<conio.h>
#include<conio.h>
#include<process.h>
#include<dos.h>
void add(int c[10][10]);
void sub(int c[10][10]);
void create();
int a[10][10],b[10][10],c[10][10],i,j,m,n,p,q;
void main()
{
clrscr();
int ch;
do{
cout<<"n\t MENU";
cout<<"\n 1.ADDITION:";
cout<<"\n 2.SUBTRACTION:";
cout<<"\n 0.EXIT:";
cout<<"\n Enter Your Choice:";
cin>>ch;
switch(ch)
{
case 1: add(c);
break;
case 2: sub(c);
break;
case 0:exit(0);
}
}while (ch!=0);
}
void add(int c[10][10])
{
create();
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]+b[i][j];
}
cout<<"\n Addition=";
for(i=0;i<m;i++)
{
//cout<<endl;
for(j=0;j<n;j++)
{
cout<<" "<<c[i][j];
}
}
}
void sub( int c[10][10])
{
create();
for(i=0;i<m;i++)
{
for(j=0;j<n;j++)
c[i][j]=a[i][j]-b[i][j];
}
cout<<"\n Subtraction=";
for(i=0;i<m;i++)
{
// cout<<endl;
for(j=0;j<n;j++)
{
cout<<" "<<c[i][j];
}
}
}
void create()
{
start:
cout<<"Enter The Number of Rows And Columns For Matrix A:";
cin>>m>>n;
cout<<"\n Enter The Number of Rows and Columns For Matrix B:";
cin>>p>>q;
if ((m==p)&&(n==q))
{
cout<<"\n Enter Matrix A:";
for(i=0;i<m;i++)
{
cout<<"\n Enter Elements For "<<i+1<<"Row:";
for(j=0;j<n;j++)
{cin>>a[i][j];
}
//cout<<endl;
}
cout<<"\n Enter Matrix B:";
for(i=0;i<p;i++)
{
cout<<"\n Enter Elements For "<< i+1<<"Row:";
for(j=0;j<q;j++)
{
cin>>b[i][j];
}
//cout<<endl;
}
clrscr();
cout<<"\n Matrix A:";
for(i=0; i<m;i++)
{
for(j=0;j<n;j++)
{
cout<<a[i][j]<<" ";
}
//cout<<endl;
}
cout<<"\n Matrix B:";
for(i=0;i<p;i++)
{
for(j=0;j<q;j++)
{cout<<b[i][j]<<" ";
}
//cout<<endl;
}
}
else
{
cout<<"Rows and Column must be equal!!\n";
goto start;
}
}

Program 20: A menu driven program to perform following operation on strings


without using built in functions.
a. Find the length of the string
b. Compare the strings
c. Concatenate two strings.
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<stdio.h>
void findlength()
{
char str[30];
int l=0;
cout<<"\nEnter the string=";
gets(str);
while(str[l]!='\0')
{
l++;
}
cout<<"\nLength of the given string is="<<l<<endl;
}
void compare()
{
char str1[30],str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\n Enter the string1=";
gets(str1);
while(str1[l1]!=0)
{
l1++;
}
cout<<"\nEnter the string2= ";
gets (str2);
while(str2[l2]!='\0')
{
l2++;
}
if(l2!=l1)
{
cout<<"\nString are not equal";
}
else
{
for(i=0;i<l1;i++)
{
if(str1[i]!=str2[i])
{
flag=1;
break;
}
}
if (flag==1)
{
cout<<"\nStrings are not equal ";
}
else
{
cout<<"\nStrings are equal";
}
}
}
void concat()
{
char str1[30],str2[30];
int l1=0,l2=0,i=0,flag=0;
cout<<"\nEnter the string 1=";
gets(str1);
while (str1[l1]!='\0')
{
l1++;
}
cout<<"Enter the string 2= )";
gets(str2);
while(str2[l2]!='\0')
{
l2++;
}
for(i=0;i<l2;i++)
{
str1[l1+i]=str2[l2];
}
str1[l1+l2]=0;
cout<<"\nThe concatenated string is=";
puts(str1);
}
void main()
{
clrscr();
cout<<"Enter your choice \n \t1.find length of string \n\t"
"2.compare two strings\n\t3.concatenate two strings \n\t4.exit \n";
char ch;
cin>>ch;
do
{
if(ch=='1')
findlength();
if(ch=='2')
compare();
if(ch=='3')
concat();
cout<<"Enter your choice \n\t1.find length of string\n\t"
"2.compare two strings \n\t3.concatenate two strings \n\t4.exit\n";
cin>>ch;
}
while(ch!='4');
getch();
}

You might also like