Harsh CPP MSC It 1st Sem
Harsh CPP MSC It 1st Sem
3 Write a c++ program to perform addition off two number using function. 4
4 Write a Program that uses a class where the member functions are defined inside the 5
class.
5 Write a program that uses a class where the member functions are defined outside a 6
class.
7 Write a c++ program to calculate simple interest using class and object. 8
15 Write a c++ program to find the greatest number of three using goto statement. 18
16 Write a c++ program to find the greatest number of three using switch statement. 19
22 Write a c++ program to implement a student class having name, roll no, rank, address 25
as data members.
23 Write a c++ program to implement sphere class to find surface area and volume. 26
26 Write a c++ program to exchange the content of variable using call by reference. 31
27 Write a c++ program to exchange the content of variable using call by value. 32
29 Write a c++ program to take data input from user and sort it in ascending order after 35
sorting using function.
30 Write a program to input name, address and telephone number of ‘n’ persons (n<=20). 37
Sort according to the name as a primary key and address as the secondary key. Print the
sorted telephone directory.
37 Write a program having pointer to void to store address of integer variable then print 46
value of integer variable using pointer to void. Perform the same operation for float
variable.
38 Write a c++ program to create class complex having data members to store real and 47
imaginary part to perform following operation:
A. add 2 complex numbers
B. subtract 2 complex numbers.
39 Create class polar having data member radius and angle it contains member function 49
for taking the input in data member and member function for displaying values of data
members. It contains declaration of friend function which accept 2 object of class polar
and returns object of class polar after addition.
40 Create class polar having data member radius and angle it contains member function 51
for taking the input in data member and member function for displaying values of data
members.It contains declaration of friend function which accept 2 object of class polar
and returns object of class polar after addition.
41 Write a c++ program to read data on an employee and compute the net salary of each 53
employee DA=60 % of basic and income tax=30% of the gross salary.
42 Write a c++ program to evaluate the sum of series 1+2+….n using while loop. 55
44 Write a C++ program to create class mother having data member to store salary of 57
mother, create another class father to store salary of father. use friend function which
accept object of father and mother and print salary of mother and father.
45 Write a C++ program using class which uses static overloaded function to swap 2 58
integers,2 float method using reference variable.
46 Write a C++ program using class which uses static overloaded function to swap 2 59
integers,2 float method using passing by address.
47 Write a c++ program to create class string having pointer to char as data member and 60
provide following facilities:
a. overloaded operators to ‘+’ to add two string object.
b. overloaded operator ‘=’ to assign string object to another string object.
48 Write a c++ program to find the biggest of 3 number using pointer and function. 62
50 Write a c++ program to create inline function to take 2 arguments and should return 64
the minimum values.
52 Write a c++ program to copy content of one file into another after removing extra 66
spaces between word and name of file should come from command line argument.
#include<iostream.h>
#include<conio.h>
class sum
{
public:
int a,b; void add();
};
void sum::add()
{
cout<<"Enter First Number="<<endl; cin>>a;
cout<<"Enter Second Number= "<<endl; cin>>b;
}
void main()
{
sum s;
clrscr();
s.add();
cout<<"Sum of Two Number is= "<<s.a+s.b;
getch();
}
Output:
1|Page
2. Write a program to input 20 arbitrary numbers in one-dimensional
array. Calculate Frequency of each number. Print the number and
its frequency in a tabular form.
Code:
#include<iostream.h>
#include<conio.h>
int main()
{
int a[20],b[20];
int i,j,top=0,t;
cout<<"Enter 20 Numbers:";
for(i=0;i<20;i++)
{
cin>>a[i];
}
//finding unique b[0]=a[0];
for(i=1;i<20;i++)
{
t=0;
for(j=0;j<=top;j++)
{
if(b[j] == a[i])
{
t=1;
break;
}
}
if(t==0)
{
b[++top]=a[i];
}
}
//counting apearance of each element cout<<"Element Frequenc";
for(i=0;i<=top;i++)
{
t=0;
for(j=0;j<20;j++)
{
if(b[i]==a[j])
{
t++;
}
}
cout<<b[i]<<"\t"<<t<<"\n";
}
}
2|Page
Output:
3|Page
3. Write a c++ program to perform addition of two number using function.
Code:
#include<iostream.h>
#include<conio.h>
int add(int x, int y);
int main()
{
int num1; int num2; int sum;
clrscr();
cout<< "Enter the first number" <<endl; cin>>num1;
cout<< "Enter the second number" <<endl; cin>>num2;
sum=add(num1, num2);
cout<<"sum of two numbers "<<sum<<endl; getch();
return 0;
}
int add(int x, int y)
{
return (x+y);
}
Output:
4|Page
4. Write a Program that uses a class where the member functions are
defined inside the class.
Code:
#include<iostream.h>
#include<conio.h>
class book
{
char title[30]; float price;
public :
void getdata()
{
cout<<"Enter the Title of book :";
cin>>title;
cout<<"Enter the price of book :";
cin>>price;
}
void putdata()
{
cout<<"\nTitle of book :"<<title;
cout<<"\nPrice of book :"<<price;
}
};
void main()
{
clrscr();
book obj;
obj.getdata();
obj.putdata();
getch();
}
Output:
5|Page
5. Write a program that uses a class where the member functions are
defined outside a class.
Code:
#include<iostream.h>
#include<conio.h>
class book
{
char title[30]; float price; public:
void getdata(); void putdata();
};
void book :: getdata()
{
cout<<"Enter the Title of book :"; cin>>title;
cout<<"Enter the Price of book :"; cin>>price;
}
void book :: putdata()
{
cout<<"\nTitle of book :"<<title; cout<<"\nPrice of book :"<<price;
}
void main()
{
clrscr(); book obj;
obj.getdata();
obj.putdata();
getch();
}
Output:
6|Page
6. Write a c++ program to calculate simple interest using function.
Code:
#include<iostream.h>
#include<conio.h>
class Simple_interest
{
public:
float si, amount, r;
void calculate(float amt, float rate)
{
amount = amt;
r = rate;
}
void calculate(float time)
{
si = (amount * r * time) / 100;
cout<< "Simple interest is:" <<si;
}
};
int main()
{
float amt, rate, time; clrscr();
cout<< "Enter amount:";
cin>>amt;
cout<< "Enter rate:";
cin>>rate;
cout<< "Enter time:";
cin>>time;
Simple_interestobj;
obj.calculate(amt, rate);
obj.calculate(time);
getch();
}
Output:
7|Page
7. Write a c++ program to calculate simple interest using class and object.
Code:
#include<iostream.h>
#include<conio.h>
class bank
{
private: float p; float r; float t; float si;
float amount;
public:
void read()
{
cout<<" Enter Principle Amount :: "; cin>>p ;
cout<<"Enter Rate of Interest :: "; cin>>r;
cout<<"Enter Number of years :: "; cin>>t;
si= (p *r*t) /100; amount = si + p;
}
void show()
{
cout<<"\nInterest : "<<si;
cout<<"\nTotal Amount : "<<amount<<"\n";
}
};
int main ()
{
clrscr();
bank b ;
b.read ( );
b.show ( );
getch();
}
Output:
8|Page
8. Write a c++ program for dynamic initialization of variable.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<"enter value for:"<<endl;
cout<<"a = ";cin>>a;
cout<<"b = ";cin>>b;
int c=a+b;
cout<<"result : "<<c;
getch();
}
Output:
9|Page
9. Write a c++ program to print weekday using if else statement.
Code:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int weekday;
cout<< "Enter weekday day number (1-7) : ";
cin>> weekday;
if(weekday == 1)
cout<<endl<< "Monday"<<"";
else
if(weekday == 2)
cout<<endl<< "Tuesday"<<"";
else
if(weekday == 3)
cout<<endl<< "Wednesday"<<"";
else
if(weekday == 4)
cout<<endl<< "Thursday"<<"";
else
if(weekday == 5)
cout<<endl<< "Friday"<<"";
else
if(weekday == 6)
cout<<endl<< "Saturday"<<"";
else
if(weekday == 7)
cout<<endl<< "Sunday"<<"";
else
cout<<endl<< "Please enter weekday number between 1-7.";
getch();
}
Output:
10 | P a g e
10. Write a c++ program to print weekday using switch statement.
Code:
#include<iostream.h>
#include<conio.h>
int main()
{
clrscr();
int weeknumber;
cout<<"Enter week number(1-7): ";
cin>>weeknumber;
switch(weeknumber)
{
case 1: cout<<"Monday";
break;
case 2: cout<<"Tuesday";
break;
case 3: cout<<"Wednesday";
break;
case 4: cout<<"Thursday";
break;
case 5: cout<<"Friday";
break;
case 6: cout<<"Saturday";
break;
case 7: cout<<"Sunday";
break;
default: cout<<"Invalid input! Please enter week no. between 1-7.";
}
getch();
}
Output:
11 | P a g e
11. Write a c++ program to design simple calculator to perform arithmetic operation.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
clrscr();
float numOne,
numTwo, res; int
choice;
do
{
cout<<"1. Addition\n";
cout<<"2. Subtraction\
n"; cout<<"3.
Multiplication\n";
cout<<"4. Division\n";
cout<<"5. Exit\n\n";
cout<<"Enter Your
Choice(1-5): ";
cin>>choice;
if(choice>=1 && choice<=4)
{
cout<<"\nEnter any two
Numbers: ";
cin>>numOne>>numTwo;
}
switch(choice)
{
case 1:
res =
numOne+numTwo;
cout<<"\nResult =
"<<res; break;
case 2:
res = numOne-
numTwo; cout<<"\
nResult = "<<res;
break;
case 3:
res =
numOne*numTwo;
cout<<"\nResult =
"<<res; break;
case 4:
res =
numOne/numTwo;
12 | P a g e
cout<<"\nResult =
"<<res; break;
case 5:
default:
cout<<"\nWrong
Choice!"; break;
}
cout<<"\n";
}
while(choice!
=5);
cout<<endl;
getch();
}
13 | P a g e
Output:
14 | P a g e
12. Write a c++ program to design mark sheet of 1stSEM.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
float
rol,m1,m2,m3,m4,m5,obt;
char n[50],fname[50];
float
per;
clrscr()
;
cout<<"\nEnter
name= "; cin>>n;
cout<<"Enter Father's
name= "; cin>>fname;
cout<<"Enter
rollno.= ";
cin>>rol;
cout<<"Enter mark of
C++= "; cin>>m1;
cout<<"Enter mark of
Maths= "; cin>>m2;
cout<<"Enter mark of
IWT= "; cin>>m3;
cout<<"Enter mark of
CSA= "; cin>>m4;
cout<<"Enter mark of
RDBMS=" ; cin>>m5;
obt=m1+m2+m3+m4+m5;
per=obt/5;
cout<<"\n\n\t\t++++Mark Sheet+++
+"<<endl; cout<<"\t\t MSc Ist
Sem"<<endl; cout<<"\nName =
"<<n;
cout<<"\nFather Name=
"<<fname; cout<<"\nRoll NO\
t"<<rol; cout<<"\nSUBJECT
OBT_MARK ";
cout<<"\nC++\t "<<m1;
cout<<"\nMaths\t "<<m2;
cout<<"\nIWT\t "<<m3;
cout<<"\nCSA\t "<<m4;
cout<<"\nRDBMS\t "<<m5;
cout<<"\nObtain Marks\t
"<<obt; cout<<"\nPercentage\t
15 | P a g e
"<<per; if(per>40)
{
cout<<"\nResult\tPASS";
}
else
{
cout<<"\nResult\tFAIL";
}
getch();
}
16 | P a g e
Output:
17 | P a g e
13. Write a c++ program to print weekday using enum.
Code:
#include<iostream.
h>
#include<conio.h
>
enum week { Monday, Tuesday, Wednesday, Thursday, Friday,
Saturday, Sunday }; int main()
{
week day;
day =
Friday;
cout<< "Day: " <<
day+1<<endl; getch();
}
Output:
18 | P a g e
14. Write a c++ program to find largest number using conditional operator.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
int num1, num2, num3,
largest; clrscr();
cout<<"Enter three
numbers: "; cin>> num1
>> num2 >> num3;
largest = num1 > num2 ? (num1 > num3 ? num1 : num3) : (num2 > num3 ? num2 : num3) ;
Output:
19 | P a g e
15. Write a c++ program to find the greatest number of three using goto statement.
Code:
#include<iostream.
h>
#include<conio.h
> void main ()
{
clrscr();
int a,b,c;
cout<<"\nEnter three numbers
"<<endl; start:
{
cin>>a>>b>>c;
if(a>b&&a>c)
{
cout<<"\na is the greatest "<<a<<endl;
}
else if (b>a&&b>c)
{
cout<<"\nb is the greatest "<<b<<endl;
}
else if (c>a&&c>b)
{
cout<<"\nc is the greatest "<<c<<endl;
}
goto start;
}
getch();
}
Output:
110 | P a g
e
16. Write a c++ program to find the greatest number of three using switch statement.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
double
x,y,z;
clrscr();
cout<< "Enter Three
Numbers: "; cin>> x >> y
>> z;
switch (x > y)
{
case 1 :
switch(x
> z)
{
case 1 :
cout<< "Largest = " << x
<<endl; break;
case 0 :
cout<< "Largest = " << z
<<endl; break;
}
break;
case 0 :
switch(y > z)
{
case 1 :
cout<< "Largest = " << y
<<endl; break;
case 0 :
cout<< "Largest = " << z
<<endl; break;
}
break;
}
getch();
}
Output:
111 | P a g
e
112 | P a g
e
17. Write a c++ program to print table of two using goto statement.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
int i=1,n,m,a;
clrscr();
cout<<"Enter the
limit :"; cin>>n;
cout<<"Enter the table's
number :"; cin>>a;
start:
cout<<"\
n"<<i<<"*"<<a<<"="<<i*a;
i++;
if(i<=n)
{
goto start;
}
getch();
}
Output:
20 | P a g e
18. Write a c++ program to find winner of candidate for election.
Code:
#include "bits/stdc+
+.h" using
namespace std;
void findElectionWinner(string votes[], int total_votes)
{ map<string, int>candidate_votes_count;
// counting each person
votes for (int i = 0;
i<total_votes; i++) {
candidate_votes_count[votes[i]]++;
}
// finding
winner int
max_votes =
0;
string election_winner;
for (auto&entry
:candidate_votes_count) { string
key = entry.first;
int val = entry.second;
// checking the votes with max
votes if (val>max_votes) {
// updating max votes and
member max_votes = val;
election_winner = key;
// comparing the name if the votes are equal
}
else if (val ==
max_votes&&election_winner> key) {
election_winner = key;
}
}
cout<<”Election winner is : “<<election_winner<<endl;
}
int main() {
string votes[] = {"A", "B", "C", "B", "A", "C", "D", "D", "A", "B", "D", "B", "A"};
findElectionWinner(votes, 13);
return 0;
}
Output:
21 | P a g e
19. Write a c++ program to check whether given no. is prime or not.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
int n,i,m=0,
flag=0; clrscr();
cout<<"Enter the Number to check
Prime: "; cin>>n;
m=n/2;
for(i=2;i<=m;i++)
{
if(n%i==0)
{
cout<<"Number is not
Prime."<<endl; flag=1;
break;
}
}
if (flag==0)
cout<< "Number is
Prime."<<endl; getch();
}
Output:
22 | P a g e
20. Write a c++ program to generate 3x3 matrix.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
int i,j,a[3]
[3]; clrscr();
cout<< "Enter Elements of
Array : "; for (i=0;i<3;i++)
{
for (j=0;j<3;j++)
{
cin>>a[i][j];
}
}
cout<<"3x3 Matrix
:"<<endl; for
(i=0;i<3;i++)
{
cout<<endl;
for (j=0;j<3;j++)
{
cout<<a[i][j]<<" ";
}
}
getch(
);
}
Outpu
t:
23 | P a g e
21. Write a c++ program for addition of 3x3 matrix.
Code:
#include<iostream.
h>
#include<conio.h
> void main()
{
int mat1[3][3], mat2[3][3], i, j,
mat3[3][3]; clrscr();
cout<<"Enter Elements of First
Matrix: "; for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>mat1[i][j];
}
cout<<"Enter Elements of Second
Matrix: "; for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cin>>mat2[i][j];
}
cout<<"\nAdding the Two Given Matrix...\
n"; for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
mat3[i][j] = mat1[i][j]+mat2[i][j];
}
cout<<"Addition Result of Two Given
Matrix is:\n"; for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
cout<<mat3[i]
[j]<<" ";
} cout<<endl;
getch(
);
}
Outpu
t:
24 | P a g e
25 | P a g e
22. Write a c++ program to implement a student class having name, roll
no, rank, address as data members.
Code:
#include<iostream.
h>
#include<conio.h
> class student
{
public:
char
name[30];
int rollno;
int rank;
char
address[30];
void
getdata(void);
void
display(void);
};
void student :: getdata(void)
{
cout<<"enter
name :";
cin>>name;
cout<<"enter roll
no. :"; cin>>rollno;
cout<<"enter
rank :"; cin>>rank;
cout<<"enter
address. :";
cin>>address;
}
void student :: display(void)
{
cout<<"name is:
"<<name<<endl; cout<<"roll no.
is: "<<rollno<<endl;;
cout<<"rank is:"<<rank<<endl;
cout<<"address is:
"<<address<<endl;
}
void main()
{
student t;
t.getdata();
cout<<"\nEntered Details..\n";
t.display();
getch();
}
26 | P a g e
Output:
27 | P a g e
23. Write a c++ program to implement sphere class to find surface area and volume.
Code:
#include<iostream.
h>
#include<conio.h
> class sphere
{
public:
float a,r,v;
void
getdata()
{
cout<<"enter radius of
sphere"<<endl; cin>>r;
}
void display()
{
v=
1.33*3.14*r*r*r;
a = 4*3.14*r*r;
cout<<"Volume of sphere is "<<v<<" cubic
meter"<<endl; cout<<"Surface Area of sphere is
"<<a<<" square meter "<<endl;
}
};
void main()
{
clrscr();
sphere s;
s.getdata(
);
s.display();
getch();
}
Output:
28 | P a g e
24. Write a c++ program to perform following without using
library function: A.to reverse the string accepted as argument.
Code:
#include<iostream.h>
#include<conio.h>
void str(char[15]);
void main()
{
char name[15];
cout<<"Enter any
string="; cin>>name;
str(name);
getch();
}
void str(char name[15])
{
char name1[15];
int l=0,i;
for(i=0;name[i]!='\0';i++)
{
l=l+1;
}
for(i=0;name[i]!='\0';i++)
{
name1[i]=name[l-1];
l=l-1;
}
name1[i]='\0';
cout<<"revers name= "<<name1<<endl;
}
Output:
29 | P a g e
24 B. Write a program to count the number of characters in string passes as argument in the form of
char array.
Code:
#include<iostream.h>
#include<conio.h>
void str(char a[10]);
void main()
{
clrscr();
char name[10]; cout<<"\
nEnter any string: ";
cin>>name;
str(name);
getch();
}
void str(char a[10])
{
int i=0,n=0;
for(i=0;a[i] !='\0';i+
+)
{ n+
+;
}
cout<<"Number of character ="<<n;
}
Output:
210 | P a g
e
25. Write a c++ program to perform following without using
library function: A.to copy one string into another passed as argument in
the form of char array.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int i;
char name[10];
char name1[10];
cout<<"Enter any string=
"; cin>>name;
for(i=0;name[i]!='\0';i++)
{
name1[i]=name[i];
}
name1[i]='\0';
cout<<"\n Copied string is:-
"<<name1<<endl; getch();
}
Output:
211 | P a g
e
25 B. Write a program to count the number of consonants, vowels in each word of sentence
passed as argument in the form of char array.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
char str[15];
int
c=0,v=0,i;
cout<<"Enter the array element= ";
cin>>str;
for(i=0;str[i]!='\0';i++)
{
if(str[i]=='a'||str[i]=='i'||str[i]=='e'||str[i]=='o'||str[i]=='u'||str[i]=='I'||str[i]=='A'||str[i]=='U'||str[i]
=='E'||str[i]=='O')
v=v+1;
else
c=c+1;
}
str[i]='\0';
cout<<"\nNumber of vowels="<<v; cout<<"\
nNumber of consonants is ="<<c<<endl; getch();
}
Output:
30 | P a g e
26. Write a c++ program to exchange the content of variable using call by reference.
Code:
#include<iostream.h>
#include<conio.h>
void swap(int *x, int *y)
{
int swap;
swap=*x;
*x=*y;
*y=swap;
}
int main()
{
clrscr();
int x=500, y=100;
cout<<"Values before swapping :\n";
cout<<"Value of x is: "<<x<<endl;
cout<<"Value of y is: "<<y<<endl;
swap(&x,&y);
cout<<"\n \n";
cout<<"Values after swapping :\n";
cout<<"Value of x is:
"<<x<<endl; cout<<"Value of y
is: "<<y<<endl; getch();
}
Output:
31 | P a g e
27. Write a c++ program to exchange the content of variable using call by value.
Code:
#include<iostream.h>
#include<conio.h>
void swap(int,int);
int main()
{
int a,b;
clrscr();
cout<<"Enter Value Of A ::
"; cin>>a;
cout<<"\nEnter Value of B ::
"; cin>>b;
swap(a,b);
Output
:
32 | P a g e
28. Write a c++ program to sort the integer array.
Code:
#include<iostream.h>
#include<conio.h>
#define MAX 100
int main()
{
int
arr[MAX];
int n,i,j;
int temp;
clrscr();
cout<<"Enter total number of elements to read: ";
cin>>n;
if(n<0 || n>MAX)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}
for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"]
"; cin>>arr[i];
}
cout<<"Unsorted Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl; for(i=0;i<n;i+
+)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Sorted (Ascending Order) Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
getch();
return 0;
}
33 | P a g e
Output:
34 | P a g e
29. Write a c++ program to take data input from user and sort it in ascending order after
sorting using function.
Code:
#include<iostream.h>
#include<conio.h>
#define MAX 100
int main()
{
int
arr[MAX];
int n,i,j;
int temp;
clrscr();
cout<<"Enter total number of elements to read: ";
cin>>n;
if(n<0 || n>MAX)
{
cout<<"Input valid range!!!"<<endl;
return -1;
}
for(i=0;i<n;i++)
{
cout<<"Enter element ["<<i+1<<"]
"; cin>>arr[i];
}
cout<<"Unsorted Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl; for(i=0;i<n;i+
+)
{
for(j=i+1;j<n;j++)
{
if(arr[i]>arr[j])
{
temp =arr[i];
arr[i]=arr[j];
arr[j]=temp;
}
}
}
cout<<"Sorted (Ascending Order) Array elements:"<<endl;
for(i=0;i<n;i++)
cout<<arr[i]<<"\t";
cout<<endl;
getch();
}
35 | P a g e
Output:
36 | P a g e
30. Write a program to input name, address and telephone number of ‘n’ persons (n<=20).
Sort according to the name as a primary key and address as the secondary key. Print the sorted
telephone directory.
Code:
#include<iostream>
#include<string>
using namespace std;
class Record
{
struct Person
{
string name,address,number;
}p[20];
int n;
public: void getdata()
{
cout<<"Enter Number of record you wish to enter :
"; cin>>n;
cout<<"Enter Name, Address and Number for "<<n<<" persons :\
n"; for(int i=0;i<n;i++)
{
cin>>p[i].name>>p[i].address>>p[i].number;
}
}
void sortrec()
{
int i,j;
for(i=0;i<n;i++)
{
for(j=0;j<n-i-1;j++)
{
if(p[j].name.compare(p[j+1].name) > 0)
{
string tmp;
//swap name
tmp = p[j].name;
p[j].name=p[j+1].name;
p[j+1].name=tmp;
//swap address
tmp = p[j].address;
p[j].address=p[j+1].address;
p[j+1].address=tmp;
//swap number
tmp = p[j].number;
p[j].number=p[j+1].number;
p[j+1].number=tmp;
}
else if(p[j].name.compare(p[j+1].name) == 0)
{
if(p[j].address.compare(p[j+1].address) > 0)
{
string tmp;
37 | P a g e
//swap name
tmp = p[j].name;
p[j].name=p[j+1].name;
p[j+1].name=tmp;
//swap address
tmp = p[j].address;
p[j].address=p[j+1].address;
p[j+1].address=tmp;
//swap number
tmp = p[j].number;
p[j].number=p[j+1].number;
p[j+1].number=tmp;
}
}
}
}
}
void display()
{
cout<<"Records..\nName Number Address\n";
for(int i=0;i<n;i++)
cout<<p[i].name<<" "<<p[i].number<<" "<<p[i].address<<"\n";
}
};
int main()
{
Record r;
r.getdata();
r.sortrec();
r.display();
return(0);
}
Output:
38 | P a g e
31. Write a c++ program to search the second largest element in an array.
Code:
#include<iostream.h>
#include<conio.h>
int main ()
{
int A[10],n,i,j,x;
cout<<"Enter size of array :
"; cin>>n;
cout<< "Enter elements of array :
"; for (i=0;i<n;i++)
cin>>A[i
]; for
(i=0;i<n;i++)
{
for(j=i+1;j<n;j++)
{
if(A[i]<A[j])
{
x=A[i];
A[i]=A[j];
A[j]=x;
}
}
}
cout<< "Second largest number : " <<
A[1]; getch();
}
Output:
39 | P a g e
32. Write a c++ program to search the second smallest array in an array.
Code:
#include<iostream.h>
#include<conio.h>
int main ()
{
int A[10], n, i, j, x;
cout<< "Enter size of array :
"; cin>> n;
cout<< "Enter elements of array :
"; for (i = 0; i< n; i++)
cin>> A[i];
for (i = 0; i< n; i+
+)
{
for (j = i + 1; j < n; j++)
{
if (A[i] < A[j])
{
x = A[i];
A[i] =
A[j]; A[j]
= x;
}
}
}
cout<< "\nSecond smallest number : " << A[n -
2]; return 0;
}
Output:
40 | P a g e
33. Write a c++ program to demonstrate static data member.
Code:
#include<iostream.h>
#include<conio.h>
class item
{
static int count;
public:
void DisplayCounter()
{
count++;
cout<<"count:"<<count<<endl;
}
};
int item::count;
void main()
{
item a,b,c;
cout<<"Printing value of static variable of class after incrementing with different objects:\n";
a.DisplayCounter();
b.DisplayCounter();
c.DisplayCounter()
; getch();
}
Output:
41 | P a g e
34. Write a c++ program to demonstrate constructor with default argument.
Code:
#include<iostream.h>
#include<conio.h>
class Demo
{
private:
int
X,Y; public:
Demo()
{
X = 0;
Y = 0;
void main()
{
clrscr();
Demo d1= Demo(10);
";
d2.putValues();
getch();
}
42 | P a g e
Output:
43 | P a g e
35. Write a c++ program to demonstrate destructor in inheritance.
Code:
#include<iostream.h>
#include<conio.h>
class parent
{
public:
parent()
{
cout<<"Parent class Constructor\n";
}
~parent()
{
cout<<"Parent class Destructor\n";
}
};
public:
child()
{
cout<<"Child class Constructor\n";
}
~ child()
{
cout<<"Child class Destructor\n";
}
};
int main()
{
clrscr()
; child
c;
getch();
}
Output:
44 | P a g e
36. Write a c++ program to generate transpose of 3x3 matrix.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int mat[3][3],i,j,trans_mat[3][3];
clrscr();
cout<<"Enter the elements of matrix : \n";
for (i = 0; i< 3; i++)
{
for (j = 0; j < 3; j++)
{
cin>> mat[i][j];
}
}
for (i = 0; i< 3; i++)
{
for (j = 0; j < 3; j++)
{
trans_mat[j][i] = mat[i][j];
}
}
Output
:
45 | P a g e
37. Write a program having pointer to void to store address of integer variable then print
value of integer variable using pointer to void. Perform the same operation for float variable
Code:
#include<iostream>
using namespace std;
int main()
{
void *ptr;
int a=5;
float b=8.91;
ptr=&a;
Output:
46 | P a g e
38. Write a c++ program to create class complex having data members to store real and
imaginary part to perform following operation:
A. add 2 complex numbers
Code:
#include<iostream.h>
#include<conio.h>
class complex_number
{
public :
int real1,real2,imag1,imag2;
void getdata()
{
cout<<"Enter real and imaginary parts of first complex number:
"<<endl; cin>>real1>>imag1;
cout<<"Enter real and imaginary parts of second complex
number:"<<endl; cin>>real2>>imag2;
}
void putdata()
{
cout<< "Sum of two complex numbers = " <<real1+real2<< " + " <<imag1+imag2<< "i";
}
};
void main()
{
clrscr();
complex_number obj;
obj.getdata();
obj.putdata();
getch();
}
Output:
47 | P a g e
B. subtract 2 complex numbers.
Code:
#include<iostream.h>
#include<conio.h>
class complex_number
{
public:
int real, imag;
};
void main()
{
clrscr();
complex_number num1, num2, sub;
cout<<"Enter real and imaginary parts of first complex
number:"<<endl; cin>> num1.real >> num1.imag;
cout<<"Enter real and imaginary parts of second complex
number:"<<endl; cin>> num2.real >> num2.imag;
sub.real = num1.real - num2.real;
sub.imag = num1.imag -
num2.imag; if ( sub.imag>= 0 )
{
cout<< "Subtraction of two complex numbers = " <<sub.real<< " + "
<<sub.imag<< "i";
}
else
{
cout<< "Subtraction of two complex numbers = " <<sub.real<< " - " <<sub.imag<<
"i";
}
getch();
}
Output:
48 | P a g e
39. Create class polar having data member radius and angle it contains member function for
taking the input in data member and member function for displaying values of data members. It
contains declaration of friend function which accept 2 object of class polar and returns object of class
polar after addition.
Code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
class Polar
{
float r,a;
public:
Polar(float rr=0,float aa=0)
{
r=rr;
a=aa;
}
void input();
void output();
friend Polar add(Polar,Polar);
};
void Polar::input()
{
cout<<"\n enter radius and angle:";
cin>>r>>a;
}
void Polar::output()
{
cout<<"\n radius=
"<<r; cout<<"\n angle=
"<<a;
}
Polar add(Polar obj1,Polar obj2)
{
Polar temp;
float x1,y1,x2,y2;
x1=obj1.r*cos(obj1.a);
y1=obj1.r*sin(obj1.a);
x2=obj2.r*cos(obj2.a);
y2=obj2.r*sin(obj2.a);
temp.r=sqrt((x1+x2)*(x1+x2)+(y1+y2)*(y1+y2));
temp.a=atan((x1+x2)/(y1+y2));
return temp;
}
void main()
{
clrscr();
Polar p(3,22.0/7/4),q(4,22.0/7/4);
Polar r;
r.input();
r.output();
r=add(p,q);
cout<<"\n resultant Polar \n";
49 | P a g e
r.output();
getch();
}
Output:
50 | P a g e
40. Create class polar having data member radius and angle it contains member function for
taking the input in data member and member function for displaying values of data members. It
contains declaration of friend function which accept 2 object of class polar and returns object of class
polar after addition.
Code:
#include <iostream.h>
#include <conio.h>
#include <math.h>
class Polar
{
float r,a;
public:
Polar(float rr=0,float aa=0)
{
r=rr;
a=aa;
}
void input();
void output();
friend Polar add(Polar,Polar);
};
void Polar::input()
{
cout<<"\n enter radius and angle:";
cin>>r>>a;
}
void Polar::output()
{
cout<<"\n radius=
"<<r; cout<<"\n angle=
"<<a;
}
Polar add(Polar obj1,Polar obj2)
{
Polar temp;
float x1,y1,x2,y2;
x1=obj1.r*cos(obj1.a);
y1=obj1.r*sin(obj1.a);
x2=obj2.r*cos(obj2.a);
y2=obj2.r*sin(obj2.a);
temp.r=sqrt((x1+x2)*(x1+x2)+(y1+y2)*(y1+y2));
temp.a=atan((x1+x2)/(y1+y2));
return temp;
}
void
main() 49
{
clrscr();
Polar p(3,22.0/7/4),q(4,22.0/7/4);
Polar r;
r.input();
r.output();
r=add(p,q);
cout<<"\n resultant Polar \n";
51 | P a g e
r.output();
getch();
}
Output:
52 | P a g e
41. Write a c++ program to read data on an employee and compute the net salary of each
employee DA=60 % of basic and income tax=30% of the gross salary.
Code:
#include<iostream.h>
#include<conio.h>
class employee
{
int emp_num;
char emp_name[20];
float emp_basic;
float sal;
float emp_da;
float net_sal;
float emp_it;
public:
void get_details();
};
void employee :: get_details()
{
cout<<"\n\nEnter employee number: ";
cin>>emp_num;
cout<<"Enter employee name: ";
cin>>emp_name;
cout<<"Enter employee basic: ";
cin>>emp_basic;
emp_da=0.6*emp_basic;
emp_it=0.3*(emp_basic+emp_da);
net_sal=(emp_basic+emp_da)-emp_it;
cout<<"\n\n\nDetailsof : "<<emp_name;
cout<<"\n\nEmployee number: "<<emp_num;
cout<<"\nBasicsalary : "<<emp_basic; cout<<"\
nEmployeeDA : "<<emp_da; cout<<"\nIncomeTax
: "<<emp_it; cout<<"\nNetSalary : "<<net_sal;
}
void main()
{
employee emp[10];
int i,num;
clrscr();
cout<<"\nEnter number of employee details\n";
cin>>num;
for(i=0;i<num;i++)
{
emp[i].get_details();
}
getch();
}
53 | P a g e
Output:
54 | P a g e
42. Write a c++ program to evaluate the sum of series 1+2+….n using while loop.
Code:
#include<iostream.h>
#include<conio.h>
void main(){
int n, sum=0;
clrscr();
cout<<"Enter the value of n(should be a positive integer):
"; cin>>n;
if(n<=0){
cout<<"Invalid value of
n";
}
else{
int i=1;
while(i<=n){
sum=sum+i;
i++;
}
cout<<"Sum of first n natural numbers is: "<<sum;
}
getch();
}
Output:
55 | P a g e
43. Write a c++ program to find roots of quadratic equation.
Code:
#include<iostream.h>
#include<math.h>
#include<conio.h>
int main() {
int a = 1, b = 2, c = 1;
float discriminant, realPart, imaginaryPart, x1,
x2; if (a == 0) {
cout<<"This is not a quadratic equation";
}
else {
discriminant = b*b -
4*a*c; if (discriminant >
0) {
x1 = (-b + sqrt(discriminant)) /
(2*a); x2 = (-b - sqrt(discriminant))
/ (2*a);
cout<<"Roots are real and different."<<endl;
cout<<"Root 1 = "<<x1<<endl;
cout<<"Root 2 = "<<x2<<endl;
}
else if (discriminant == 0) {
cout<< "Roots are real and same."
<<endl; x1 = (-b + sqrt(discriminant)) /
(2*a); cout<<"Root 1 = Root 2 ="<< x1
<<endl;
}
else
{ realPart = (float) -b/(2*a);
imaginaryPart =sqrt(-discriminant)/(2*a);
cout<<"Roots are complex and
different."<<endl;
cout<<"Root 1 = "<<realPart<<" + "<<imaginaryPart<<"i"<<endl;
cout<<"Root 2 = "<<realPart<<" - "<<imaginaryPart<<"i"<<endl;
}
}
getch();
return(0);
}
Output:
56 | P a g e
44. Write a C++ program to create class mother having data member to store salary of
mother, create another class father to store salary of father. use friend function which accept object
of father and mother and print salary of mother and father.
Code:
#include<iostream.h>
#include<conio.h>
class father;
class mother
{
long int
msal; public:
mother()
{
cout<<"Enter mother's salary : ";
cin>>msal;
}
friend void add(mother, father);
};
class father
{
long int
fsal; public:
father()
{
cout<<"Enter father's salary :
"; cin>>fsal;
}
friend void add(mother, father);
};
void add(mother m, father f)
{
cout<<"\nsum of salaries is "<<m.msal+f.fsal;
}
void main()
{
mother m;
father f;
add(m,f);
getch();
}
Output:
57 | P a g e
45. Write a C++ program using class which uses static overloaded function to swap 2
integers,2 float method using reference variable.
Code:
#include<iostream.h>
#include<conio.h>
class sample
{
public:
static void swap(int &x,int&y)
{
int t=0;
t=x;
x=y;
y=t;
}
static void swap(float &x,float&y)
{
float t=0;
t=x;
x=y
;
y=t;
}
};
void main()
{
clrscr();
static int a,b;
static float f1,f2;
cout<<"Enter two integer\n";
cin>>a>>b;
sample::swap(a,b);
cout<<"\nSwapped integer values are "<<a<<" and
"<<b; cout<<"\nEnter two float\n";
cin>>f1>>f2;
sample::swap(f1,f2);
cout<<"\nSwapped float values are "<<f1<<" and
"<<f2; getch();
}
Output:
58 | P a g e
46. Write a C++ program using class which uses static overloaded function to swap 2
integers,2 float method using passing by address.
Code:
#include<iostream.h>
#include<conio.h>
class sample
{
public:
static void swap(int *x,int *y)
{
int t=0;
t=*x;
*x=*y;
*y=t;
}
static void swap(float *x,float *y)
{
float t=0;
t=*x;
*x=*y;
*y=t;
}
};
void main()
{
clrscr();
static int a,b;
static float f1,f2;
cout<<"Enter two integer\n";
cin>>a>>b;
sample::swap(&a,&b);
cout<<"\nSwapped integer values are "<<a<<" and
"<<b; cout<<"\nEnter two float\n";
cin>>f1>>f2;
sample::swap(&f1,&f2);
cout<<"\nSwapped float values are "<<f1<<" and
"<<f2; getch();
}
Output:
59 | P a g e
47. Write a c++ program to create class string having pointer to char as data member
and provide following facilities:
A. overloaded operators to ‘+’ to add two string object
Code:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class String
{
char str1[20];
char *p1;
public:
void input()
{
p1=str1;
cout<<"Enter your String:
"<<endl; cin>>str1;
}
void display()
{
cout<<"String: "<<str1;
}
String operator +(String s)
{
String obj;
strcat(str1,s.str1);
strcpy(obj.str1,str1);
return obj;
}
};
void main()
{
String s1,s2,s3;
s1.input();
s2.input();
s3=s1+s2;
s3.display();
getch();
}
Output:
60 | P a g e
B. overloaded operator ‘=’ to assign string object to another string object.
Code:
#include<iostream.h>
#include<string.h>
#include<conio.h>
class String
{
char str1[20];
char *p1;
public:
void input()
{
p1=str1;
cout<<"Enter your String:
"<<endl; cin>>str1;
}
void display()
{
cout<<"Assigned String: "<<str1;
}
String operator =(String obj)
{
strcpy(obj.str1,str1);
return obj;
}
};
void main()
{
clrscr();
String
s1,s2;
s1.input();
s2.input();
s2=s1;
s2.display();
getch();
}
Output:
61 | P a g e
48. Write a c++ program to find the biggest of 3 number using pointer and function.
Code:
#include<iostream.h>
#include<conio.h>
void large(int *a,int *b,int *c)
{
if(*a>*b)
{
if(*a>*c)
{
cout<<"A is greater"<<endl;
}
else
{
cout<<"C is greater"<<endl;
}
}
else
{
if(*b>*c)
{
cout<<"B is greater"<<endl;
}
else
{
cout<<"C is greater"<<endl;
}
}
}
void main()
{
clrscr();
int x,y,z;
cout<<"Enter value of A=
"; cin>>x;
cout<<"Enter value of B=
"; cin>>y;
cout<<"Enter value of C=
"; cin>>z;
large(&x,&y,&z);
getch();
}
Output:
62 | P a g e
49. Write a c++ program to create inline function to calculate area of circle.
Code:
#include <iostream.h>
#define PI 3.1416
#include<conio.h>
inline float area(float r)
{
return PI*r*r;
}
void main()
{
float r;
clrscr();
cout<<"Enter radius of a circle:
"; cin>>r;
cout<<"Area of the circle is
"<<area(r); getch();
}
Output:
63 | P a g e
50. Write a c++ program to create inline function to take 2 arguments and should return
the Greater value.
Code:
#include<iostream.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
cout<<"\nEnter 1st number :: ";
cin>>a;
cout<<"\nEnter 2nd number :: ";
cin>>b;
Output
:
64 | P a g e
51. Write a c++ program to calculate factorial of number using copy constructor.
Code:
#include<iostream.h>
#include<conio.h>
class fact
{
int n, i,
facti;
public:
fact(int x)
{
n=x;
facti=1;
}
fact(fact &x)
{
n=x.n;
facti=1;
}
void calculate()
{
for(i=1;i<=n;i++)
{
facti=facti*i;
}
}
void display()
{
cout<<"\n Factorial : "<<facti;
}
};
void main()
{
int x;
clrscr();
cout<<"\n Enter Value :
"; cin>>x;
fact f1(x);
f1.calculate();
f1.display();
fact f2(f1);
f2.calculate();
f2.display();
getch();
}
Output:
65 | P a g e
52. Write a c++ program to copy content of one file into another after removing extra
spaces between word and name of file should come from command line argument.
Code:
#include <iostream>
using namespace std;
void removeSpaces(string &str)
{
int n =
str.length(); int i =
0, j = -1;
bool spaceFound = false;
while (++j < n && str[j] == '
');
while (j < n)
{
if (str[j] != ' ')
{
if ((str[j] == '.' || str[j] == ',' ||
str[j] == '?') && i - 1 >= 0 &&
str[i - 1] == ' ')
str[i - 1] = str[j++];
else
str[i++] = str[j++];
spaceFound = false;
}
else if (str[j++] == ' ')
{
if (!spaceFound)
{
str[i++] = ' ';
spaceFound = true;
}
}
}
if (i <= 1)
str.erase(str.begin() + i, str.end());
else
str.erase(str.begin() + i - 1, str.end());
}
int main()
{
string str = " Welcome to MyProgramnumber52 ";
removeSpaces(str);
cout << str;
return 0;
}
Output
:
66 | P a g e
53. Write a c++ program to demonstrate virtual function.
Code:
#include<iostream.h>
#include<conio.h>
class Base
{
public:
virtual void display()
{
cout<< "Base Function" <<endl;
}
};
void main()
{
clrscr();
Derived
d1;
Base* b1 =
&d1; b1-
>display();
getch();
}
Output:
67 | P a g e
54. Write a c++ program to demonstrate pure virtual function
Code:
#include<iostream.h>
#include<conio.h>
class B {
public:
virtual void s() = 0; // Pure Virtual Function
};
class D:public B
{ public:
void s() {
cout<< "Virtual Function in Derived class\n";
}
};
void main()
{
clrscr();
B *b;
D dobj;
b=
&dobj; b-
>s();
getch();
}
Output:
68 | P a g e