Jaycpp

Download as pdf or txt
Download as pdf or txt
You are on page 1of 95

Q1. Write a program to print a hut using cout.

Sol:

#include <iostream>

using namespace std;

int main()

cout << "\n * * * * * * * * * * * * * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * * * * * * * * * * * * * * * * * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * ";

cout << "\n * * * * * * * ";

cout << "\n * * * * * ";

cout << "\n * * * * * ";

cout << "\n * * * * * ";


cout << "\n * * * * * ";

cout << "\n * * * * * ";

cout << "\n * * * * * ";

cout << "\n * * * * * * * * * * * * * * * * * * * * * ";

OUTPUT
Q2. Write a program to generate electricity bill.

Sol:

#include <iostream>

using namespace std;

int main()

int cid;

int prev, curr;

cout << "\t\t ELECTRICITY BILL \t\t" << endl;

cout << "\nEnter The CONSUMER ID : ";

cin >> cid;

cout << "\n Enter Previous Meter Reading : ";

cin >> prev;

cout << "\n Enter Current Meter Reading : ";

cin >> curr;

cout << "\tTotal Reading = " << curr - prev << endl;

float total = curr - prev;

if (total <= 200)

cout << "\tBILL : $0 (NOT TO PAY)" << endl;

else if (total > 200 && total <= 300)

total = total * 5 + 150;

cout << "\tBILL : $" << total << endl;

else
{

total = total * 10 + 300;

cout << "\tBILL : $" << total << endl;

OUTPUT
Q3. Write a program to convert a character from uppercase to lowercase and vice versa using
ASCII value.

Sol:

#include<iostream>

using namespace std;

int main()

char ch;

cout << "\nEnter a Character : ";

cin >> ch;

if (ch >= 65 && ch <= 90)

cout << "\nTHE CHARACTER ENTERED IS IN UPPERCASE : ";

cout << "\n" << ch;

ch = ch+32;

cout << "\nNOW THE CHARACTER CHANGED TO LOWERCASE : ";

cout << "\n" << ch;

else if (ch >=97 && ch <=122)

cout << "\nTHE CHARACTER ENTERED IS IN LOWERCASE : ";

cout << "\n" << ch;

ch = ch-32;

cout << "\nNOW THE CHARACTER CHANGED TO UPPERCASE : ";

cout << "\n" << ch;

}
OUTPUT
Q4. Write a program to check whether the entered character is a consonant or a vowel.

Sol:

#include<iostream>

using namespace std;

int main()

char ch;

cout << "\nEnter a Character : ";

cin >> ch;

if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U')

cout << "\nTHE CHARACTER " << ch << " IS A VOWEL";

else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')

cout << "\nTHE CHARACTER " << ch << " IS A VOWEL";

else

cout << "\nTHE CHARACTER " << ch << " IS A CONSONANT";

}
OUTPUT
Q5. Write a program to enter the marks and print grades.

Sol:

#include<iostream>

using namespace std;

int main()

int marks;

cout << "\nEnter the Marks you obtained : ";

cin >> marks;

if (marks >= 90)

cout << "\nGrade A";

else if (marks >= 80 && marks < 90)

cout << "\nGrade B";

else if (marks >= 70 && marks < 80)

cout << "\nGrade C";

else if (marks >= 60 && marks < 70)

cout << "\nGrade D";

else if (marks >= 50 && marks < 60)

{
cout << "\nGrade E";

else

cout << "\nFAIL";

OUTPUT
Q6. Write a program to print the days of a week using switch case.

Sol:

#include <iostream>

using namespace std;

int main()

int c;

cout << "\nEnter your choice : ";

cin >> c;

switch (c)

case 1:

cout << "SUNDAY";

break;

case 2:

cout << "MONDAY";

break;

case 3:

cout << "TUESDAY";

break;

case 4:

cout << "WEDNESDAY";

case 5:

cout << "THURSDAY";

break;

case 6:

cout << "FRIDAY";


break;

case 7:

cout << "SATURDAY";

break;

default:

cout << "INVALID CHOICE";

OUTPUT
Q7. Write a program to create an arithmetic calculator.

Sol:

#include <iostream>

using namespace std;

int main()

cout << "\n\t\tCALCULATOR\t\t";

cout << "\n1 : ADDITION";

cout << "\n2 : SUBTRACTION";

cout << "\n3 : MULTIPLICATION";

cout << "\n4 : DIVISION";

cout << "\n5 : MODULOUS";

int c;

cout << "\nENTER YOUR CHOICE : ";

cin >> c;

switch (c)

case 1:

int add;

cout << "\nEnter the numbers you want to ADD : ";

int num1,num2;

cin >> num1 >> num2;

add = num1 + num2;

cout << "\nAddition : " << add;

break;

case 2:

int sub;
cout << "\nEnter the numbers you want to SUBTRACT :";

int num3,num4;

cin >> num3 >> num4;

sub = num3 - num4;

cout << "\nSubtraction : " << sub;

break;

case 3:

int mul;

cout << "\nEnter the numbers you want to MULTIPLY : ";

int num5,num6;

cin >> num5 >> num6;

mul = num5 * num6;

cout << "\nMultipication : " << mul;

break;

case 4:

float div;

cout << "\nEnter the numbers you want to DIVIDE : ";

int num7,num8;

cin >> num7 >> num8;

div = num7 / num8;

cout << "\nDivision : " << div;

break;

case 5:

int mod;

cout << "\nEnter the numbers you want to MOD : ";

int num9,num10;

cin >> num9 >> num10;


mod = num9 % num10;

cout << "\nModulous : " << mod;

break;

default:

cout << "\nINVALID CHOICE";

OUTPUT
Q8. Write a program to check whether inputted number is palindrome or not.

Sol:

#include<iostream>

using namespace std;

int main()

int check , rev = 0 , check1;

cout << "\nEnter a number : ";

cin >> check;

check1 = check;

while (check > 0)

int d = check % 10;

rev = (rev * 10) + d;

check = check / 10;

if (rev == check1)

cout << "PALINDROME NUMBER";

else

cout << "NOT A PALINDROME";

}
OUTPUT
Q9. Write a program to find the integers & sum of all integers greater than 100 & less than 200
that are divisible by 7.

Sol:

#include<iostream>

using namespace std;

int main()

int s = 0;

cout << "\nTHE NUMBERS DIVISIBLE BY 7 ARE:";

for (int i = 100; i <= 200; i++)

if (i%7 == 0)

s = s+i;

cout << "\n" << i;

cout << "\nSum of numbers are : " << s;

OUTPUT
Q10. Write a program to generate the Fibonacci series upto a given number.

Sol:

#include<iostream>

using namespace std;

int main()

int n;

cout << "\nEnter the number of terms you want to print the Fibonacci Series : ";

cin >> n;

int f = 0, s = 1, t = 0;

cout << f << "\t" << s;

for (int i = 1; i <= (n-2); i++)

t = f+s;

cout << "\t" << t;

f = s;

s = t;

OUTPUT
Q11. Write a program to find the product of the sums of all odd and even digits in a given number.
For example if the number is 2314568 then the program should calculate (2+4+6+8)*(3+1+5)

Sol:

#include<iostream>

using namespace std;

int main()

int n;

cout << "\nEnter the number : ";

cin >> n;

int evsum = 0, odsum = 0, prod = 0;

while (n > 0)

int d = n % 10;

if (d % 2 == 0)

evsum = evsum + d;

else

odsum = odsum + d;

n = n / 10;

prod = evsum * odsum;

cout << "\nSum of even digits : " << evsum;

cout << "\nSum of odd digits : " << odsum;

cout << "\n\n\nProduct of sum of odd and even digits of your given number : " << prod;
}

OUTPUT
Q12. Write a program to check whether inputted number is prime or not.

Sol:

#include<iostream>

using namespace std;

int main()

int check;

int f = 0;

cout << "\nEnter the number to check : ";

cin >> check;

for (int i = 1; i <= check; i++)

if (check % i == 0)

f++;

if (f == 2)

cout << "\n\nYour number is Prime";

else

cout << "\n\nYour number is not Prime";

}
OUTPUT
Q13. Write a program to check whether inputted string is palindrome or not without using string
functions.

Sol:

#include<iostream>

#include<string.h>

using namespace std;

int main()

string word;

cout << "\nEnter the word to check : ";

cin >> word;

string rev = "";

for (int i = 0; i < word.length(); i++)

char ch = word[i];

rev = ch + rev;

if (word == rev)

cout << "\nPalindrome Word";

else

cout << "\nNot a Palindrome Word";

}
OUTPUT
Q14. Write a program to generate the series.

***

*****

*******

*********

Sol:

#include<iostream>

using namespace std;

int main()

int sp = 4;

for (int i = 1; i <= 5; i++)

for (int j = 1; j <= sp; j++)

cout << " ";

for (int k = 1; k <= i; k++)

cout << "*";

sp--;

cout << "\n";

}
OUTPUT
Q15. Write a program to generate the series.

***

*****

*******

Sol:

#include<iostream>

using namespace std;

int main()

for (int i = 1; i <= 5; i++)

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

cout << "* ";

cout << "\n";

OUTPUT
Q16. Write a program to generate the series.

**

***

****

*****

****

***

**

Sol:

#include<iostream>

using namespace std;

int main()

int c = 4;

for (int i = 1; i <= 9; i++)

if (i <= 5)

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

cout << "* ";

cout << "\n";

else
{

for (int k = 1; k <= c; k++)

cout << "* ";

c--;

cout << "\n";

OUTPUT
Q17. Write a program to generate the series.

***

*****

***

Sol:

#include<iostream>

using namespace std;

int main()

int sp = 3 , sy = 1;

for (int i = 1; i <= 7; i++)

if (i <= 4)

for (int j = 1; j <= sp; j++)

cout << " ";

for (int k = 1; k <= sy; k++)

cout << "*";

sp--;

sy = sy + 2;

cout << "\n";


}

else

if (i == 5)

sp = 1;

sy = 5;

for (int l = 1; l <= sp; l++)

cout << " ";

for (int m = 1; m <= sy; m++)

cout << "*";

sp++;

sy = sy - 2;

cout << "\n";

OUTPUT
Q18. Write a program to generate the series.

212

32123

4321234

543212345

Sol:

#include<iostream>

using namespace std;

int main()

int sp = 4 , sy = 2;

for (int i = 1; i <= 5; i++)

for (int j = 1; j <= sp; j++)

cout << " ";

for (int k = i; k >= 1; k--)

cout << k;

for (int l = sy; l <= i; l++)

cout << l;

cout << "\n";


sy=2;

sp--;

OUTPUT
Q19. Write a program to generate the series.

ABCDEFGFEDCBA

ABCDEF FEDCBA

ABCDE EDCBA

ABCD DCBA

ABC CBA

AB BA

A A

Sol:

#include<iostream>

using namespace std;

int main()

int sp = -1, sy = 0;

for (int i = 71; i >= 65; i--)

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

cout << (char)j;

for (int k = 1; k <= sp; k++)

cout << " ";

for (int l = i; l >= 65; l--)

if (i == 71 && sy == 0)
{

l = 70;

sy = 1;

cout << (char)l;

cout << "\n";

sp = sp + 2;

OUTPUT
Q20. Write a Program to Find the Sum OF Digit Using Recursion

Sol:

#include<iostream>

using namespace std;

int sum (int num)

int res = 0;

if (num == 0)

return res;

int d = num % 10;

int add = sum (num / 10);

res = d + add;

return res;

int main()

int n;

cout << "\nEnter the number : ";

cin >> n;

cout << "\nSum of digits of your number : " << sum(n);

OUTPUT
Q21. Write a program that computesthe value of the following expression. 1+X/1! + X/2! + X/3! +
XN/N!.

Sol:

#include<iostream>

#include<math.h>

using namespace std;

int main()

int n , x , u , fact = 1;

cout << "\nEnter the value of n where the series ends : ";

cin >> n;

cout << "\nEnter the value of X : ";

cin >> x;

float sum = 0;

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

u = pow(x , i);

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

fact = fact * j;

sum = sum + (u / fact);

fact = 1;

cout << "\nSum of the Expression : " << sum;

OUTPUT
Q22. Write a program that computes the values of the following. X/1 + X^2/2 +
X^3/3………….X^N/N.

Sol:

#include<iostream>

#include<math.h>

using namespace std;

int main()

int n , x , u;

cout << "\nEnter the value of n where the series ends : ";

cin >> n;

cout << "\nEnter the value of X : ";

cin >> x;

float sum = 0;

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

u = pow(x , i);

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

if (i % 2 == 0)

sum = sum - (double) u / j;

else

sum = sum + (double) u / j;

}
}

cout << "\nSum of the Expression : " << sum;

OUTPUT
Q23. Write a program to find the factorial of number using recursion.

Sol:

#include<iostream>

using namespace std;

int factorial (int x)

if (x > 1)

return x * factorial (x - 1);

else

return true;

int main()

int n;

cout << "Enter the Number you want the Factorial : ";

cin >> n;

cout << "Factorial of "<< n << " : " << factorial (n);

OUTPUT
Q24. Write a program that swaps two integers by reference without using third variable.

Sol:

#include<iostream>

using namespace std;

int main()

int a = 5, b = 10;

cout << "\nBefore swap a : " << a << " b : " << b;

a = a * b;

b = a / b;

a = a / b;

cout << "\nAfter swap a : " << a << " b : " << b;

OUTPUT
Q25. Using the concept of function overloading write two versions of power() functions to raise a
number m to a power n. The function takes a float value for m and int type for n in one version
and an int value for both m & n in other version and the return type of the function should be
double. Use a default value of 2 for n to make the function to calculate square when this argument
omitted. Write a main() that calls both the function.

Sol:

#include<iostream>

#include<math.h>

using namespace std;

double power (int m , int n = 2)

return pow(m,n);

double power (float m,int n=2)

return pow(m,n);

int main()

int num1 , num3;

float num2;

cout << "\n\nEnter a number : ";

cin >> num1;

cout << "\n\nEnter a number : ";

cin >> num2;

cout << "\n\nEnter a number : ";

cin >> num3;

cout << "\n\n" << num1 << " raise to " << num3 << ":" << power (num1,num3);

cout << "\n\n" << num1 << " raise to 2 : " << power (num1);
cout << "\n\n" << num2 << " raise to " << num3 << ":" << power (num2,num3);

cout << "\n\n" << num2 << " raise to 2 : " << power (num2);

OUTPUT
Q26. Define a class employee with the following specifications.

Sol.

Private members of class employee.

Empno integer

Ename 20 characters

Basic,hr,da float

Netpay float

Calculate(): a function to find basic + hra + da with float returntype.

Public member function of class employee.

Havedata(): to accept values for empno, ename, basic, hra, da & invoke.

Calculate() to calculate netpay.

Sol:

#include<iostream>

using namespace std;

class Employee

int empno;

char ename[20];

float basic,hra,da,netpay;

float calculate(float,float,float);

public:

void getdata();

void display();

};

float Employee::calculate(float b,float h,float d)

return b+h+d;
}

void Employee::getdata()

cout<<"\nEnter the Employee no : "; cin>>empno;

cout<<"\nEnter the Employee Name : "; cin>>ename;

cout<<"\nEnter the Basic Pay : "; cin>>basic;

cout<<"\nEnter the DA Pay : "; cin>>da;

cout<<"\nEnter the HRA Pay : "; cin>>hra;

netpay=calculate(basic,da,hra);

void Employee::display()

cout<<"\n\nEmployee no: "<<empno;

cout<<"\nEmployee Name: "<<ename;

cout<<"\nBasic Salary: "<<basic;

cout<<"\nDA Allowance: "<<da;

cout<<"\nHR Allowance: "<<hra;

cout<<"\nNetpay: "<<netpay;

int main()

Employee one;

one.getdata();

one.display();

}
OUTPUT
Q27. Declare a class student to simulate result preparation system for 10 students with the
following data member rollno, name, marks in 3 subjects, average and grade. After calculating
average marks, grade should be calculated as follows.

Sol.

Average Marks Grade

<50 ”F”

>=50 <65 “D”

>=65 <75 “C”

>=75 <90 “B”

>=90 “A”

Sol:

#include<iostream>

using namespace std;

class student

int rollno;

string name;

float marks[3],average,total; char grade;

public:

student()

total=0;average=0;

void getdata();

void display();

};

void student :: getdata()

{
cout<<"\n\nEnter the Rollno:";

cin>>rollno;

cout<<"\nEnter the Name:"; cin>>name;

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

cout<<"\nEnter the marks of Subject No:10"<<i+1<<":";

cin>>marks[i];

total+=marks[i];

average=total/3;

void student:: display()

cout<<"\n\nRollno:"<<rollno;

cout<<"\nName:"<<name;

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

cout<<"\nMarks of Subject No:10"<<i+1<<":"<<marks[i];

cout<<"\nSum total of mark="<<total;

if(average>=90)

cout<<"\nGrade=A";

else if(average>=75 && average<90)

cout<<"\nGrade=B";
}

else if(average>=65 && average<75)

cout<<"\nGrade=C";

else if(average>=50 && average<65)

cout<<"\nGrade=D";

else

cout<<"\nGrade=F";

int main()

student one[10];

cout<<"\n\n\nEnter the details";

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

one[i].getdata();

cout<<"\n\n\nStudents details";

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

one[j].display();

}
}

OUTPUT
Q28. Write a program to explain the concept of static members.

Sol:

#include<iostream>

using namespace std;

class static_member

static int nobj;

public:

static_member()

nobj++;

static void no_of_object()

cout<<"\nNumber of Object="<<nobj;

};

int static_member :: nobj=0;

int main()

static_member obj[50];

obj[1].no_of_object();;

OUTPUT
Q29. Create a class time that has 3 data members int hour, min, sec. Write a program in which one
constructor should initialized these data members to 0 and another should initialized it to some
fixed values. A member function should two objects of type time passed as arguments. A main()
program should create two initialized time objects and one that isn’t initialized. Then it should add
two initialized values together leaving the result in the third variable. Finally it should display the
value of this third variable.

Sol:

#include<iostream>

using namespace std;

class Time

int hh,mm,ss;

public:

Time()

hh=mm=ss=0;

Time(int a,int b,int c)

hh=a;

mm=b;

ss=c;

Time calci(Time& ,Time &);

void display();

};

void Time ::display()

if(mm>=60)
{

hh=hh+mm/60;

mm=mm%60;

if(ss>=60)

mm=mm+ss/60;

ss=ss%60;

cout<<"\nThe Time is:"<<hh<<":"<<mm<<":"<<ss;

Time Time ::calci(Time &T,Time &t)

Time temp;

temp.hh=T.hh+t.hh;

temp.mm=T.mm+t.mm;

temp.ss=T.ss+t.ss;

return temp;

int main()

int h1,h2,m1,m2,s1,s2;

cout<<"\n\t\tEnter the Time in the Format HH:MM:SS:";

cin>>h1>>m1>>s1;

Time T1(h1,m1,s1);

T1.display();

cout<<"\n\n\t\tEnter the Time in the Format HH:MM:SS:";


cin>>h2>>m2>>s2;

Time T2(h2,m2,s2);

T2.display();

Time T3=T1.calci(T1,T2);

cout<<"\n\n\t\tValues after Addition as follows\n";

T3.display();

OUTPUT
Q30. Write a program to swap private data members of two different classes (Friend as a bridge).

Sol:

#include<iostream>

using namespace std;

class B;

class A

int num1,num2;

public:

void getdata(int a,int b)

num1=a;

num2=b;

void display(void)

cout<<"\n\t\tThe value of Num 1 in Class 1:"<<num1;

cout<<"\n\t\tThe value of Num 2 in Class 1:"<<num2<<"\n";

friend void swap(A &, B &);

};

class B

int num3,num4;

public:

void getdata(int a,int b)

{
num3=a;

num4=b;

void display(void)

cout<<"\n\t\tThe value of Num 3 in Class 2:"<<num3<<"\n";

cout<<"\t\tThe value of Num 4 in Class 2:"<<num4<<"\n";

friend void swap(A &, B &);

};

void swap(A &x, B &y)

int temp = x.num1;

x.num1 = y.num3;

y.num3 = temp;

int temp1 = x.num2;

x.num2 = y.num4;

y.num4 = temp1;

int main()

A C1;

B C2;

C1.getdata(100,300);

C2.getdata(200,400);

cout<<"\n\n\t\tValues before exchange"<<endl;

C1.display();
C2.display();

swap(C1, C2);

cout<<"\n\n\t\tValues after exchange"<<endl;

C1.display();

C2.display();

OUTPUT
Q31. Define a class distance having private member feet and inch of integer type. Use overloaded
pre increment ++ operator to increment distance and return the result to the main().

Sol:

#include<iostream>

using namespace std;

class Distance

int feet,inch;

public:

void getdata();

void display();

Distance operator++()

++inch;

++feet;

return *this;

};

void Distance :: getdata()

cout<<"\n\nEnter the inch:";

cin>>inch;

cout<<"\n\nEnter the feet:";

cin>>feet;;

void Distance :: display()

if(inch>=12)
{

feet+=inch/12;

inch=inch%12;

cout<<"\n\nThe Feet is:"<<feet;

cout<<"\n\nThe inch is:"<<inch;

int main()

Distance d1;

d1.getdata();

d1.display();

++d1;

cout<<"\n\nAfter using operator overloading";

d1.display();

OUTPUT
Q32. Define a class string. Use overloaded binary += operator to concatenate two string using
friend function.

Sol:

#include<iostream>

#include<string.h>

using namespace std;

class Star

char s[100];

public:

void getdata();

void display();

friend Star operator+=(Star,Star);

};

void Star ::getdata()

cout<<"\n\nEnter String:";

cin>>s;

void Star :: display()

cout<<s;

Star operator+=(Star x,Star y)

Star s1;

strcpy(s1.s,x.s);

strcat(s1.s,y.s);
return s1;

int main()

Star str1, str2, str3;

str1.getdata();

str2.getdata();

cout<<"\n\nFirst String is:";

str1.display();

cout<<"\n\nSecond String is:";

str2.display();

str3=str1+=str2;

cout<<"\n\nConcatenated String is:";

str3.display();

OUTPUT
Q33. Write a program to overload << and >> operators used with cout and cin respectively

Sol:

#include <iostream>

using namespace std;

class Complex

int real , img;

public:

Complex(int r = 0, int i =0)

real = r;

img = i;

friend ostream &operator << (ostream &, Complex &);

friend istream &operator >> (istream &, Complex &);

};

istream &operator >> (istream &in, Complex &c)

cout<<"\nEnter Real Part:";

in>>c.real;

cout<<"\nEnter Imaginary Part:";

in>>c.img;

return in;

ostream &operator << (ostream &out, Complex &c)

out<<c.real;
out<<" + i"<< c.img;

return out;

int main()

Complex c1;

cin>>c1;

cout<<"\nThe complex object is:";

cout<<c1;

OUTPUT
Q34. Write a program to create a base class car containing the following member.

Protected: carname

Public: member functions to input & print out .

Create a derived class maruti containing the following members

Private: registrationfees public: readdata(), showdata()

In the main() function of the program make object of maruti class and ask the user to fill the data
and then display the data with showdata()

Sol: #include<iostream>

using namespace std;

class car

protected:

char carname[20];

char customername[30];

public:

void input();

void print_out();

};

class maruti:public car

int registrationfees;

public:

void readdata();

void showdata();

};

void car::input()

cout<<"\nEnter the Car name:";


cin>>carname;

void maruti::readdata()

cout<<"\nEnter the Customer Name:";

cin>>customername;

input();

cout<<"\nEnter the Registration fee:";

cin>>registrationfees;

void car:: print_out()

cout<<"\nThe Car name is:"<<carname;

void maruti::showdata()

cout<<"\n\nRegistration details";

cout<<"\n\nThe Customer Name is:"<<customername;

print_out();

cout<<"\nThe registration fee:"<<registrationfees;

int main()

maruti obj;

obj.readdata();

obj.showdata();
OUTPUT
Q35. An organization wishes to maintain a database of its employees. The database is divided into
a number of Classes whose relationship is shown below. The figure also show the minimum
information required for each class. Specify all the classes and define functions to create the
database and retrieve the individual information as and when required.

Sol:

#include<iostream>

using namespace std;

class staff

protected:

int empno; char name[30];

float basic;

};

class clerk:staff

char grade;

public:

void getdata();

void showdata();

};

void clerk::getdata()

cout<<"\n\n\t\tEnter the Details of clerk\n";

cout<<"\nEnter the Employee no:";

cin>>empno;

cout<<"\nEnter the Employee Name:";

cin>>name;

cout<<"\nEnter the basic salary:";

cin>>basic;
cout<<"\nEnter the grade:";

cin>>grade;

void clerk ::showdata()

cout<<"\n\n\t\tClerk Details";

cout<<"\nEmployee no:"<<empno;

cout<<"\nEmployee Name:"<<name;

cout<<"\nBasic salary:"<<basic;

cout<<"\nGrade of clerk Grade-"<<grade;

class officer:staff

char design[30];

public:

void getdata();

void showdata();

};

void officer::getdata()

cout<<"\n\n\t\tEnter the Details of officer\n";

cout<<"\nEnter the Employee no:";

cin>>empno;

cout<<"\nEnter the Employee Name:";

cin>>name;

cout<<"\nEnter the basic salary:";

cin>>basic;
cout<<"\nEnter the designation of officer:";

cin>>design;

void officer::showdata()

cout<<"\n\n\t\tOfficer Details";

cout<<"\nEmployee no:"<<empno;

cout<<"\nEmployee Name:"<<name;

cout<<"\nBasic salary:"<<basic;

cout<<"\nDesignation of the officer:"<<design;

int main()

officer one;

one.getdata();

clerk two;

two.getdata();

one.showdata();

two.showdata();

OUTPUT
Q36. Consider a class network of figure. The class master derives information from both account
and admin classes which in turn derive information from the class person. Define all the four
classes & Write a program to create, update & display the information contained in master objects
(Hybrid inheritance)

Person

Name

basic Account

Pay

Admin

Experience

Master

Name

Code

Pay

Sol:

#include <iostream>

using namespace std;

class Person

protected:

float basic;

char name[30];

};

class Account: virtual public Person

protected:

float pay;

};

class Admin : virtual public Person


{

protected:

int experience;

};

class Master : public Account , public Admin

protected:

int code;

public:

void getdata()

cout<<"\n\t\tEnter the details";

cout<<"\nEnter the code:";

cin>>code;

cout<<"\nEnter the name:";

cin>>name;

cout<<"\nEnter the basic pay:";

cin>>basic;

cout<<"\nEnter the salary:";

cin>>pay;

cout<<"\nEnter the years of experience:";

cin>>experience;

void Display();

void Update();

};

void Master::Display()
{

cout<<"\n\t\tDetails as follows";

cout<<"\nCode:"<<code;

cout<<"\nName:"<<name;

cout<<"\nBasic pay:"<<basic;

cout<<"\nSalary:"<<basic+pay;

cout<<"\nYears of experience:"<<experience;

void Master::Update()

int ch;

cout<<"\n\n\n\t\tUpdate the details";

cout<<"\n\n\tPress 1 for Edit Name\n\tPress 2 for Edit Code\n\tPress 3 for Edit Pay\n\tPress 4 for
Edit Experience";

cout<<"\n\t\tEnter the choice:";

cin>>ch;

if (ch == 1)

cout <<"\nEnter The Name:";

cin>>name;

else if (ch == 2)

cout<<"\nEnter The Code:";

cin>>code;

else if (ch == 3)

{
cout<<"\nEnter The Salary:";

cin>>pay;

else if (ch == 4)

cout<<"\nEnter The Experience:";

cin>>experience;

else

cout <<"\nInvalid";

int main()

Master obj;

obj.getdata();

obj.Display();

obj.Update();

obj.Display();

cout<<endl;

return 0;

OUTPUT
Q37. Consider a class shape. Use this class to store two double type values that could be used to
compute the area of figure. Derive two specific classes triangle and rectangle from the base shape.
Add to the base class a member function display_area() to compute & display the area of figures.
Make a display_area() as a virtual function & redefine this function in the derived classes to suit
their Requirements.

Sol:

#include<iostream>

using namespace std;

class shape

protected:

double l1,l2;

public:

virtual void display_area(){}

};

class triangle:public shape

public:

void getdata();

void display_area();

};

class rectangle:public shape

public:

void getdata();

void display_area();

};

void triangle::getdata()

{
cout<<"\nEnter the base of triangle:";

cin>>l1;

cout<<"\nEnter the height of the triangle:";

cin>>l2;

void triangle::display_area()

double res;

res=(l1*l2)/2;

cout<<"\nThe area of the triangle:"<<res;

void rectangle::getdata()

cout<<"\nEnter the Length of the rectangle:";

cin>>l1;

cout<<"\nEnter the width of the rectangle:";

cin>>l2;

void rectangle::display_area()

double res;

res=(l1*l2);

cout<<"\nThe area of the rectangle:"<<res<<"\n\n";

int main()

rectangle rec;
rec.getdata();

rec.display_area();

triangle tri;

tri.getdata();

tri.display_area();

OUTPUT
Q38. Write a program to take a rollno from the user and find that rollno in a text file which
contains all students record. If you find that record then delete the particular record from the file.

Sol:

#include<iostream>

#include<fstream>

using namespace std;

class student

int roll_no;

char name[40];

float marks;

public:

void getdata();

void display();

int getrno()

return roll_no;

};

void student :: getdata()

cout<<"\tEnter rollno:";

cin>>roll_no;

cout<<"\tEnter name:";

cin>>name;

cout<<"\tEnter the marks:";

cin>>marks;

cout<<"\n";
}

void student :: display()

cout<<"\tRoll no:"<<roll_no<<"\tName:"<<name<<"\tMarks:"<<marks<<"\n";

int main()

student info[6],s1,stud;

fstream my_file;

my_file.open("student.txt",ios::in|ios::app);

if(!my_file)

cout<<"Cannot open the file";

my_file.seekg(0);

cout<<"\n\tStudent details\n";

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

my_file.read((char*)&info[i],sizeof(info[i]));

info[i].display();

my_file.close();

ifstream fio("student.txt",ios::in);

ofstream file("temp.txt",ios::out);

int rno; char found='f',confirm='n';

cout<<"\n\tEnter the rollno to deleted:";

cin>>rno;
while(!fio.eof())

fio.read((char*)&s1,sizeof(s1));

if(s1.getrno()==rno)

s1.display();

found='t';

continue;

else

file.write((char*)&s1,sizeof(s1));

if(found=='f')

cout<<"Record is not found";

fio.close();

file.close();

remove("student.txt");

rename("temp.txt","student.txt");

fio.open("student.txt",ios::in);

cout<<"\n\tNow the file contains\n";

while(!fio.eof())

fio.read((char*)&stud,sizeof(stud));

if(fio.eof())

break;
stud.display();

fio.close();

OUTPUT
Q39. Write a program that reads a text file and create another file that is identical except that
every sequence of consecutive blank space is replaced by a single space. Printout both files.

Sol:

#include<iostream>

#include<fstream>

using namespace std;

int main()

string str="I am a BCA Student",s;

ofstream of1;

ifstream if1;

of1.open("File.txt",ios::out);

of1<<str;

of1.close();

if1.open("file.txt",ios::in);

while(!if1.eof())

if1>>str;

s+=str+ " ";

of1.open("file2.txt",ios::out);

of1<<s;

of1.seekp(0);

of1.close();

if1.close();

ifstream if2;

if2.open("file.txt",ios::in);

string line;
getline(if2,line);

cout<<"\n\t\tContent in first file with spaces:";

cout<<line;

if2.close();

ifstream if3;

if3.open("file2.txt",ios::in);

string line1;

getline(if3,line1);

cout<<"\n\n\t\tContent in temp file after removing extra spaces:";

cout<<line1;

if3.close();

remove("file.txt");

remove("file2.txt");

OUTPUT
Q40. A file contains a list of telephone numbers in the following form.

John 25551123

Ahmed 26521234

The name contains only one word. Write a program to read the file and output the list in two
columns. Use a class object to store each set of data.

Sol:

#include<iostream>

#include<fstream>

using namespace std;

class telephone

char name[50];

long phone;

public:

void getdata();

void display();

};

void telephone::getdata()

cout<<"\nEnter customer name:";

cin>>name;

cout<<"\nEnter phone no:";

cin>>phone;

void telephone::display()

cout<<"\n"<<name<<"\t"<<phone;

}
int main()

telephone tel[2];

fstream file;

file.open("data.txt",ios::in | ios:: out);

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

tel[i].getdata();

file.write((char*) &tel[i], sizeof(tel[i]));

file.seekg(0);

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

file.read((char*) &tel[i], sizeof(tel[i]));

tel[i].display();

file.close();

OUTPUT
Q41. Write a function template for finding the minimum value contained in an array.

Sol:

#include<iostream>

using namespace std;

template<class T>

T minimum(T a[],int size)

T min=a[0];

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

if(a[i]<min)

min=a[i];

return min;

int main()

int a[10],size,i,min1;

cout<<"\nEnter the size value:";

cin>>size;

cout<<"\nEnter the array elements:";

cout<<"\n";

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

cin>>a[i];
}

min1=minimum(a,size);

cout<<"\nThe minimum element in the array is:";

cout<<min1;

OUTPUT
Q42. Write a program with the following

a) A function to read two double type numbers from keyboard

b) A function to calculate the division of these two numbers

c) A try block to throw an exception when a wrong type is keyed in.

d) A try block to defect and throw an exception if the condition divide by zero occurs.

Sol:

#include<iostream>

using namespace std;

class doubledivison

double a,b;

public: void read();

void div();

};

void doubledivison :: read()

cout<<"\nEnter the first double values:";

cin>>a;

cout<<"\nEnter the second double values:";

cin>>b;

void doubledivison :: div()

try

if(cin.fail())

{
throw "Bad input!";

if(b==0)

throw 0;

cout<<"\nAns is "<<a/b;

catch(const int n)

cout << "\nDivision by " << n << " not allowed\n";

catch(const char* Str)

cout<<"\n"<<Str;

int main()

doubledivison one;

one.read();

one.div();

OUTPUT

You might also like