Sie sind auf Seite 1von 102

Student Name Amruta Jawahar Chaudhari

Roll No 17
Class MCA II
Academic Year 2019-20
Semester III
Subject T1-IT31:Advanced Data Structure and C++ Programming.

INDEX

Srno. Assignments Name Remark


1 Write a c++ program to find out given number is prime or not.

2 Write a c++ program to find out given number is palindrome or not.

3 Write a c++ program to find out given number is armstrong or not.

4 Write a c++ program to find out Fibonacci series for given number.

5 Write a c++ program to find out is factorial of given number .

6 Write a c++ program to find out the addition of two complex numbers.

7 Write a c++ program to perform the arithmetical operations.

8 Write a c++ program to find out given number is even or odd.

9 Write a c++ program to read ‘n’ number and print a number whos addition of digit is
maximum.

10 Write a c++ program to read m*n matrix and display transpose of matrix.

11 Write a c++ program to maintain record of no.of employees in a company. For that use
data members as :emp_id,emp_name,emp_basic,emp_da,emp_hra,sal,net_sal member
functions as:get_details(),find_net_sal(),show_emp_details().

12 Write a c++ program to from to maintain student record for that use data members
as:student_id,student_name,student_branch,sub1,sub2,sub3,avg,perc member function
as void getdata(),void putdata(),void calculate().

13 Write a c++ program to demonstrate private member function for that create class
reverse_no data member as interger number member function as void read(),as private
function public function as void show(),void reverse().

14 Write a c++ program for swapping of two numbers using call by value.

1
15 Write a c++ program for swapping of two numbers using call by reference.

16 Write a c++ program for swapping of two numbers using reference variable.

17 Write a c++ program to find out ASCII value of a character using reference variable.

18 Write a c++ program to calculate simple interest and compound interest using default
argument as rate of interest=0.15.

19 Write a c++ program to write a function power() to raise number m to power n using
function overloading.

20 Write a c++ program calculate area using function overloading.

21 Define a class to represent BankAccount include the members,name of depositor


,account number, type of account,balance amount and member functions are:
i)To assign initial value .
ii) To Deposit amount.
iii)To withdrow amount.
iv)Display amount using menu.

22 Write a c++ program to generate following


1
22
333
4444
55555
23 Write a c++ program to generate light bill according to given data
Unit Charges
0 to 100 3.50
101 to 200 5.50
201 to 300 7
More then 300 10
24 An election is contested by 5 candidates. The candidates are numbered from 1 to 5 and
voting is done by making the candidate number on the ballots paper.Write a program to
read ballots and count the votes for each candidate using an array.

25 Book details using Array of object


Data Member: char title[30],author[30],int price.
Member Function: getdata(void),putdata(void)

26 Employee Details using Array of object.

27 Write a class to represent a vector (a series of float values) include member functions to
perform the following tasks:
a) To create a vector.
b) To modify the value of given statement.
c) To multiply by a scalar value .
d) To display the vector in form(10,20,30….)

28 Use the constrctor overloading to demonstrate following Unary Operator over i) Unary
minus
ii) !
iii)Increment and decrement
2
iv) Assignment operator
29 Binary operator overloading
i) Addition and multiplication
ii) String compare and String concatenation.

30 Write a cpp program to Inherit a Student class from Person Class printing
the properties of the Student (Single Inheritance).
firstnam
gender Person lastnam

level Student collegenam

31 C++ program to create a programmer derived from employee


which is himself derived from person using Multilevel Inheritance

Person

Employee

Programmer

32 Write a cpp program to display petrol's data using Multiple Inheritance


from fuel and liquid.
Liquid Fuel

Petrol

33 Write a cpp program to Implement the following inheritance (Hybrid Inheritance)

Student

Sport Exam Test

Result

3
Write a cpp program to Implement the following inheritance (Hybrid Inheritance)

34 Write a cpp program to Implement the following inheritance (Hierachical Inheritance)


Student
num1
num2

Arts Engineering Medical

Computer Mechanical Electrical

35

Plus Arithmetic Minus

Sub
Result

36 Write a cpp program to handle divide by zero exception.

37 Write a cpp program for merge two files into third file and display third file.

38 Write a cpp program to read a file contents and replaced space with ‘$’ sign.

39 Write a c++ program to implement stack operations.

40 Write a cpp program to implement queue operations.

41 Write a cpp program to implement different linked list operations.

4
Q1.] Write a cpp program to check number is prime or not.?

#include<iostream.h>

#include<conio.h>

class Prime

public:

int n;

void getdata();

void check();

};

void Prime::getdata()

cout<<"\n"<<"Enter the number:-";

cin>>n;

void Prime::check()

int i=2,f=1;

while(i<=n/2)

if(n%i==0)

f=0;

i++;

else
5
{

f=1;

i++;

if(f==0)

cout<<n<<" is not prime number.";

else

cout<<n<<" is prime number.";

void main()

Prime obj;

clrscr();

obj.getdata();

obj.check();

getch();

/*OUTPUT:-

Enter the number:-5

5 is prime number.
6
Enter the number:-10

10 is not prime number.

Enter the number:-77

77 is prime number . */

Q2.] Write a cpp program to check number is palindrome or not.?

#include<iostream.h>

#include<conio.h>

class Palindrome

public:

int n;

void getdata();

void check(int);

};

void Palindrome::getdata()

cout<<"Enter the number:-";

cin>>n;

check(n);

void Palindrome::check(int n)

int num,r,sum=0;

num=n;

while(num>0)

r=num%10;
7
sum=sum*10+r;

num=num/10;

if(n==sum)

cout<<"Number is Palindrome";

else

cout<<"Number is not Palindrome";

void main()

Palindrome obj;

//clrscr();

obj.getdata();

getch();

/* OUTPUT:

Enter the number:-121

Number is Palindrome

Enter the number:-

142

Number is not Palindrome


8
*/

Q3.] Write a cpp program to check whether number is Armstrong or not.?

#include<iostream.h>

#include<conio.h>

class Armstrong

public:

int n;

void getdata();

void check(int);

};

void Armstrong::getdata()

cout<<"Enter the number:-";

cin>>n;

check(n);

void Armstrong::check(int n)

int r,num,sum=0;

num=n;

while(num>0)

r=num%10;

sum=sum+(r*r*r);
9
num=num/10;

if(sum==n)

cout<<"Number is Armstrong";

else

cout<<"Number is not Armstrong";

void main()

Armstrong obj;

clrscr();

obj.getdata();

getch();

/* OUTPUT:

Enter the number:-123

Number is not Armstrong

Enter the number:-153

Number is Armstrong

Enter the number:-583


10
Number is not Armstrong

*/

Q4.] Write a c++ program to print the Fibonacci series.?

#include<iostream.h>

#include<conio.h>

class Fibonacci

public:

int n,num;

void getdata();

void series(int,int);

};

void Fibonacci::getdata()

cout<<"Enter the number which u want start a series:-";

cin>>n;

cout<<"How many number series u want:-";

cin>>num;

series(n,num);

void Fibonacci::series(int n,int num)

int a,b,c,i;

a=0;

b=n;

cout<<"series is:"<<"\n";
11
// cout<<a<<"\n";

// cout<<b<<"\n";

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

c=a+b;

cout<<c<<"\n";

a=b;

b=c;

void main()

Fibonacci obj;

//clrscr();

obj.getdata();

getch();

/* Output:

Enter the number which u want start a series:-23

How many number series u want:-10

series is:

23

46

69

115
12
184

299

483

782

1265

2047

*/

Q5.] Write a cpp program to find the factorial of given number.?

#include<iostream.h>

#include<conio.h>

class Factorial

public:

int n;

void getdata();

void calculate(int);

};

void Factorial::getdata()

cout<<"Enter the number:-";

cin>>n;

calculate(n);

void Factorial::calculate(int n)

int fact=1,num;

num=n;
13
while(n>0)

fact=fact*n;

n--;

cout<<"Factorial of"<< num<<" is "<<fact;

void main()

Factorial obj;

//clrscr();

obj.getdata();

getch();

/*OUTPUT:-

C:\TURBOC3\BIN>TC

Enter the number:-5

Factorial of0is120

Enter the number:-6

Factorial of6 is 720

*/

Q6].WCPP to perform the addition of two complex number.

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

class Complex

public:

int real,imag;

void getdata();

};

void Complex::getdata()

Complex n1,n2,sum;

cout<<"Enter the real and imaginary part of complex no:"<<endl;

cin>>n1.real>>n1.imag;

cout<<"Enter the real and imaginary part if complex no:"<<endl;

cin>>n2.real>>n2.imag;

sum.real=n1.real+n2.real;

sum.imag=n1.imag+n2.imag;

if(sum.imag>0)

cout<<"sum of two complex no:"<<sum.real<<"+"<<sum.imag<<"i";

else

cout<<"sum of two complex no:"<<sum.real<<"-"<<sum.mag<<"i";

void main()
15
{

clrscr();

Complex c;

c.getdata();

getch();

7].WCPP to perfrom arithmetic operation.

#include<iostream.h>

#include<conio.h>

class Matrix

public:

int i,j,m,n,a[10][10],b[10][10],c[10][10];

void Add();

void Sub();

void Mul();

void Operation();

};

void Matrix::Add()

cout<<"How many rows you want to enter:";

cin>>m;

cout<<"How many columns you want to enter:";

cin>>n;

cout<<"enter the elements for first matris:"<<endl;

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

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

cin>>a[i][j];

cout<<endl;

cout<<"Enter the elements of second matrix:"<<endl;

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

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

cin>>b[i][j];

cout<<endl;

cout<<"Addition of Matrix:"<<endl;

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

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

c[i][j]=a[i][j]+b[i][j];

cout<<"\t"<<c[i][j];

cout<<endl;

17
void Matrix::Sub()

cout<<"How many rows you want to enter:";

cin>>m;

cout<<"How many columns you want to enter:";

cin>>n;

cout<<"enter the elements for first matris:"<<endl;

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

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

cin>>a[i][j];

cout<<endl;

cout<<"Enter the elements of second matrix:"<<endl;

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

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

cin>>b[i][j];

cout<<endl;

cout<<"Subtraction of Matrix:"<<endl;

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

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

c[i][j]=a[i][j]-b[i][j];

cout<<"\t"<<c[i][j];

cout<<endl;

void Matrix::Mul()

cout<<"How many rows you want to enter:";

cin>>m;

cout<<"How many columns you want to enter:";

cin>>n;

cout<<"enter the elements for first matris:"<<endl;

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

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

cin>>a[i][j];

cout<<endl;

cout<<"Enter the elements of second matrix:"<<endl;

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

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

cin>>b[i][j];

cout<<endl;

cout<<"Multiplication of Matrix:"<<endl;

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

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

c[i][j]=a[i][j]*b[i][j];

cout<<"\t"<<c[i][j];

cout<<endl;

void Matrix::Operation()

char flag='y';

int ch;

cout<<"Select the option:"<<endl;

cout<<"1.Addition"<<endl;

cout<<"2.subtraction"<<endl;

cout<<"3.Multiplication"<<endl;

cout<<"enter your choice:";

cin>>ch;

switch(ch)
20
{

case 1:

Add();

break;

case 2:

Sub();

break;

case 3:

Mul();

break;

default:

cout<<"Enter correct choice"<<endl;

cout<<"Again start Y/N :";

cin>>flag;

// Operation();

if(flag=='y')

Operation();

else

cout<<"no choice selected";

break;

21
}

void main()

Matrix m;

clrscr();

//m.Add();

//m.Sub();

// m.Mul();

m.Operation();

getch();

Output:

Q8].WACPP to fine out even and odd number.

#include<iostream.h>

#include<conio.h>

class Demo

public:

int num;

void getdata();

void check();

};

void Demo::getdata()

cout<<"Enter the number:";

cin>>num;
22
}

void Demo::check()

if(num%2==0)

cout<<"Number is even"<<"\n";

else

cout<<"Number is odd"<<"\n";

void main()

Demo obj;

// clrscr();

obj.getdata();

obj.check();

getch();

/*Output:

Enter the number:

Number is even

Enter the number:5

Number is odd

*/
23
Q9].WCCP to read ‘n’ number and print a number whose addition of digit is
maximum.

Q10].WCPP to read m*n matrix and display transpose matrix.

#include<iostream.h>

#include<conio.h>

class Matrix

int r,c,a[14][14];

public:

void getData();

void transpose();

};

void Matrix:: getData()

int i,j;

cout<<"Enter the number of rows and columns:-";

cin>>r>>c;

cout<<"Enter the element:-";

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

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

24
{

cin>>a[i][j];

cout<<"\n";

cout<<"Entered Matrix Is:-"<<"\n";

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

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

cout<<"\t"<<a[i][j];

cout<<"\n";

}//close getData()

void Matrix:: transpose()

int i,j;

cout<<"Transpose Of Matrix:-"<<"\n";

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

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

cout<<"\t"<<a[j][i];

cout<<"\n";

}
25
}

void main()

Matrix obj;

clrscr();

obj.getData();

obj.transpose();

getch();

/* OUTPUT:

Enter the number of rows and columns:-3

Enter the element:-

12

13

Entered Matrix Is:-

3 4 5
26
6 7 8

9 12 13

Transpose Of Matrix:-

3 6 9

4 7 12

5 8 13

*/

Q.11 write a c++ program to form to maintain student record for that use data
member as sid,name ,branch,sub1,sub2,sub3,average and member function as
void getdata(),putdata(),void calculate().

#include<iostream.h>

#include<conio.h>

class Que11

public:

int id;

char name[20],branch[25];

float sub1,sub2,sub3,avg,per;

void getdata();

void putdata();

void calculate();

};

void Que11::getdata()

cout<<"Enter Student details:"<<endl;

27
cout<<"Enter Student Id:";

cin>>id;

cout<<"Enter name:";

cin>>name;

cout<<"Enter Branch:";

cin>>branch;

cout<<"Enter Marks of sub1:";

cin>>sub1;

cout<<"enter Marks of sub2:";

cin>>sub2;

cout<<"Enter Marks of sub3:";

cin>>sub3;

cout<<endl;

void Que11::putdata()

cout<<"Student Details:"<<endl;

cout<<"Id:"<<id<<endl;

cout<<"Name:"<<name<<endl;

cout<<"Branch:"<<branch<<endl;

cout<<"Marks:"<<endl;

cout<<"sub1:"<<sub1<<endl;

cout<<"Sub2:"<<sub2<<endl;

cout<<"Sub3:"<<sub3<<endl;

cout<<"Average Percentage:"<<avg<<endl;

cout<<"Total Percentage:"<<per<<endl;

}
28
void Que11::calculate()

int total;

total=sub1+sub2+sub3;

avg=total/3;

per=total/3;

void main()

Que11 q;

clrscr();

q.getdata();

q.calculate();

q.putdata();

getch();

/* Output

Enter Student details:

Enter Student Id:1

Enter name:Kajal

Enter Branch:MCA

Enter Marks of sub1:85

enter Marks of sub2:98

Enter Marks of sub3:87

Student Details:
29
Id:1

Name:Kajal

Branch:MCA

Marks:

sub1:85

Sub2:98

Sub3:87

Average Percentage:90

Total Percentage:90

*/

Q.12 write a c++ program to form to maintain employees in a company.for that


use data member as:eid,name,basic,da,hra,net sal.member functions
as:getdetails(),find net_sal(),show details()?

#include<iostream.h>

#include<conio.h>

class Que12

public:

int id;

30
char name[20];

float basic,da,hra,net;

void getdata();

void show_emp_details();

void find_net_sal();

};

void Que12::getdata()

cout<<"Enter Employee Details:"<<endl;

cout<<"Enter id of Employee:";

cin>>id;

cout<<"Enter Name:";

cin>>name;

cout<<"Enter Basic Salary:";

cin>>basic;

cout<<"enter duration Allowence:";

cin>>da;

cout<<"Enter Home Rent Allowence:";

cin>>hra;

cout<<endl;

void Que12::show_emp_details()

cout<<"Employee Details:"<<endl;

cout<<"Id:"<<id<<endl;

cout<<"Name:"<<name<<endl;

cout<<"Basic salary:"<<basic<<endl;
31
cout<<"Duration Allowence:"<<da<<endl;

cout<<"Home Rent allowence:"<<hra<<endl;

cout<<"Net Salary:"<<net<<endl;

void Que12::find_net_sal()

net=basic+hra+da;

void main()

Que12 q;

clrscr();

q.getdata();

q.find_net_sal();

q.show_emp_details();

getch();

/* Output

Enter Student Details:

Enter id of student:1

Enter Name:kk

Enter Basic Salary:12000

enter duration Allowence:1200

Enter Home Rent Allowence:2000

Student Details:
32
Id:1

Name:kk

Basic salary:12000

Duration Allowence:1200

Home Rent allowence:2000

Net Salary:15200

*/

Q.13]Write a program to demonstrate private member function for that create


a class reverse no data member as integer no member function as void read(); as
private function public function as void show(),void reverse() method.

#include<iostream.h>

#include<conio.h>

class Que13

public:

int rev,r,n;

void read()

cout<<"Enter the no which you want to reverse:";

cin>>n;

reverse();

private:

33
void reverse()

rev=0;

while(n>0)

r=n % 10;

rev=rev*10+r;

n=n/10;

cout<<"Reverse no="<<rev<<endl;

};

void main()

Que13 q;

clrscr();

q.read();

getch();

/*

Output:

Enter the no which you want to reverse:458

Reverse no=854

*/

34
Q.14] Write a program for swapping two no. using call by value.

#include<iostream.h>

#include<conio.h>

class Que14

public:

int a,b;

void swap(int,int);

};

void Que14::swap(int x,int y)

int z;

z=x;

x=y;

y=z;

cout<<"After Swapping:"<<x<<"\t"<<y<<endl;

void main()

Que14 q;

int a,b;

clrscr();

cout<<"Enter first no:"<<endl;

cin>>a;

cout<<"Enter second no:"<<endl;

cin>>b;
35
cout<<"before swapping:"<<a<<"\t"<<b<<endl;

q.swap(a,b);

getch();

/* Output

Enter first no:

48

Enter second no:

87

before swapping:48 87

After Swapping:87 48

*/

Q.15]Write a program for swapping two no using call by reference.

#include<iostream.h>

#include<conio.h>

class Que15

public:

int a,b;

void swap(int *,int *);

};
36
void Que15::swap(int *x,int *y)

int z;

z=*x;

*x=*y;

*y=z;

cout<<"After swapping:"<<*x<<"\t"<<*y<<endl;

void main()

Que15 q;

clrscr();

int a,b;

cout<<"Enter first no:"<<endl;

cin>>a;

cout<<"Enter second no:"<<endl;

cin>>b;

cout<<"Before Swapping:"<<a<<"\t"<<b<<endl;

q.swap(&a,&b);

getch();

/* Output

Enter first no:

14

Enter second no:

54
37
Before Swapping:14 54

After swapping:54 14

*/

Q16.]Write a cpp program for swap two numbers using reference variable.?

#include<iostream.h>

#include<conio.h>

class Que16

public:

int a,b;

void swap(int &,int &);

};

void Que16::swap(int &x,int &y)

int z;

z=x;

x=y;

y=z;

cout<<"After swapping:"<<x<<"\t"<<y<<endl;

void main()

Que16 q;

clrscr();
38
int a,b;

cout<<"Enter first no:"<<endl;

cin>>a;

cout<<"Enter second no:"<<endl;

cin>>b;

cout<<"Before Swapping:"<<a<<"\t"<<b<<endl;

q.swap(a,b);

getch();

/* Output

Enter first no:

45

Enter second no:

96

Before Swapping:45 96

After swapping:96 45

*/

Q.17]Calculate simple interest and compound interest using default argument


as rate of interest=0.5.

#include<iostream.h>

#include<conio.h>

#include<math.h>

39
class interest

public:

void amt(float,float,int,int);

};

void interest::amt (float prin=5000,float rate=.05,int time=5,int years=7)

cout<<"Principal amount : "<<prin<<" ";

cout<<"Rate : "<<rate<<" ";

cout<<"Time : "<<time<<" ";

cout<<"Simple interest : "<<prin*rate*time<<endl;

cout<<"Compound interest :"<<prin*pow((1+rate/time),time*years)<<endl;

int main()

clrscr();

interest i;

i.amt(8000.0F);

i.amt(9000.0F,.08F);

i.amt(9000.0F,.09F,8,6);

getch();

return 0;

40
Q.18]Write a function power() to raise number m to power n using function
overloading.

#include<iostream.h>

#include<conio.h>

#include<math.h>

class Calculate

public:

void power(int,int);

void power(float,int);

void display(float);

};

void Calculate::power(int m,int n)

float pow=1;

int i=1;

while(i<=n)

pow=pow*m;
41
i++;

cout<<"Power of integer number "<<m<<" is:"<<"\n";

display(pow);

/*

void Calculate::power(float m,int n)

float pow=1;

int i=1;

while(i<=n)

pow=pow*m;

i++;

cout<<"Power of fraction number "<<m<<"is:"<<"\n";

display(pow);

*/

void Calculate::display(float pow)

cout<<pow;

void main()

{
42
Calculate obj;

int a,b;

clrscr();

cout<<"Enter the number which have to calculate a power:"<<"\n";

cin>>a;

cout<<"Enter the power number:"<<"\n";

cin>>b;

obj.power(a,b);

getch();

/*Output:

*/

Q.19]Calculate area using function overloading.

#include<iostream.h>

#include<conio.h>

class over

43
{

float l,b,r,area;

public:

void volume(float,float);

void volume(float);

};

void over::volume(float l, float b)

cout<<"Area of rectangle = "<<l*b;

void over::volume(float r)

cout<<"Area of circle = "<<3.14*r*r;

44
void main()

over o;

clrscr();

float r,l,b;

cout<<"\nEnter radius: ";

cin>>r;

o.volume(r);

cout<<"\n\nEnter lenght and breadth: ";

cin>>l>>b;

o.volume(l,b);

getch();

/*Output:
45
*/

Q20.]Define class to represent Bank Account include the member,name of


depositer,account number,account type,balance amt,and member function are:

i] to assign intial value.

ii]to deposite amount.

iii]towithdraw amount.

iv]to display menu.

using menu.

#include<iostream.h>

#include<stdio.h>

#include<conio.h>

class bacc

private:

char dnm[20],tyacc[20],accno[20];

float bamt,damt,wamt;

public:

bacc()

bamt=200;

}
46
void insert()

cout<<"enter depositor name:";

cin>>dnm;

cout<<"type of account:";

cin>>tyacc;

cout<<"account number:";

cin>>accno;

void deposit()

cout<<"How much amount you want to deposit:";

cin>>damt;

bamt=bamt+damt;

cout<<"now your balance is:"<<bamt<<endl;

void wdrawal()

cout<<"How much amount you want to withdraw:";

cin>>wamt;

if(bamt<wamt)

cout<<"Your balance is less than withdrawal amount";

else

bamt=bamt-wamt;
47
cout<<"now your balance is:"<<bamt<<endl;

void display()

cout<<endl<<" ------The Details of your account are ------";

cout<<endl<<"Name Account type Account No. Balance";

cout<<endl<<dnm<<"\t"<<tyacc<<"\t \t "<<accno<<"\t"<<bamt;

};

int main()

int ch;

clrscr();

bacc obj;

while(ch!=5)

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

cout<<endl<<"Menu"<<endl<<"1. Enter details of account"<<endl<<"2.Deposit


amount"<<endl<<"3. Withdraw amount"<<endl<<"4. Display all
details"<<endl<<"5.Exit"<<endl;

cout<<endl<<"Enter you want choice:";

cin>>ch;

switch(ch)

case 1:

48
obj.insert();

break;

case 2:

obj.deposit();

break;

case 3:

obj.wdrawal();

break;

case 4:

obj.display();

break;

case 5:

break;

getch();

return 0;

49
Q.21] Define a class to represent a bank account which includes the
members,Name ,Account number ,Type of account ,Bal. amt Member functions
are:

a.To assign initial value

b.To deposit an account

c.To withdraw an account

d.To display name, account number & balance.

#include<iostream.h>

#include<conio.h>

#include<string.h>

class bank

char name[20];

int ano;

char atype[20];

float bal;

public:

void get(int no,char *n,char *t,float b)

strcpy(name,n);

ano=no;

strcpy(atype,t);

bal=b;

float deposit()

50
float amt;

cout<<“\nEnter amount: “;

cin>>amt;

bal=bal+amt;

return bal;

float withdrw()

float amt;

cout<<“\nHow many Rupees withdraw: “;

cin>>amt;

bal=bal-amt;

return bal;

void disp()

cout<<“\n\nAccount number: “<<ano;

cout<<“\n\nName: “<<name;

cout<<“\n\nAccount type: “<<atype;

cout<<“\n\nDeposit Amount: “<<deposit();

cout<<“\n\nAfter Withdraw Amount balnace: “<<withdrw();

};

void main()

int n;

char nm[20],t[20];
51
float a;

bank bk;

clrscr();

cout<<“\nEnter Account no.: “; cin>>n;

cout<<“\nEnter Name: “; cin>>nm;

cout<<“\nEnter account type: “; cin>>t;

cout<<“\nEnter balance amount: “;cin>>a;

bk.get(n,nm,t,a);

bk.disp();

getch();

Q22]Write a Program to generate following:


1
22
333
4444
55555

#include<iostream.h>
#include<conio.h>
int main()
{
int i,j;
for(i=1;i<=5;i++)
{
for(j=1;j<=i;j++)
{
cout<<i<<"\t";
}
cout<<endl;
}
getch();
}
52
/*
output
1
2 2
3 3 3
4 4 4 4
5 5 5 5 5

Q23]Write a program to generate light bill according to given data.

Unit charges
0 to 100 3.50
101 to 200 5.50
201 to 300 7
More than 300 10

#include<iostream.h>
#include<conio.h>
class bill
{
public:
int unit;
char name[50];
void accept();
void display();
};
void bill::accept()
{
cout<<"Enter customer Name:";
cin>>name;
cout<<"\n Enter unit";
cin>>unit;
}
void bill::display()
{
cout<<"\n Information of customer\n";
cout<<"Customer Name:"<<name;
53
cout<<"\nUnit \t:"<<unit;
cout<<"\nCharges:";
if(unit>=0 && unit<=200)
{
cout<<unit*3.50;
}
else if(unit>100 && unit<=200)
{
cout<<unit*5.50;
}
else if(unit>200 && unit<=300)
{
cout<<unit*7;
}
else
{
cout<<unit*10;
}
}
void main()
{
bill b;
b.accept();
b.display();
getch();

}
\*output:
Enter customer Name:shreya

Enter unit45

Information of customer
Customer Name:shreya
Unit :45
Charges:157.5
*/

54
Q24] An election is contested by 5 candidates. the candidates are numbered
from 1 to 5 and the voting is done by making the candidate number on the
ballots paper. Write a program to read the ballots and count the votes for each
candidates using an array.
#include<iostream.h>
#include<conio.h>
int main()
{
int i,n,count[100],a=0,b=0,c=0,d=0,e=0,spoilt=0;
cout<<"how many voters?";
cin>>n;
if(n<0)
{
cout<<"invalid";

}
cout<<"the following is the list of the candidates\n";
cout<<"1.CONGRESS.\n";
cout<<"2.BJP.\n";
cout<<"3.NCP.\n";
cout<<"4.MGP.\n";
cout<<"5.INDEPENDENT.\n";
for(i=1;i<=n;i++)
{
cout<<"enter your vote,that is the respective no of ur candidate:";
cin>>count[i];
if(count[i]==1)
a++;
else if(count[i]==2)
b++;
else if(count[i]==3)
c++;
else if(count[i]==4)
d++;
else if(count[i]==5)
e++;
else
spoilt++;
cout<<"\n";
}
cout<<"the no of votes are:\n";
cout<<"1.CONGRESS:"<<a<<"\n";
cout<<"2.BJP:"<<b<<"\n";
cout<<"3.NCP:"<<c<<"\n";
cout<<"4.MGP:"<<d<<"\n";
cout<<"4.INDEPENDENT:"<<e<<"\n";
55
cout<<"5.spoilt ballot:"<<spoilt<<"\n";
if(a>b && a>c && a>d && a>e)
cout<<"the winner is:CONGRESS!!";

else if(b>a && b>c && b>d && b>e)


cout<<"the winner is:BJP!!";

else if(c>a && c>b && c>d && c>e)


cout<<"the winner is:NCP!!";

else if(d>a && d>c && d>b && d>e)


cout<<"the winner is:MGP!!";

else if(e>a && e>c && e>d && e>b)


cout<<"the winner is:INDEPENDENT!!";
else
cout<<"there is a tie";
getch();
}
output:
3.NCP.
4.MGP.
5.INDEPENDENT.
enter your vote,that is the respective no of ur candidate:1

enter your vote,that is the respective no of ur candidate:2

enter your vote,that is the respective no of ur candidate:1

enter your vote,that is the respective no of ur candidate:1

enter your vote,that is the respective no of ur candidate:1

enter your vote,that is the respective no of ur candidate:3

enter your vote,that is the respective no of ur candidate:3

the no of votes are:


1.CONGRESS:4
2.BJP:1
3.NCP:2
4.MGP:0
4.INDEPENDENT:0
5.spoilt ballot:0
the winner is:CONGRESS!!
*/
56
Q25]Book details using array of object

Data member:char title[30],author[30],int price.

Member Function:getdata(void),putdata(void)

#include<iostream.h>

#include<conio.h>

class book

public:

char title[30];

char author[30];

int price;

void getdata(void);

void putdata(void);

};

void book::getdata(void)

cout<<"Enter title of book"<<"\n";

cin>>title;

cout<<"Enter author name:"<<"\n";

cin>>author;

cout<<"Enter price of book:"<<"\n";

cin>>price;

void book::putdata(void)

cout<<"book Name:"<<title<<"\n";
57
cout<<"book Author:"<<author<<"\n";

cout<<"book price:"<<price<<"\n";

void main()

book obj[20];

int n,i;

clrscr();

cout<<"Enter the how many book record u want:";

cin>>n;

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

cout<<"Enter the book details for book no.:"<<i<<"\n";

obj[i].getdata();

cout<<"\n"<<"book details"<<"\n";

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

cout<<"Book Details No.:"<<i<<"\n";

obj[i].putdata();

cout<<"\n";

getch();

/* OUTPUT:

Enter price of book:

500
58
book details

Book Details No.:1

book Name:Java

book Author:Kanetkar

book price:300

Book Details No.:2

book Name:C++

book Author:Mayuri

book price:250

Book Details No.:3

book Name:Swami

book Author:ShivajiSavant

book price:400

Book Details No.:4

book Name:OperatingSystem

book Author:Shreyada

book price:500

*/

59
Q26]Employee Details using Array of object

#include<iostream.h>

#include<conio.h>

class Employee

public:

char name[30];

int id;

int salary;

char Address[30];

void getdata(void);

void putdata(void);

};

void Employee::getdata(void)

cout<<"Enter name of Employee"<<"\n";

cin>>name;

cout<<"Enter id name:"<<"\n";

cin>>id;

cout<<"Enter salary of employee:"<<"\n";

cin>>salary;

cout<<"Enter address of Employee:"<<"\n";

cin>>Address

void Employee::putdata(void)

{
60
cout<<"Employee Name:"<<name<<"\n";

cout<<"Employee id:"<<id<<"\n";

cout<<"Employee salary:"<<salary<<"\n";

cout<<"Employee address:"<<Address<<"\n";

void main()

Employee obj[20];

int n,i;

clrscr();

cout<<"Enter the how many Employee record u want:";

cin>>n;

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

cout<<"Enter the Employee details for book no.:"<<i<<"\n";

obj[i].getdata();

cout<<"\n"<<"Employee details"<<"\n";

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

cout<<"Employee Details No.:"<<i<<"\n";

obj[i].putdata();

cout<<"\n";

getch();

/*output:
61
Enter the Employee details for book no.:2

Enter name of Employee

piyushya

Enter id name:

Enter salary of employee:

700

Enter address of Employee:

sangli

Employee details

Employee Details No.:1

Employee Name:sonal

Employee id:3

Employee salary:500

Employee address:pune

Employee Details No.:2

Employee Name:piyushya

Employee id:3

Employee salary:700

Employee address:sangli

*/

Q27]Write a class to represent a vector. Include member functions to perform


the following tasks:

62
1) To create the vector

2) To modify the value of given element.

3) To display the vector in the form (10, 20, 30,…)

#include<iostream>

#include<stdio.h>

using namespace std;

class vector

int size;

int *coord;

public:

vector();

void modify();

void display();

void multiply();

};

vector::vector()

cout<<"\n Enter Number of Co-ordinates : ";

cin>>size;

coord=new int[size];

cout<<"\n Enter " << size << " Co-ordinates : \n";

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

cout<<" ";
63
cin>>coord[i];

void vector::modify() //Function for Modifying the Co-ordinates

cout<<endl<<"\n Enter " << size << " New Co-ordinates : \n";

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

cout<<" ";

cin>>coord[i];

void vector::multiply() //Multiplying the Co-ordinates

int num;

cout<<endl<<"\n Enter Number to Multiply : ";

cin>>num;

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

coord[i]=coord[i]*num; //Multiplying the co-ordinates with the number


entered by the user

void vector::display() //Displaying the vector

cout<<"\n Vector : (";

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

64
{

cout<<coord[i];

if(i!=size-1)

cout<<",";

cout<<")";

int main()

vector v;

v.display();

v.modify();

v.display();

v.multiply();

v.display();

return 0;

/*Output:

Enter number of co-ordinates : 3

Enter 3 co-ordinates :

10

20

30

Vector : (10,20,30)

Enter 3 new co-ordinates :

40

50
65
60

Vector : (40,50,60)

Enter number to multiply : 3

Vector : (120,150,180)

*/

Q28]Use the constractor overloading to demonstrate following Unary oprator


over

1.Unary minus

2.!

3.Increment and decrement

4.Assingment oprator

#include <iostream>

using namespace std;

class check_count

public:

int count_plus;

int count_minus;

check_count()

count_plus = 0;

count_minus = 2;

66
};

void operator ++() { ++count_plus; } // count increment

void operator --() { --count_minus; } // count increment

};

int main()

check_count x, y; //creating objects

//before increment/decrement

cout << "x =" << x.count_plus<<"\n";

cout <<"y =" << y.count_minus<<"\n";

++x;

--y;

cout<<"\nAfter increment/decrement\n";

cout<<"x ="<<x.count_plus<<"\n";

cout<<"y ="<<y.count_minus<<"\n";

return 0;

/*Output:

X=0

Y=2

After increment/decrement

X=1
67
Y=1

*/

Q29 ]Binary oprator overloading

1.Addition and multiplication

2.String campare and String concatenation.

#include <iostream>

using namespace std;

int main()

int first, second, add, subtract, multiply;

cout << "Please enter two integer: ";

cin >> first;

cin >> second;

add = first + second;

subtract = first - second;

multiply = first * second;

divide = first / (float)second; //typecasting

cout << endl <<"Sum = " << add;

cout << endl <<"Difference = " << subtract;

cout << endl <<"Multiplication = " << multiply;

68
cout << endl <<"Division = " << divide;

return 0;

/*Output:

Please enter two integer:

11

Sum = 13

Difference = 9

Multiplication = 22

Division = 5.5

*/

2.String campare and String concatenation.

#include <cstrinag>

#include <iostream>

#include <string.h>

using namespace std;

class CompareString {

public:

char str[25];

CompareString(char str1[])
69
{

strcpy(this->str, str1);

int operator==(CompareString s2)

if (strcmp(str, s2.str) == 0)

return 1;

else

return 0;

int operator<=(CompareString s3)

if (strlen(str) <= strlen(s3.str))

return 1;

else

return 0;

int operator>=(CompareString s3)

if (strlen(str) >= strlen(s3.str))

return 1;

else

return 0;

};

void compare(CompareString s1, CompareString s2)

{
70
if (s1 == s2)

cout << s1.str << " is equal to "

<< s2.str << endl;

else {

cout << s1.str << " is not equal to "

<< s2.str << endl;

if (s1 >= s2)

cout << s1.str << " is greater than "

<< s2.str << endl;

else

cout << s2.str << " is greater than "

<< s1.str << endl;

void testcase1()

char str1[] = "Geeks";

char str2[] = "ForGeeks";

CompareString s1(str1);

CompareString s2(str2);

cout << "Comparing \"" << s1.str << "\" and \""

<< s2.str << "\"" << endl;

compare(s1, s2);

void testcase2()

char str1[] = "Geeks";


71
char str2[] = "Geeks";

CompareString s1(str1);

CompareString s2(str2);

cout << "\n\nComparing \"" << s1.str << "\" and \""

<< s2.str << "\"" << endl;

compare(s1, s2);

int main()

testcase1();

testcase2();

return 0;

/*Output:

Comparing "Geeks" and "ForGeeks"

Geeks is not equal to ForGeeks

ForGeeks is greater than Geeks

Comparing "Geeks" and "Geeks"

Geeks is equal to Geeks

*/

Q31].Write a cpp program to perform assignment (=) operator overloading for


binary operator (using friend function).

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

private:
72
int
x,y;
public:

void getdata()

clrscr();

cout<<"\n enter the value of x ";


cout<<endl;

cin>>x;

cout<<"\n enter the value of x "<<endl;


cin>>y;

void operator=(sample obj)

x=obj.x
;
y=obj.y
;

void display()

cout<<"\n value of x"<<x;

73
cout<<"\n value of y"<<y;

};

void main()

sample obj1,obj2;
obj1.getdata();
obj1.display();
obj2=obj1;
obj2.display();
getch();

32)Write a cpp program to overload

typecast operator.

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

void

main()

{ clrscr();

double a =

21.09399; float b

= 10.20;

int c ;

c = (int) a;

cout << "Line 1 - Value of (int)a is :" << c << endl ;

c = (int) b;

cout << "Line 2 - Value of (int)b is :" << c << endl ;

getch();

/*Output:

75
33)Write a cpp program to perform string compare (=) and string
concatenation (+) using operator overloading.

#include<iostream

.h>

#include<conio.>

#include<string.h

>

class cstring

public:

char

str[20];

public:

void getstring()

cout<<"\nEnter

string"; cin>>str;

void display()

cout<<str;

cstring operator+(cstring x)//concatennating string

76
{

cstring s;

strcat(str,x.st

r);

strcpy(s.str,st

r); return s;

int operator==(cstring &t); //compare string

};

int cstring::operator==(cstring &t)

77
for(int i=0;str[i]!='-';i++)

for(int j=0;t.str[j]!='-';j++)

if(str[i]==t.str[j])

return 0;

else

return 1;

int main()

clrscr();

cstring

s1;

cstring s2;

cstring s3;

s1.getstring
78
();

s2.getstring

();

cout<<"\n_ ";

cout<<"\n\nFirst string

is:"; s1.display();//display

first string

cout<<"\n\nsecond string

is:"; s2.display();

cout<<"\n_ ";

s3=s1+s2;

cout<<"\n\nconcatennated

string

79
s3.display();

int result;

result=s1==s2;//compare string using

==operator if(result==0)

cout<<"\nboth string is equal";

else

cout<<"\nboth are not equal";

return
0;

80
/*output

81
34)Write a cpp program to Inheritance Student class from Person Class
printing the properties of the Student (Single Inheritance).

#include<iostream

.h>

#include<conio.h>

class person

public:

char

firstnm[30];

char

lastnm[30];

char

gender[30];

void getdata()

cout<<"Enter First

Name:"<<endl;

cin>>firstnm;

cout<<"Enter Last

Name:"<<endl; cin>>lastnm;

cout<<"Enter

Gender"<<endl;

cin>>gender;

}};
82
class student:public person

private:

char

collegenm[30];

char level[30];

public:

void readdata()

cout<<"Enter Student College

Name:"<<endl; cin>>collegenm;

cout<<"enter Student

Level:"<<endl; cin>>level

83
cout<<end

l;

cout<<end

l;

void display()

cout<<"#####-----Student Information-----#####"<<endl;

cout<<"First Name Of

Student :-"<<firstnm<<endl; cout<<"Last

Name Of Student :-"<<lastnm<<endl;

cout<<"Gender :-"<<gender<<endl;

cout<<"College Name

:-"<<collegenm<<endl;

cout<<"Level :-"<<level<<endl;

};

void main()

clrscr();

student s;

s.getdata(

);

84
s.readdata();

s.display();

getch();

/*OUTPUT*/

85
86
35)Write a cpp program to display petrol's data using Multiple
Inheritance from fuel and liquid.

#include

<iostream.h>

#include

<conio.h>

class liquid

float

specific_gravity;

public:

void input()

cout<<"Specific gravity: ";

cin>>specific_gravity;

void output()

cout<<"Specific gravity: "<<specific_gravity<<endl;

};

87
class fuel

float

rate;

public:

void input()

cout<<"Rate(per liter):

Rs."; cin>>rate;

void output()

cout<<"Rate(per liter): Rs."<<rate<<endl;

88
}

};

class petrol: public liquid, public fuel

public:

void input()

liquid::input();

fuel::input();

void output()

liquid::outpu

t();

fuel::output(

);

};

int main()

89
clrscr()

; petrol

p;

cout<<"Enter

data"<<endl; p.input();

cout<<endl<<"Displaying

data"<<endl; p.output();

getch()

; return

0;
}

/*OUTPUT*/

90
Q36]. write a c++ program to handle divide by zero exception ?

#include<iostream.h>

#include<conio.h>

void main() {

int a, b, c;

float d;

clrscr();

cout << "Enter the value of a:";

cin>>a;

cout << "Enter the value of b:";

cin>>b;

cout << "Enter the value of c:";

cin>>c;

91
try {

if ((a - b) != 0) {

d = c / (a - b);

cout << "Result is:" << d;

} else {

throw (a - b);

} catch (int i) {

cout << "Answer is infinite because a-b is:" << i;

getch();

/*Output :-

Enter the value for a: 20

Enter the value for b: 20

Enter the value for c: 40

*/

Q37]. write a c++ program for merge two files into third file and display third
file ?

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

92
#include<stdio.h>
#include<stdlib.h>
using namespace std;
int main()
{
 //Ghanendra Yadav
 ifstream ifiles1, ifiles2;
 ofstream ifilet;
 char ch, fname1[20], fname2[20], fname3[30];
 cout<<"Enter first file name (with extension like file1.txt) : ";
 gets(fname1);
 cout<<"Enter second file name (with extension like file2.txt) : ";
 gets(fname2);
 cout<<"Enter Third File name of file : ";
 gets(fname3);
 ifiles1.open(fname1);
 ifiles2.open(fname2);
 if(ifiles1==NULL || ifiles2==NULL)
 {
  perror("Error Message ");
  cout<<"Press any key to exit...\n";
  getch();
  exit(EXIT_FAILURE);
 }
 ifilet.open(fname3);
 if(!ifilet)
 {
  perror("Error Message ");
  cout<<"Press any key to exit...\n";
  getch();
  exit(EXIT_FAILURE);
 }

93
 while(ifiles1.eof()==0)
 {
  ifiles1>>ch;
  ifilet<<ch;
 }
 while(ifiles2.eof()==0)
 {
  ifiles2>>ch;
  ifilet<<ch;
 }
 cout<<"The two files were merged into "<<fname3<<" file successfully..!!";
 ifiles1.close();
 ifiles2.close();
 ifilet.close();
 getch();
}

Q38.] write a c++ program to read a file contents and replaced space with $
sign ?

#include <iostream>

#include <fstream>

using namespace std;

int main () {

ofstream myfile;

myfile.open ("example.txt");

myfile << "Writing this to a file.\n";

myfile.close();
94
return 0;

/*Output :-

[file example.txt]

Writing this to a file.

*/

Q39.] write a c++ program to implement stack operations ?

#include <iostream>

#include <stack>

using namespace std;

int main()

// Empty stack

stack<int> mystack;

mystack.push(0);

mystack.push(1);

mystack.push(2);

// Printing content of stack

while (!mystack.empty()) {

cout << ' ' << mystack.top();

95
mystack.pop();

/*

Output :-

210

Note that output is printed on the basis of LIFO property

*/

Q40.] write a c++ program to implement queue operations ?

// Queue in Standard Template Library (STL)

#include <iostream>

#include <queue>

using namespace std;

void showq(queue <int> gq)

queue <int> g = gq;

while (!g.empty())

cout << '\t' << g.front();

g.pop();

96
cout << '\n';

int main()

queue <int> gquiz;

gquiz.push(10);

gquiz.push(20);

gquiz.push(30);

cout << "The queue gquiz is : ";

showq(gquiz);

cout << "\ngquiz.size() : " << gquiz.size();

cout << "\ngquiz.front() : " << gquiz.front();

cout << "\ngquiz.back() : " << gquiz.back();

cout << "\ngquiz.pop() : ";

gquiz.pop();

showq(gquiz);

return 0;

97
/*Output :-

The queue gquiz is : 10 20 30

gquiz.size() : 3

gquiz.front() : 10

gquiz.back() : 30

gquiz.pop() : 20 30

*/

Q41 ] write a program in c++ to implement different linked list operations ?

#include <iostream>

using namespace std;

struct Node {

int data;

struct Node *next;

};

struct Node* top = NULL;

void push(int val) {

struct Node* newnode = (struct Node*) malloc(sizeof(struct Node));

newnode->data = val;

newnode->next = top;

top = newnode;

98
void pop() {

if(top==NULL)

cout<<"Stack Underflow"<<endl;

else {

cout<<"The popped element is "<< top->data <<endl;

top = top->next;

void display() {

struct Node* ptr;

if(top==NULL)

cout<<"stack is empty";

else {

ptr = top;

cout<<"Stack elements are: ";

while (ptr != NULL) {

cout<< ptr->data <<" ";

ptr = ptr->next;

cout<<endl;

int main() {

99
int ch, val;

cout<<"1) Push in stack"<<endl;

cout<<"2) Pop from stack"<<endl;

cout<<"3) Display stack"<<endl;

cout<<"4) Exit"<<endl;

do {

cout<<"Enter choice: "<<endl;

cin>>ch;

switch(ch) {

case 1: {

cout<<"Enter value to be pushed:"<<endl;

cin>>val;

push(val);

break;

case 2: {

pop();

break;

case 3: {

display();

break;

100
case 4: {

cout<<"Exit"<<endl;

break;

default: {

cout<<"Invalid Choice"<<endl;

}while(ch!=4);

return 0;

/*

Output

1) Push in stack

2) Pop from stack

3) Display stack

4) Exit

Enter choice: 1

Enter value to be pushed: 2

Enter choice: 1

Enter value to be pushed: 6

Enter choice: 1

101
Enter value to be pushed: 8

Enter choice: 1

Enter value to be pushed: 7

Enter choice: 2

The popped element is 7

Enter choice: 3

Stack elements are:8 6 2

Enter choice: 5

Invalid Choice

Enter choice: 4

Exit

*/

102