You are on page 1of 63

C++ RECORD II YEAR

2008-2009
SUJITH KUMAR

Copy rights reserved to RameshKumar.

KELTRON IT EDUCATION CENTRE


SPENCER
TRIVANDRUM
UNIVERSITY OF KERALA
Program 1:MATRIX MULTIPLICATION

Aim: Write a C++ program to multiply two matrices of order m x n.

Program:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

void main()

int A[4][4], B[4][4], C[4][4], i,j,k,n,m,p,q,sum=0;

clrscr();

cout<<"Enter the order of first matrix"<<endl;

cin>>m>>n;

cout<<"Enter the order of second matrix"<<endl;

cin>>p>>q;

if(n!=p)

cout<<"Matrix multiplication Not Possible";

getch();

exit(0);

cout<<"Enter First Matrix"<<endl;

for(i=0;i<m;i++)

for(j=0;j<n;j++)

cin>>A[i][j];

cout<<"Enter Second Matrix"<<endl;

for(i=0;i<p;i++)

2
for(j=0;j<q;j++)

cin>>B[i][j];

for(i=0;i<m;i++)

for(j=0;j<q;j++)

sum=0;

for(k=0;k<n;k++)

sum=sum+A[i][k]*B[k][j];

C[i][j]=sum;

cout<<"Resultant Matrix"<<endl;

for(i=0;i<m;i++)

for(j=0;j<q;j++)

cout<<" "<<C[i][j];

cout<<endl;

getch();

Out put 1:

Enter the order of first matrix

3 4

3
Enter the order of second matrix

4 3

Enter First Matrix

12 13 14 10 25 21 14 13 12 47 10 12

Enter Second Matrix

24 20 23 21 22 20 35 36 14 12 14 14

Resultant Matrix

1171 1170 872

1687 1648 1373

1769 1802 1524

Out put 2:

Enter the order of first matrix

3 2

Enter the order of second matrix

4 3

Matrix multiplication Not Possible

Program 2:COUNT – CHARACTERS,WORDS

Aim: Write a C++ program to to count characters, words and number of lines in a text.

Program:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

#include <string.h>

class textc

protected:

char text[100];

int nw;

int nc;

int nl;

public:

4
textc()

strcpy(text,"");

nw=0;

nc=0;

nl=0;

void gettext()

cout<<"Enter Text : ";

cin.getline(text,100);

void count()

int i,x;

x=strlen(text);

for(i=0;i<x;i++)

if(text[i]==' ')

nw++;

if(text[i+1]==' '||text[i+1]=='.'||text[i+1]==NULL)

nw--;

if(text[i]=='.'||text[i+1]==NULL)

nl++;

if(text[i+1]!=' ')

nw++;

5
nc=i;

void display()

cout<<"Characters -> "<<nc<<endl;

cout<<"words -> "<<nw<<endl;

cout<<"Lines -> "<<nl<<endl;

};

void main()

textc T;

clrscr();

T.gettext();

T.count();

T.display();

getch();

Out put:

Enter Text : hello world good morning.welcome to all of you.

Characters -> 47

words -> 9

Lines ->2

Program 3:ELECTRICITY BILL

Aim: Write a C++ program to calculate the electricity charge

Program:

#include <iostream.h>

#include <fstream.h>

#include <string.h>

6
#include <stdlib.h>

#include <conio.h>

class electricity

char Cnam[20];

int Cno;

int Pre_R;

int Cur_R;

float Rate;

public:

void getdata()

cout<<"Con.No : ";

cin>>Cno;

cout<<"Con Name : ";

cin>>Cnam;

cout<<"Previous Reading : ";

cin>>Pre_R;

cout<<"Current Reading : ";

cin>>Cur_R;

void display()

int Cunit=Cur_R - Pre_R;

gotoxy(15,15);

cout<<"Consumer No: "<<Cno;

gotoxy(45,15);

cout<<"Consumer Name: "<<Cnam;

gotoxy(15,17);

cout<<"pre:reading:"<<Pre_R;

gotoxy(15,19);

7
cout<<"cur:reading:"<<Cur_R;

gotoxy(15,21);

cout<<"Consumed Unit: "<<Cunit;

gotoxy(15,23);

cout<<"Amount : "<<Rate;

void calcu()

int Cunit=Cur_R - Pre_R;

if(Cunit<101)

Rate=Cunit*.8;

else if(Cunit<201)

Rate=80+(Cunit-100)*1;

else if(Cunit<301)

Rate=180+(Cunit-200)*2;

else if(Cunit<501)

Rate=380+(Cunit-300)*5;

else

Rate=1380+(Cunit-500)*9;

};

void main()

electricity E;

clrscr();

cout<<"Enter Billig Details"<<endl;

E.getdata();

E.calcu();

clrscr();

gotoxy(25,10);

8
cout<<"Electricity Billig";

gotoxy(24,12);

cout<<"********************";

E.display();

getch();

Out put:

Enter Billig Details

Con.No :S2456

Con Name : Sreekantan

Previous Reading : 10245

Current Reading : 10347

Electricity Billig

********************

Consumer No: S2456 Consumer Name: Sreekantan

pre:reading:10245

cur:reading:10347

Consumed Unit:102

Amount :82

Program 4:ITEM DETAILS USING CLASS

Aim: Write a C++ program to read a list containing item name, code, and cost and produce a three column output.
Program:

#include <iostream.h>

#include <iomanip.h>

#include <conio.h>

class List

char It_nam[20];

9
int It_code;

float It_price;

public:

void getitem()

cout<<"EnterItem Name : ";

cin>>It_nam;

cout<<"EnterItem Code : ";

cin>>It_code;

cout<<"EnterPrice/unit : ";

cin>>It_price;

void display()

cout<<endl<<setw(10)<<It_code<<setw(10)<<It_nam<<setw(10)<<It_price<<endl;

};

void main()

List L[20];

char ch='y';

int i=0,j=0;

clrscr();

cout<<"Enter Item Details "<<endl;

while(ch=='y')

L[i].getitem();

++i;

cout<<"Enter Another....?(y/n) : ";

cin>>ch;

10
clrscr();

gotoxy(15,5);

cout<<"Item Details ";

gotoxy(14,6);

cout<<"**************" <<endl;

cout<<setw(10)<<"Code"<<setw(10)<<"Item"<<setw(10)<<"Price"<<endl;

while(j<i)

L[j].display();

++j;

getch();

Out put:

Enter Item Details

EnterItem Name : LuxSoap

EnterItem Code :0123

EnterPrice/unit : 18.50

Enter Another....?(y/n) :y

Enter Item Details

EnterItem Name : Rasna

EnterItem Code : 1242

EnterPrice/unit :25.80

Enter Another....?(y/n) :n

Item Details

**************

Code Item Price

0123 LuxSoap 18.50

1242 Rasna 25.80

Program 5:SHOPPING LIST USING CLASS

11
Aim: Write a C++ program to prepare a shopping list using class with arrays as data members.

Program:

#include <iostream.h>

#include <iomanip.h>

#include <stdlib.h>

#include <conio.h>

class shopping

char item[20][10];

int it_no[20],it_qty[20];

float it_rate[20],total[20],sum;

int count;

public:

shopping()

count=0;

sum=0;

void getdata()

cout<<"Enter the item: ";

cin>>item[count];

cout<<"Enter Item Price : ";

cin>>it_rate[count];

cout<<"Enter quantity:";

cin>>it_qty[count];

total[count]=it_rate[count]*it_qty[count];

it_no[count]=count+1;

count++;

void display()

12
{

int y=6;

cout<<"Slno:"<<"\t"<<"Item"<<setw(25)<<"Price"<<setw(5)<<"qty"<<setw(10)<<"Total"<<endl;

cout<<"-----------------------------------------------------"<<endl;

for(int i=0;i<count;i++)

cout<<it_no[i]<<"\t"<<item[i];

gotoxy(23,y);

cout<<setw(14)<<it_rate[i]<<setw(5)<<it_qty[i]<<setw(10)<<total[i]<<endl;

sum=sum+total[i];

y++;

cout<<endl<<setw(17)<<"Grand Total : "<<sum<<endl;

void disp()

cout<<endl<<endl<<"\t\t"<<"* TOTAL AMOUNT YOU REQUIRED IS :"<<sum<< " *";

};

void main()

shopping S;

char ch='Y';

int x;

do

clrscr();

S.getdata();

S.display();

cout<<"Do u want to continue?(Y/N)";

13
cin>>ch;

}while(ch=='y'||ch=='Y');

S.disp();

getch();

Out put:

Enter the item: soap

Enter Item Price : 12.50

Enter quantity:3

Slno: Item Price qty Total

1 soap 12.5 03 37.5

Grand Total :37.5

Do u want to continue?(Y/N) y

Enter the item: Horlicks

Enter Item Price : 125

Enter quantity:1

Slno: Item Price qty Total

1 soap 12.5 03 37.5

2 Horlicks 125 01 125

Grand Total :162.5

Do u want to continue?(Y/N) n

* TOTAL AMOUNT YOU REQUIRED IS : 162.5 *

Program 6:CONSTRUCTORS IN A CLASS

Aim: Write a C++ program to find the interest of a FD using dynamic initialization of constructors.

Program:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

class FD

{
14
float principle;

float rate;

float days;

float intr;

public:

FD()

principle=0.0;

rate=0.0;

days=0;

intr=0.0;

FD(float pr, float r, int d)

principle=pr;

rate=r;

days=d;

intr=principle*(days/365)*rate/100;

void display()

cout<<endl<<"Principle : "<<principle<<endl;

cout<<"Rate : "<<rate<<"%"<<endl;

cout<<"Interest : "<<setprecision(3)<<intr<<endl;

};

void main()

FD F1;

float pr,rt;

15
char ch='y';

int dt;

clrscr();

do

cout<<setw(25)<<"FD Details"<<endl;

cout<<"Principle : ";

cin>>pr;

cout<<"Rate [%]: ";

cin>>rt;

cout<<"Period[days] : ";

cin>>dt;

F1=FD(pr,rt,dt);

F1.display();

cout<<endl<<"Do You want to Continue?(Y/N)";

cin>>ch;

}while (ch=='Y'||ch=='y');

Out put:

FD Details

Principle : 25000

Rate [%]: 4.5

Period[days] : 450

Principle : 25000

Rate : 4.5%

Interest :1386.986

Do You want to Continue?(Y/N)y

FD Details

Principle :50000

Rate [%]: 3

16
Period[days] : 365

Principle : 50000

Rate : 3%

Interest :1500

Do You want to Continue?(Y/N)n

Program 7:RANK LIST USING TWO CLASSES

Aim: Write a C++ program to create a rank list of students using two different classes.
Program:

#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

#include <string.h>

class student

protected:

char nam[20];

int M1,M2,M3,tot;

public:

student()

strcpy(nam,"");

M1=0;

M2=0;

M3=0;

tot=0;

void getstud()

cout<<setw(25)<<"Mark Details"<<endl;

cout<<"Student Name : ";

cin>>nam;

17
cout<<"Mark1 : ";

cin>>M1;

cout<<"Mark2 : ";

cin>>M2;

cout<<"Mark3 : ";

cin>>M3;

tot=M1+M2+M3;

};

class rank:public student

int rnk;

public:

void display()

static int rnk;

cout<<setw(10)<<++rnk;

cout<<setw(10)<<nam;

cout<<setw(10)<<tot<<endl;

friend void great( rank & x,rank & y);

};

void great( rank & x,rank & y)

rank T;

if(x.tot<y.tot)

T=x;

x=y;

y=T;

18
}

void main()

rank R[10],T;

char check='y';

int x=0;

clrscr();

for(int i=0;check=='y';i++)

R[i].getstud();

cout<<"Do u want to enter another [Y/N]: ";

cin>>check;

cout<<"Rank list"<<endl;

for(int j=0;j<i;j++)

for(int k=0;k<i-j-1;k++)

great(R[k],R[k+1]);

clrscr();

cout<<setw(25)<<"Rank List"<<endl;

cout<<setw(10)<<"Rank"<<setw(10)<<"Name"<<setw(10)<<"Total"<<endl;

for(j=0;j<i;j++)

R[j].display();

19
getch();

Out put:

Mark Details

Student Name : ramesh

Mark1 :48

Mark2 : 49

Mark3 :47

Do u want to enter another [Y/N]: y

Mark Details

Student Name : suresh

Mark1 :46

Mark2 : 45

Mark3 :45

Do u want to enter another [Y/N]: y

Mark Details

Student Name :victor

Mark1 :42

Mark2 : 43

Mark3 :46

Do u want to enter another [Y/N]: n

Rank Name Total

1 ramesh 144

2 suresh 136

3 victor 131

Program 8:DIFFERENT OPERATIONS ON STRING

Aim: Write a C++ program for different string manipulations - concatenation, copy, compare

20
Program:

#include <iostream.h>

#include <conio.h>

#include <string.h>

#include <iomanip.h>

void main()

char str1[20],str2[20];

clrscr();

cout<<" Enter String 1: ";

cin>>str1;

strcpy(str2,str1);

cout<<endl<<" String 1 is COPIED to string 2"<<endl;

cout<<endl<<"String 1 is :"<<str1<<endl;

cout<<endl<<" String 2 is : "<<str2<<endl;

cout<<endl<<" Enter Another String : ";

cin>>str2;

int x=strcmp(str1,str2);

if (x<0 || x>0)

cout<<endl<<"Strings you entered are not equal "<<endl;

else

cout<<endl<<" Both the Strings you entered are equal "<<endl;

strcat(str1,str2);

cout<<endl<<" CONCATINATED String : "<<str1<<endl;

getch();

Out put:

21
Enter String 1: keltron

String 1 is COPIED to string 2

String 1 is : keltron

String 2 is : keltron

Enter Another String : ITeducation

Strings you entered are not equal

CONCATINATED String : keltronITeducation

Program 9:SALARY LIST USING FRIEND FUNCTION

Aim: Write a C++ program to calculate the salary of different grade of employers in a company using friend
function.
Program:
/*DA 45% LIC 5%grade1:hra->10%, pf->15%grade2:hra->8%, pf->13%grade3:hra->7%, pf->11%
grade4:hra->6%, pf->10%*/
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
#include <iomanip.h>
#include <conio.h>
#define DA 0.45
class employee
{
char enam[20];
float ebasic;
int grade;
public:

void getdata()
{
cout<<setw(30)<<" Employee Details."<<endl;
cout<<"Enter emp Name : ";
cin>>enam;
cout<<"Enter grade[1-4] : ";
cin>>grade;
cout<<"Enter basic : ";
cin>>ebasic;
}
friend void calcu(employee E);

};
void calcu(employee emp)
{
float da,pf,lic,hra,gsal,nsal;
da=DA * emp.ebasic;
lic=.05*emp.ebasic;
if(emp.grade==1)
{
pf=.15*emp.ebasic;
hra=.1*emp.ebasic;

22
}
if(emp.grade==2)
{
pf=.13*emp.ebasic;
hra=.08*emp.ebasic;
}
if(emp.grade==3)
{
pf=.11*emp.ebasic;
hra=.07*emp.ebasic;
}

if(emp.grade==4)
{
pf=.10*emp.ebasic;
hra=.06*emp.ebasic;
}
gsal=emp.ebasic+da+hra;
nsal=gsal-pf-lic;
cout<<"Employee Name:"<<setw(5)<<emp.enam<<endl;
cout<<"Basic "<<setw(5)<<emp.ebasic<<endl;
cout<<"DA "<<setw(5)<<da<<endl;
cout<<"HRA "<<setw(5)<<hra<<endl;
cout<<"PF "<<setw(5)<<pf<<endl;
cout<<"LIC "<<setw(5)<<lic<<endl;
cout<<"Gross Salary "<<setw(5)<<gsal<<endl;
cout<<"Net Salary "<<setw(5)<<nsal<<endl;
}
void main()
{
employee E;
char x;
do
{

clrscr();
E.getdata();
calcu(E);
cout<<"Do u calculate another(y/n) -> ";
cin>>x;

}while(x=='y');
}

Out put:

Employee Details.

Enter emp Name :gowri

Enter grade[1-4] : 2

Enter basic :5000

Employee Name:gowri

Basic 5000

23
DA 2250

HRA 400

PF 650

LIC 250

Gross Salary 7650

Net Salary 6750

Do u calculate another(y/n) -> n

Program 10:MULTIPLE INHERITANCE

Aim: Write a C++ program to print a student report using multiple inheritances.
Program:
#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#include<string.h>

#include<iomanip.h>

class marks

protected:

int M1,M2,M3;

public:

marks()

M1=M2=M3=0;

void getmark();

};

class sports

protected:

int grace;

public:

24
sports()

grace=0;

void getgrace();

};

class student:public marks, public sports

int Rollno;

char name[20];

public:

student()

Rollno=0;

strcpy(name,"");

void getstudent();

void display();

};

void marks::getmark()

cout<<"Enter Marks"<<endl;

cin>>M1>>M2>>M3;

void sports::getgrace()

cout<<"Enter grace mark(sports) : ";

cin>>grace;

void student::getstudent()

25
{

cout<<setw(25)<<"Students Details "<<endl;

cout<<"Enter Rollno ";

cin>>Rollno;

cout<<"Enter Name ";

cin>>name;

void student::display()

int s=M1+M2+M3;

int Tot=s+grace;

cout<<endl<<Rollno<<"\t\t"<<name<<"\t\t\t"<<Tot<<endl;

void main()

student s[10];

int j,i=0;

char ch='y';

while(ch=='y')

clrscr();

s[i].getstudent();

s[i].getmark();

s[i].getgrace();

i++;

cout<<"Do u want to enter Another Record [y/n]";

cin>>ch;

cout<<endl<<"Students Records"<<endl;

cout<<endl<<"Roll No:\tName\t\t\tTotalmarks"<<endl;

for(j=0;j<i;j++)

26
{

s[j].display();

getch();

Out put:

Students Details

Enter Rollno :1245

Enter Name :sreeja

Enter Marks

45 46 43

Enter grace mark(sports) :25

Do u want to enter Another Record [y/n]: y

Students Details

Enter Rollno :1346

Enter Name :adithya

Enter Marks

42 43 41

Enter grace mark(sports) :25

Do u want to enter Another Record [y/n]: n

Students Records

Roll No: Name Totalmarks

1245 sreeja 159

1346 adithya 146

Program 11:TOWER OF HANOI

Aim: Write a C++ program to solve the problem of towers of Hanoi.


Program:

#include <iostream.h>

#include <conio.h>

27
#include <string.h>

void main()

clrscr();

int n;

void transfer(int,char,char,char);

cout<<"Enter no. of Discs: ";

cin>>n;

transfer(n,'A','C','B');

getch();

void transfer(int n, char from,char to,char tmp)

if(n>0)

transfer(n-1,from,tmp,to);

cout<<"Move disk"<<n<<"From"<<from<<"to"<<to<<endl;

transfer(n-1,tmp,to,from);

Out put:

Enter no. of Discs: 4

Move 1disk FromAtoB

Move 2disk FromAtoC

Move 1 disk FromBtoC

Move 3 disk FromAtoB

Move 1 disk FromCtoA

Move 2disk FromCtoB

Move 1disk FromAtoB

Move 4 disk FromAtoC

28
Move 1 disk FromBtoC

Move 2disk FromBtoA

Move 1disk FromCtoA

Move 3disk FromBtoC

Move1disk FromAtoB

Move 2disk FromAtoC

Move 1disk FromBtoC

Program 12:OPERATOR OVERLOADING

Aim: Write a C++ program to perform arithmetic operations on complex numbers using operator overloading.
Program:
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

class complex

float real;

float img;

public:

complex()

real=img=0;

complex operator +(complex);

complex operator -(complex);

complex operator *(complex);

complex operator /(complex);

void getdata()

cout<<"Real Part : ";

cin>>real;

29
cout<<"Imaginary Part : ";

cin>>img;

void display()

if(img>=0)

cout<<real<<"+"<<"i"<<img<<endl;

else

cout<<real<<"-"<<"i"<<(-1*img)<<endl;

};

complex complex::operator+(complex c)

complex tmp;

tmp.real=real+c.real;

tmp.img=img+c.img;

return(tmp);

complex complex::operator-(complex c)

complex tmp;

tmp.real=real-c.real;

tmp.img=img-c.img;

return(tmp);

complex complex::operator*(complex c)

complex tmp;

tmp.real=(real*c.real)-(img*c.img);

tmp.img=(img*c.real)+(real*c.img);

return(tmp);

30
}

complex complex::operator/(complex c)

complex tmp;

float ft;

ft=c.real*c.real+c.img*c.img;

tmp.real=(real*c.real+img*c.img)/ft;

tmp.img=(img*c.real-real*c.img)/ft;

return(tmp);

void main()

complex c1,c2,c3;

clrscr();

cout<<"Enter Complex no.1:"<<endl;

c1.getdata();

cout<<"Enter Complex no.2:"<<endl;

c2.getdata();

cout<<endl<<" Results "<<endl;

cout<<"Addition : ";

c3=c1+c2;

c3.display();

cout<<"Subtraction : ";

c3=c1-c2;

c3.display();

cout<<"Multiplication : ";

c3=c1*c2;

c3.display();

cout<<"Division : ";

c3=c1/c2;

31
c3.display();

getch();

Out put:

Enter Complex no.1:

Real Part : 16

Imaginary Part : 8

Enter Complex no.2:

Real Part : 12

Imaginary Part : 5

Results

Addition : 28 + i13

Subtraction : 4 + i3

Multiplication : 152 + i176

Division : 1.3727 + i0.0946

Program 13:OPERATOR OVERLOADING ON STRING

Aim: Write a C++ program to concatenate two strings using operator overloading.
Program:
#include <iostream.h>

#include <conio.h>

#include <string.h>

class string

char str[20];

public:

string()

strcpy(str,"");

string(char *ch)

32
strcpy(str,ch);

string operator +(string);

void display()

cout<<str<<endl;

};

string string::operator+(string ch)

string tmp;

strcpy(tmp.str,str);

strcat(tmp.str,ch.str);

return(tmp);

void main()

clrscr();

string ch3;

string ch1="GOOD";

string ch2=" MORNING";

cout<<"The Strings Are Concatinated using Operator Overloading"<<endl;

cout<<endl<<" Concatenated string is : ";

ch3=ch1+ch2;

ch3.display();

getch();

Out put:

The Strings Are Concatinated using Operator Overloading

Concatenated string is : GOOD MORNING

33
Program 14:VIRTUAL FUNCTION

Aim: Write a C++ program to display the details of books and CDs in a shop using virtual functions.
Program:
#include <iostream.h>

#include <conio.h>

#include <string.h>

#include <iomanip.h>

class media

protected:

char title[20];

public:

media()

strcpy(title,"");

void getmedia()

cout<<"Title : ";

cin>>title;

virtual void display()

{}

};

class book:public media

char author[20];

float price;

public:

34
book()

strcpy(author,"");

price=0;

void getbook()

getmedia();

cout<<"Author : ";

cin>>author;

cout<<"Price : ";

cin>>price;

void display()

cout<<"Title : "<<title<<endl;

cout<<"Author : "<<author<<endl;

cout<<"Price : "<<price<<endl<<endl;

};

class CD:public media

char director[20];

float price;

public:

CD()

strcpy(director,"");

price=0;

35
}

void getcd()

getmedia();

cout<<"Director : ";

cin>>director;

cout<<"Price : ";

cin>>price;

void display()

cout<<"Name : "<<title<<endl;

cout<<"Director : "<<director<<endl;

cout<<"Price : "<<price<<endl<<endl;

};

void main()

book B;

CD C;

clrscr();

cout<<"Enter Book Details"<<endl;

B.getbook();

cout<<"Enter CD Details"<<endl;

C.getcd();

media *M1= new media;

media *M2=new media;

M1 = &B;

M2 = &C;

cout<<setw(20)<<"MEDIA DETAILS"<<endl;

cout<<setw(15)<<"Book"<<endl;

36
M1->display();

cout<<setw(15)<<"CD Details"<<endl;

M2->display();

getch();

Out put:

Enter Book Details

Title :othello

Author : shakespere

Price :250

Enter CD Details

Title :IIhariharnagar

Director :lal

Price :100

MEDIA DETAILS

Book

Title :othello

Author : shakespere

Price :250

CD

Name : IIhariharnagar

Author : lal

Price:100

Program 15:IMPLEMENTATION OF STACK

Aim: Write a C++ program to perform push and pop operations on a stack.

Program:

#include<iostream.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 5

37
int stack[20];

class stck

int top;

public:

stck()

top=-1;

char push(int);

int pop();

int empty();

int overflow();

};

char stck::push(int st)

char ch;

if(overflow())

cout<<"Stack Overflow "<<st<<" cann't pushed"<<endl;

return 0;

stack[++top]=st;

cout<<"Do u want to push another(y/n)-";

cin>>ch;

return(ch);

int stck::pop()

if(empty())

38
{

cout<<"Stack Empty"<<endl;

return 0;

cout<<"Poped item : ";

cout<<stack[top--];

return 0;

int stck::overflow()

if(top==MAX-1)

return 1;

return 0;

int stck::empty()

if(top==-1)

return 1;

return 0;

void main()

stck s;

int n,x;

char ch='y';

clrscr();

while(1)

cout<<endl<<"\t"<<"MENU"<<endl;

cout<<"1. PUSH"<<endl;

cout<<"2. POP"<<endl;

39
cout<<"3. Exit"<<endl;

cout<<"Enter ur choice : ";

cin>>x;

switch(x)

case 1:

do

cout<<"Enter the number to push: ";

cin>>n;

ch=s.push(n);

}while(ch=='y');

break;

case 2:

s.pop();

break;

case 3:

exit(0);

break;

default:

cout<<"Invalied";

break;

Out put:

MENU

1. PUSH

2. POP

3. Exit

40
Enter ur choice : 1

Enter the number to push:24

Do u want to push another(y/n)-y

Enter the number to push:50

Do u want to push another(y/n)-n

MENU

1. PUSH

2. POP

3. Exit

Enter ur choice : 2

Poped item :50

MENU

1. PUSH

2. POP

3. Exit

Enter ur choice : 2

Poped item :24

MENU

1. PUSH

2. POP

3. Exit

Enter ur choice :2

Stack Empty

MENU

1. PUSH

2. POP

3. Exit

Enter ur choice : 3

Program 16:LINKED LIST

Aim: Write a C++ program to create a linked list and add and delete from it.
Program:

41
#include <iostream.h>

#include <conio.h>

#include <stdlib.h>

struct node

int data;

node * next;

}*p,*q,*head,*temp;

void main()

void create();

void insert();

void display();

void del();

clrscr();

while(1)

int choice;

cout<<" MENU"<<endl<<"1. Create"<<endl<<"2. Insert"<<endl<<"3. Delete"<<endl<<"4.


Exit"<<endl<<"Enter your choice : ";

cin>>choice;

switch(choice)

case 1:

create();

break;

case 2:

insert();

break;

case 3:

42
del();

break;

case 4:

exit(0);

break;

void display()

p=head;

cout<<endl<<" Data in List"<<endl;

while(p!=NULL)

cout<<p->data<<"-";

p=p->next;

cout<<endl;

void create()

node *p;

char ch;

do

temp=new node;

temp->next=NULL;

cout<<"Enter data : ";

cin>>temp->data;

if(head==NULL)

43
head=temp;

else

p->next=temp;

p=temp;

cout<<"Do u want to insert another(y/n) : ";

cin>>ch;

}while(ch=='y');

display();

void insert()

int choice, data1;

char ch='n';

cout<<" Where to insert"<<endl;

do

cout<<"1. First Node"<<endl<<"2. Last Node"<<endl<<"3. Insert Before"<<endl<<"4. Insert


After"<<endl<<"Enter your choice : ";

cin>>choice;

temp=new node;

temp->next=NULL;

cout<<"Enter Data to be insert : ";

cin>>temp->data;

switch(choice)

case 1:

temp->next=head;

head=temp;

break;

44
case 2:

p=head;

while(p->next!=NULL)

p=p->next;

p->next=temp;

break;

case 3:

cout<<"Current list"<<endl;

display();

cout<<"Before which Node : ";

cin>>data1;

p=head;

while(p!=NULL)

if(p->data==data1)

q->next=temp;

temp->next=p;

break;

else

q=p;

p=p->next;

break;

case 4:

cout<<"Current list"<<endl;;

45
display();

cout<<"After which Node : ";

cin>>data1;

p=head;

while(p!=NULL)

if(p->data==data1)

temp->next=p->next;

p->next=temp;

break;

else

p=p->next;

break;

cout<<"Changed Data"<<endl;

display();

cout<<"Do u insert another....?(y/n)";

cin>>ch;

}while(ch=='y');

void del()

int choice, data1;

char ch='n';

46
cout<<" Delete from Where : "<<endl;

do

cout<<"1. First Node"<<endl<<"2. Last Node"<<endl<<"3. Particular Node"<<endl<<"Enter your choice :


";;

cin>>choice;

switch(choice)

case 1:

head=head->next;

break;

case 2:

p=head;

while(p->next!=NULL)

q=p;

p=p->next;

q->next =NULL;

delete p;

break;

case 3:

cout<<"Current list";

display();

cout<<"Node to be deleted : ";

cin>>data1;

p=head;

while(p!=NULL)

if(p->data==data1)

47
{

q->next=p->next;

delete p;

break;

else

q=p;

p=p->next;

break;

cout<<"Changed Data"<<endl;

display();

cout<<"Do u Delete another....?(y/n)";

cin>>ch;

}while(ch=='y');

Out put:

MENU

1. Create

2. Insert

3. Delete

4. Exit

Enter your choice :1

Enter Data to be insert : 10

Do u insert another....?(y/n)y

Enter Data to be insert : 12

48
Do u insert another....?(y/n)n

Data in List

10 – 12 –

MENU

1. Create

2. Insert

3. Delete

4. Exit

Enter your choice :2

Where to insert

1. First Node

2. Last Node

3. Insert Before

4. Insert After

Enter your choice : 4

Enter Data to be insert : 9

Current list

Data in List

10 – 12 –

After which Node : 10

Changed Data

Data in List

10 – 9 -12 –

Do u insert another....?(y/n) n

MENU

1. Create

2. Insert

3. Delete

4. Exit

Enter your choice :3

Delete from Where :

49
1. First Node

2. Last Node

3. Particular Node

Enter your choice : 1

Changed Data

Data in List

9 -12 –

Do u Delete another....?(y/n) n

MENU

1. Create

2. Insert

3. Delete

4. Exit

Enter your choice : 4

Program 17:BUBBLE SORT

Aim: Write a C++ program to sort an array using bubble sort.


Program:
#include <iostream.h>

#include <conio.h>

#include <iomanip.h>

#include <string.h>

void main()

int n,i,*A;

void bsort(int, int*);

clrscr();

cout<<"Enter no:of Element-> ";

cin>>n;

A=new int[n];

cout<<"Enter Elements :"<<endl;

for(i=0;i<n;i++)

cin>>A[i];

50
bsort(n,A);

cout<<"Elements in Sorted order is"<<endl;

for(i=0;i<n;i++)

cout<<setw(10)<<A[i]<<endl;

getch();

void bsort(int n,int *A)

for(int j=0;j<n-1;j++)

for(int k=0;k<n-j-1;k++)

if(A[k]>A[k+1])

int tp=A[k];

A[k]=A[k+1];

A[k+1]=tp;

Out put:

Enter no:of Element-> 10

Enter Elements : 12 20 8 56

4 23 11 10

6 7

Elements in Sorted order is

51
8

10

11

12

20

23

56

Program 18:SEARCH IN A FILE

Aim: Write a C++ program to search the record in the file using name or phrases.
Program:

#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <string.h>

class student

char nam[20];

int age;

public:

student()

strcpy(nam,"");

age=0;

//void getstud();

int search(char *ch)

return(strcmp(ch,nam));

void display()

52
cout<<"Student Name : "<<nam<<endl;

cout<<"Age : "<<age<<endl;

void getstud()

cout<<"Student Name : ";

cin>>nam;

cout<<"Age : ";

cin>>age;

};

void main()

student S;

char ch[20],check='y';

int x=0;

fstream f;

f.open("student",ios::in|ios::out);

clrscr();

cout<<"Enter Data"<<endl;

while(check=='y')

S.getstud();

f.write((char*)&S, sizeof(S));

cout<<"Do u want to enter another : (y/n) ";

cin>>check;

f.seekg(0);

cout<<"Enter name to search"<<endl;

cin>>ch;

53
cout<<"Result"<<endl;

while(f.eof()==0)

f.read((char*)&S, sizeof(S));

x=S.search(ch);

if(x==0)

S.display();

break;

if(x!=0)

cout<<"Not Found"<<endl;

f.close();

getch();

Out put:

Enter Data

Student Name : sreebha

Age : 24

Do u want to enter another : (y/n)y

Enter Data

Student Name : prajitha

Age : 23

Do u want to enter another : (y/n)y

Enter Data

Student Name : arika

Age : 26

Do u want to enter another : (y/n)n

Enter name to search: prajitha

54
Result

Student Name :prajitha

Age : 23

Program 19:FILE- ADD/DELETE/UPDATE

Aim: Write a C++ program to add, update, and delete from a file.
Program:
#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <string.h>

#include <stdlib.h>

#include <iomanip.h>

#include <stdio.h>

class student

public:

char nam[20],place[20];

int age;

public:

void display()

cout<<setw(13)<<nam<<setw(10)<<age<<setw(10)<<place<<endl;

void getstud()

cout<<"Student Name : ";

cin>>nam;

cout<<"Age : ";

cin>>age;

cout<<"Place :";

cin>>place;

55
}

};

void main()

student S;

char ch[20],c;

int i,check,n,flg=0;

fstream f,f1;

int sz=sizeof(S);

clrscr();

while(1)

cout<<endl<<"Menu"<<endl;

cout<<"1. Add"<<endl<<"2. Update"<<endl<<"3. Delete"<<endl<<"4. Display"<<endl<<"5.


Exit"<<endl<<"Enter ur Choice -> ";

cin>>check;

switch(check)

case 1:

f.open("d:\student1.txt",ios::in|ios::out);

cout<<"Enter Data"<<endl;

f.seekp(0,ios::end);

S.getstud();

cin.get(c);

f.write((char*)&S, sizeof(S));

f.close();

break;

case 2:

int loc;

flg=0;

f1.open("e:\student1.txt",ios::in|ios::out|ios::ate);

56
f1.seekg(0,ios::beg);

f1.seekp(0,ios::beg);

cout<<"Enter which one modified(enter name)"<<endl;

cin>>ch;

while(f1.read((char*)&S, sizeof(S)))

if(strcmp(S.nam,ch)==0)

cout<<"Enter New Data"<<endl;

S.getstud();

cin.get(c);

n=f1.tellg();

loc=n/sz;

f1.seekp((loc-1)*sz);

f1.write((char*)&S, sizeof(S));

f1.close();

flg=1;

break;

if(flg==0)

cout<<"Can't Found"<<endl;

f1.close();

break;

case 3:

f1.open("d:\student1.txt",ios::in|ios::out);

f.open("d:\student.txt",ios::in|ios::out);

f1.seekg(0,ios::beg);

f.seekg(0,ios::beg);

57
flg=0;

cout<<"Enter which one delete(enter name)"<<endl;

cin>>ch;

while(f1.read((char*)&S, sizeof(S)))

if(strcmp(S.nam,ch)!=0)

f.write((char*)&S, sizeof(S));

if(strcmp(S.nam,ch)==0)

flg=1;

cin.get(c);

if(flg==1)

cout<<"Removed Successfully"<<endl;

else

cout<<"Not found...!!!"<<endl;

f.close();

f1.close();

remove("d:\student1.txt");

if(rename("d:\student.txt","d:\student1.txt")==0)

remove("d:\student.txt");

break;

case 4:

f.open("d:\student1.txt",ios::in);

i=0;

f.seekg(0);

cin.get(c);

cout<<setw(24)<<"Student Details"<<endl<<setw(15)<<"Student
Name"<<setw(10)<<"Age"<<setw(10)<<"Place"<<endl;

while(f.read((char*)&S, sizeof(S)))

S.display();

58
i++;

if(i==0)

cout<<"No Data....!!!!!"<<endl;

f.close();

break;

case 5:

exit(0);

break;

f.close();

Out put:

Menu

1. Add

2. Update

3. Delete

4. Display

5. Exit

Enter ur Choice -> 1

Enter Data

Student Name : ramesh

Age : 25

Place : pallichal

Menu

1. Add

2. Update

59
3. Delete

4. Display

5. Exit

Enter ur Choice -> 1

Enter Data

Student Name : sreejith

Age : 26

Place : ooruttambalam

Menu

1. Add

2. Update

3. Delete

4. Display

5. Exit

Enter ur Choice -> 2

Enter which one modified(enter name)

ramesh

Enter New Data

Student Name : ramesh

Age : 26

Place : trivandrum

Menu

1. Add

2. Update

3. Delete

4. Display

5. Exit

Enter ur Choice -> 4

Student Details

Student Name Age Place

Ramesh 26 trivandrum

60
Sreejith 26 ooruttambalam

Place : trivandrum

Menu

1. Add

2. Update

3. Delete

4. Display

5. Exit

Enter ur Choice -> 3

Enter which one delete(enter name)

suresh

Removed Successfully

Menu

1. Add

2. Update

3. Delete

4. Display

5. Exit

Enter ur Choice -> 5

Program 20:COMMAND LINE ARGUMENTS

Aim: Write a C++ program to create copy command (command line arguments).
Program:
#include <iostream.h>

#include <conio.h>

#include <fstream.h>

#include <string.h>

class student

char nam[20];

int age;

public:

student()

61
{

strcpy(nam,"");

age=0;

void getstud()

cout<<"Student Name : ";

cin>>nam;

cout<<"Age : ";

cin>>age;

void display()

cout<<"Student Name : "<<nam<<endl;

cout<<"Age : "<<age<<endl;

};

void main(int argc, char* argv[])

student S;

char ch[20],check='y';

fstream f1,f2;

f1.open(argv[1],ios::in|ios::out);

clrscr();

cout<<"Enter Data"<<endl;

while(check=='y')

S.getstud();

f1.write((char*)&S, sizeof(S));

cout<<"Do u want to enter another : ";

cin>>check;

62
}

f2.open(argv[2],ios::in|ios::out);

f1.seekg(0,ios::beg);

f2.seekp(0,ios::beg);

while(f1.read((char*)&S, sizeof(S)))

f2.write((char*)&S, sizeof(S));

cout<<"Content of file 2"<<endl;

f2.seekg(0,ios::beg);

while(f2.read((char*)&S, sizeof(S)))

S.display();

f1.close();

f2.close();

getch();

Thank GOD.

Thank Parents.

Thank you.

63