Psoop Lab Internal Codes
Psoop Lab Internal Codes
1. a)Write a C++ program to check whether the given number is palindrome or not using while loop.
#include <iostream>
using namespace std;
int main() {
int n, original, reversed = 0, remainder;
cout << "Enter a number: ";
cin >> n;
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
cout << original << " is a palindrome." << endl;
else
cout << original << " is not a palindrome." << endl;
return 0;
}
b)Construct an Abstract Data Type (ADT) named as “complex” with real, imag as private data members, default
constructor and parameterized constructor’s with one argument and two arguments to initialize the complex numbers
and proper accessor and mutator functions to find the difference of 2 complex numbers and display the result and also
destructor to destroy the objects created by constructor. Write a C++ program to display the difference between 2
complex numbers.
#include<iostream>
using namespace std;
class complex
{
float real,imag;
public:
complex()
{ real= 0.0; imag=0.0;}
complex(float x, float y)
{ real = x; imag = y;}
complex(float x)
{ real = imag = x;}
friend complex diff(complex, complex);
friend void show(complex);
~complex() { }
};
complex diff(complex c1,complex c2)
{
complex c3;
c3.real = c1.real-c2.real;
c3.imag = c1.imag-c2.imag;
return c3;}
void show(complex c)
{
cout<<c.real<<"+i"<<c.imag<<"\n";
}
int main(void)
{
float r,i;
cout<<"\nEnter first complex number:";
cin>>r>>i;
complex A(r,i);
complex B(2);
complex C ;
C = diff(A,B);
cout<<"A = ";
show(A);
cout<<"B = ";
show(B);
cout<<"C = ";
show(C);
return 0;
}
2. a)Write a C++ program to input an integer number and check whether the given number is Prime number or
composite number using “for” loop.
#include <iostream>
using namespace std;
int main() {
int num, flag = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << num << " is a prime number.";
else
cout << num << " is a composite number.";
return 0;
}
b)Write a C++ program to illustrate array of objects. Consider a complex class. Read ‘n’ complex numbers using suitable
constructors and display sum on ‘n’ complex numbers?
#include<iostream>
using namespace std;
int n;
class complex
{
int real,imag;
public:
complex():real(0),imag(0)
{}
complex(int x,int y):real(x),imag(y)
{}
void display(void);
friend complex add(complex );
~complex(){}
};
complex add(complex p)
{
static complex sum;
sum.real=sum.real+p.real;
sum.imag=sum.imag+p.imag;
return sum;
}
void complex::display(void)
{
cout<<real<<"+i"<<imag<<"\n";
}
int main(void) {
cout<<"\nEnter number of complex numbers:";
cin>>n;
int a,b;
complex q[20];
for(int i=0;i<n;i++)
{
cout<<"\nenter real and imag of complex number"<<i+1<<":";
cin>>a>>b;
q[i]=complex(a,b);
}
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<" Complex Number: ";
q[i].display();
}
complex res;
for(int i=0;i<n;i++)
{
res=add(q[i]);
}
cout<<"\nAfter Addition result is: ";
res.display();
return 0;
}
3. a)Write a C++ program to find the largest of 2 integer numbers, 2 floating point numbers, 2 characters and 3
integers by overloading the function getbig() with different signatures.
#include <iostream>
using namespace std;
int getbig(int a, int b) {
return (a > b) ? a : b;
}
float getbig(float a, float b) {
return (a > b) ? a : b;
}
char getbig(char a, char b) {
return (a > b) ? a : b;
}
int getbig(int a, int b, int c) {
int max = a;
if (b > max) max = b;
if (c > max) max = c;
return max;
}
int main() {
int int1, int2, int3;
float float1, float2;
char char1, char2;
cout << "Enter two integers: ";
cin >> int1 >> int2;
cout << "Enter two floating point numbers: ";
cin >> float1 >> float2;
cout << "Enter two characters: ";
cin >> char1 >> char2;
cout << "Enter three integers: ";
cin >> int1 >> int2 >> int3;
cout << "Largest of " << int1 << " and " << int2 << " (int): " << getbig(int1, int2) << endl;
cout << "Largest of " << float1 << " and " << float2 << " (float): " << getbig(float1, float2) << endl;
cout << "Largest of '" << char1 << "' and '" << char2 << "' (char): " << getbig(char1, char2) << endl;
cout << "Largest of " << int1 << ", " << int2 << ", and " << int3 << " (int): " << getbig(int1, int2, int3) << endl;
return 0;
}
b)Write a C++ program to perform the subtraction between two complex numbers by overloading the
binary subtraction operator (-). Operator function as a friend to the class “complex”?
#include<iostream>
using namespace std;
class complex
{
int real,imag;
public:
complex():real(0),imag(0)
{}
complex(int a,int b)
{
real=a; imag=b;
}
void display(void);
friend complex operator-(complex,complex);
};
complex operator-(complex x,complex y)
{
complex z;
z.real=x.real-y.real;
z.imag=x.imag-y.imag;
return z;
}
void complex::display(void)
{
cout<<real<<"+i"<<imag<<"\n";
}
int main(void)
{
int r,i;
cout<<"\nEnter the first complex number:";
cin>>r>>i;
complex A(r,i);
cout<<"\nEnter the second complex number:";
cin>>r>>i;
complex B(r,i);
complex C;
C=A-B;
A.display();
B.display();
cout<<"\nAfter subtraction the resultant complex number:";
C.display();
return 0;
}
4. a)Write a C++ program to decide the best pizza to buy, by providing the diameter and price of a circular
pizza and length, width, price of a rectangular pizza. Use “function overloading”.
#include<iostream>
using namespace std;
float unitprice(float, float);
float unitprice(float, float, float);
int main(void)
{
float dia, len, wid;
float pri_rou, pri_rect, unit_pri_rou, unit_pri_rect;
cout<<"Enter the diameter in inches of a Round Pizza:";
cin>>dia;
cout<<"\nEnter the length and width of the Rect angular pizza:";
cin>>len>>wid;
cout<<"\nEnter the Price of the Round Pizza:";
cin>>pri_rou;
cout<<"\nEnter the price of the Rectangular Pizza:";
cin>>pri_rect;
unit_pri_rou = unitprice(dia,pri_rou);
unit_pri_rect = unitprice(len, wid, pri_rect);
cout.precision(2);
cout<<"\nUnit price of the Round Pizza is :"<<unit_pri_rou;
cout<<"\nUnit price of the rectangle Pizza is:"<<unit_pri_rect;
if(unit_pri_rou<unit_pri_rect)
cout<<"\nThe Round Pizza is the best buy!";
else
cout<<"\nThe Rect angular Pizza is the best buy!";
return 0;
}
float unitprice(float d,float p)
{
float r = d/2;
float area = 3.1415*r*r;
return (p/area);
}
float unitprice(float l,float w,float p)
{
float area = l*w;
return (p/area);
}
b)Write a C++ program to display the product of 2 rational numbers by defining a class named as “rati”,
with num and den as private data members, getdata() , putdata() functions as pure member functions of
a class to initialize and display the objects (Rational numbers) and define a function mul() to perform the
multiplication between 2 rational numbers as normal function and make it as “friend” to the class “rati”.
#include <iostream>
using namespace std;
class rati {
private:
int num, den;
public:
void getdata() {
cout << "Enter numerator and denominator of the rational number: ";
cin >> num >> den;
}
void putdata() {
cout << "Rational number: " << num << "/" << den << endl;
}
friend rati mul(const rati& r1, const rati& r2);
};
rati mul(const rati& r1, const rati& r2) {
rati result;
result.num = r1.num * r2.num;
result.den = r1.den * r2.den;
return result;
}
int main() {
rati rational1, rational2, product;
cout << "Enter data for the first rational number:\n";
rational1.getdata();
cout << "Enter data for the second rational number:\n";
rational2.getdata();
product = mul(rational1, rational2);
cout << "Product of the two rational numbers:\n";
product.putdata();
return 0;
}
5. a)Write a C++ program to read decimal integer and convert it into octal and display the result without
using format specification %o using do – while loop.
#include <iostream>
using namespace std;
int main() {
int decimal, remainder, octal = 0, place = 1;
cout << "Enter a decimal integer: ";
cin >> decimal;
do {
remainder = decimal % 8;
octal += remainder * place;
place *= 10;
decimal /= 8;
} while (decimal != 0);
cout << "Octal equivalent: " << octal << endl;
return 0;
}
b)Write a C++ program to overload binary addition (+) operator to perform the addition between two
complex numbers?
#include<iostream>
using namespace std;
class complex
{
int real,imag;
public:
complex():real(0),imag(0){ }
complex(int a,int b):real(a),imag(b){ }
void display(void);
complex operator+(complex );
};
complex complex::operator+(complex x)
{
complex z;
z.real= this->real + x.real;
z.imag= this->imag + x.imag;
return z;
}
void complex::display(void)
{
cout<<real<<"+i"<<imag<<"\n";
}
int main(void)
{
int r,i;
cout<<"\nEnter the first complex number:";
cin>>r>>i;
complex A(r,i);
cout<<"\nEnter the second complex number:";
cin>>r>>i;
complex B(r,i);
complex C;
C=A+B;
A.display();
B.display();
cout<<"\nAfter addition the resultant complex number:";
C.display();
return 0;
}
6. a)Write a C++ program two read two integer numbers, perform any arithmetic operation (addition,
subtraction, multiplication, division) by user’s choice at run time.
#include <iostream>
using namespace std;
int main() {
int num1, num2;
char op;
switch (op) {
case '+':
cout << "Result of addition: " << num1 + num2 << endl;
break;
case '-':
cout << "Result of subtraction: " << num1 - num2 << endl;
break;
case '*':
cout << "Result of multiplication: " << num1 * num2 << endl;
break;
case '/':
cout << "Result of division: " << num1/ num2 << endl;
break;
default:
cout << "Invalid arithmetic operation!" << endl;
}
return 0;
}
b)Write a C++ program to perform the addition between ‘n’ number of complex numbers by allocating
memory at run time?
#include <iostream>
using namespace std;
class Complex {
private:
int real;
int imag;
public:
Complex(int r = 0, int i = 0) : real(r), imag(i) {}
void add(Complex num) {
real += num.real;
imag += num.imag;
}
void display() {
cout << "(" << real << ") + (" << imag << "i)" << endl;
}
};
int main() {
int n;
cout << "Enter the number of complex numbers to add: ";
cin >> n;
Complex *num = new Complex[n];
for (int i = 0; i < n; ++i) {
int real, imag;
cout << "Enter real and imaginary parts of complex number " << i + 1 << ": ";
cin >> real >> imag;
num[i] = Complex(real, imag);
}
Complex result;
for (int i = 0; i < n; ++i) {
result.add(num[i]);
}
cout << "Sum of " << n << " complex numbers: ";
result.display();
delete[] num;
return 0;
}
7. a) Write a C++ program to find the GCD of two numbers using Recursion.
#include<iostream>
using namespace std;
int findgcd(int,int);
int main(void)
{
int n1,n2,a,b,gcd;
cout<<"Enter two integers:";
cin>>n1>>n2;
if(n1<n2)
{
a=n2;
b=n1;
}
else
{
a=n1;
b=n2;
}
gcd = findgcd(a,b);
cout<<"\nGCD of "<<n1<<","<<n2<<"is "<<gcd;
return 0;
}
Int findgcd(int x,int y)
{
int rem;
rem = x%y;
if(rem==0)
{
return y;
}
else
{
return findgcd(y,rem);
}
}
b)“An outside function can be friend to more than one class”, write C++ program to justify the statement? Consider class
“first” for first number and class “second” for second number, define exchange() function which is friend to both classes
having first argument as of type “first” and second argument as of type “second”, it performs the swapping. Also
observe the forward declaration of the class?
#include<iostream>
using namespace std;
class second; //Forward Declaration
class first
{
int val1;
public:
void indata(int a)
{
val1=a;
}
void display(void)
{
cout<<val1<<endl;
}
friend void exchange(first &, second &);
};
class second
{
int val2;
public:
void indata(int a)
{
val2=a;
}
void display(void)
{
cout<<val2<<endl;
}
friend void exchange(first &, second &);
}
void exchange(first & x, second & y)
{
int temp = x.val1;
x.val1 = y.val2;
y.val2 = temp;
}
int main(void)
{
first c1;
second c2;
c1.indata(100);
c2.indata(200);
cout<<"\nBefore swapping:";
c1.display();
c2.display();
exchange(c1,c2);
cout<<"\nAfter swapping:";
c1.display();
c2.display();
return 0;
}
8. Write a C++ program to display all the perfect numbers of a certain range given by the user
#include <iostream>
using namespace std;
int perfect(int num) {
int sum = 1;
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) {
if (i * i != num) {
sum += i + num / i;
} else {
sum += i;
}
}
}
return (sum == num && num != 1)? num : 0;
}
int main() {
int start, end;
cout << "Enter the range (start and end): ";
cin >> start >> end;
cout << "Perfect numbers within the range [" << start << ", " << end << "] are:" << endl;
for (int i = start; i <= end; ++i) {
if (perfect(i)) {
cout << i << endl;
}
}
return 0;
}
b) Write a C++ program to decrement the real and imaginary parts of a complex number by 1 by overloading the unary
decrement operator (--). Operator function as a friend to a class.
#include<iostream>
using namespace std;
class complex {
private:
int real, imag;
public:
complex() : real(0), imag(0) {}
complex(int r, int i) : real(r), imag(i) {}
void display();
friend void operator--(complex &c);
};
void complex::display() {
cout << this->real << "+i" << this->imag;
}
void operator--(complex &c) {
c.real--;
c.imag--;
}
int main(void) {
int x, y;
cout << "Enter real and imag of complex number: ";
cin >> x >> y;
complex C(x, y);
cout << "\nBefore decrement operation: ";
C.display();
--C;
cout << "\nAfter decrement operation: ";
C.display();
return 0;
}
9. a)Write a C++ program to find the area of different shapes square, rectangle, triangle and trapezoid by overloading
the function area() with different signatures.
#include <iostream>
#include <cmath>
using namespace std;
float area(float side) {
return side * side;
}
float area(float length, float width) {
return length * width;
}
float area(float base, float height) {
return 0.5 * base * height;
}
float area(float base1, float base2, float height) {
return 0.5 * (base1 + base2) * height;
}
int main() {
char shape;
float side, length, width, base, height, base1, base2;
cout << "Choose a shape (s for square, r for rectangle, t for triangle, z for trapezoid): ";
cin >> shape;
switch (shape) {
case 's':
cout << "Enter the side length of the square: ";
cin >> side;
cout << "Area of the square: " << area(side) << endl;
break;
case 'r':
cout << "Enter the length and width of the rectangle: ";
cin >> length >> width;
cout << "Area of the rectangle: " << area(length, width) << endl;
break;
case 't':
cout << "Enter the base and height of the triangle: ";
cin >> base >> height;
cout << "Area of the triangle: " << area(base, height) << endl;
break;
case 'z':
cout << "Enter the two bases and height of the trapezoid: ";
cin >> base1 >> base2 >> height;
cout << "Area of the trapezoid: " << area(base1, base2, height) << endl;
break;
default:
cout << "Invalid shape!" << endl;
}
return 0;
}
b) Write a C++ program to allocate memory at run time for one dimensional integer array in a constructor, arrange the
data values in ascending order using bubble sort algorithm
#include <iostream>
using namespace std;
class BubbleSort {
private:
int *arr;
int size;
public:
BubbleSort(int n) {
size = n;
arr = new int[size];
cout << "Enter " << size << " integers: ";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
}
void bubbleSort() {
for (int i = 0; i < size - 1; ++i) {
for (int j = 0; j < size - i - 1; ++j) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}
void display() {
cout << "Sorted array: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
~BubbleSort() {
delete[] arr;
}
};
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
BubbleSort arr(n);
arr.bubbleSort();
arr.display();
return 0;
}
10. a) Write a C++ program to display the hallow right angle triangle with ‘*’ symbol as boarder
#include <iostream>
using namespace std;
int main() {
int rows;
cout << "Enter the number of rows: ";
cin >> rows;
for (int i = 1; i <= rows; ++i) {
for (int j = 1; j <= i; ++j) {
if (i == 1 || j == i || j == 1) {
cout << "* ";
} else {
cout << " ";
}
}
cout << endl;
}
return 0;
}
b) Write a C++ program to display the total marks obtained by the student in class “test” of 3 subjects and under sports
quota using “hybrid inheritance”. Derive the class “result” from class “test” which is having 3 subject marks as members
and class “sports” which is having the sports quota marks as member and derive the class “test” from class ”student”
which is having the roll number as member
#include<iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a): roll_number(a){}
void put_number(void)
{
cout<<"Roll No: "<<roll_number<<"\n";
}
};
class test : public student
{
protected:
float part1, part2;
public:
void get_marks(float x, float y) : part1(x),part2(y){}
void put_marks(void)
{
cout<<"Marks Obtained: "<<"\n";
cout<<"Part1 = "<<part1<<"\n";
cout<<"Part2 = "<<part2<<"\n";
}
};
class sports
{
protected:
float score;
public:
void get_score(float s): score(s){}
void put_score(void)
{
cout<<"Sports Weithtage: "<<score<<"\n";
}
};
class result : public test, public sports
{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = part1 + part2 + score;
put_number();
put_marks();
put_score();
cout<<"Total Score: "<<total<<"\n";
}
int main(void)
{
result student1;
student1.get_number(99);
student1.get_marks(27.5,33.0);
student1.get_score(6.9);
student1.display();
return 0;
}
11. Write a C++ program to input the angle in degrees and display the sine value using the series expansion
#include <iostream>
#define PI 3.1415
using namespace std;
int main() {
float xdeg, xrad, sum = 0.0, num;
int n, k = 2, sign = 1, i;
long int den;
cout << "\nEnter the phase angle in degrees: ";
cin >> xdeg;
cout << "\nEnter the number of terms: ";
cin >> n;
xrad = xdeg * (PI / 180.0);
num = xrad;
den = 1;
for (i = 1; i <= n; i++) {
sum = sum + ((num / den) * sign);
sign = sign * -1;
num = num * xrad * xrad;
den = den * k * (k + 1);
k = k + 2;
}
cout << "\n\tsin(" << xdeg << ") = " << sum << endl;
return 0;
}
b) Construct an Abstract Data Type (ADT) named as “complex” with real, imag as private data members, default
constructor and parameterized constructor’s with one argument and two arguments to initialize the complex numbers
and proper accessor and mutator functions to find the difference of 2 complex numbers and display the result and also
destructor to destroy the objects created by constructor. Write a C++ program to display the multiplication between 2
complex numbers.
#include<iostream>
using namespace std;
class Complex {
private:
float real;
float imag;
public:
Complex() : real(0), imag(0) {}
Complex(float r) : real(r), imag(0) {}
Complex(float r, float i) : real(r), imag(i) {}
Complex multiply(Complex num) {
float r = real * num.real - imag * num.imag;
float i = real * num.imag + imag * num.real;
return Complex(r, i);
}
void display() {
cout << "(" << real << ") + (" << imag << "i)" << endl;
}
~Complex() {}
};
int main() {
float real1, imag1, real2, imag2;
cout << "Enter real and imaginary parts of first complex number: ";
cin >> real1 >> imag1;
cout << "Enter real and imaginary parts of second complex number: ";
cin >> real2 >> imag2;
Complex num1(real1, imag1);
Complex num2(real2, imag2);
cout << "Multiplication of the two complex numbers: ";
Complex result = num1.multiply(num2);
result.display();
return 0;
}
12. Define a function Com_Int(), with principle amount, time as normal arguments and Rate of Interest as “default
argument”. Write a C++ program to find the Compound Interest for a account holder
#include <iostream>
#include <cmath>
using namespace std;
float Com_Int(float principle, float time, float rate = 0.05) {
return principle * pow(1 + rate, time) - principle;
}
int main() {
float principle, time, rate;
cout << "Enter principle amount: ";
cin >> principle;
cout << "Enter time (in years): ";
cin >> time;
cout << "Compound interest with default rate (5%): " << Com_Int(principle, time) << endl;
cout << "Enter rate of interest (in decimal): ";
cin >> rate;
cout << "Compound interest with custom rate: " << Com_Int(principle, time, rate) << endl;
return 0;
}
b) Write a C++ program to perform the multiplication between 2 rational numbers using constructor overloading.
#include <iostream>
using namespace std;
class Rational {
private:
int numerator;
int denominator;
public:
Rational() : numerator(0), denominator(1) {}
Rational(int num, int denom) : numerator(num), denominator(denom) {}
Rational multiply(Rational num) {
Rational result;
result.numerator = numerator * num.numerator;
result.denominator = denominator * num.denominator;
return result;
}
void display() {
cout << numerator << "/" << denominator;
}
};
int main() {
int num1, denom1, num2, denom2;
cout << "Enter numerator and denominator of first rational number: ";
cin >> num1 >> denom1;
cout << "Enter numerator and denominator of second rational number: ";
cin >> num2 >> denom2;
Rational r1(num1, denom1);
Rational r2(num2, denom2);
Rational result = r1.multiply(r2);
cout << "Result of multiplication: ";
result.display();
cout << endl;
return 0;
}
13. a) Write a C++ program to read today’s date, display the yesterday’s date in dd-mm-yyyy format.
#include <iostream>
using namespace std;
int main() {
int d, m, y, leap;
cout << "Enter today's date (format: day month year): ";
cin >> d >> m >> y;
leap = (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
if (d == 1 && m == 1) {
d = 31;
m = 12;
y--;
} else if (leap == 1 && m == 3 && d == 1) {
d = 29;
m--;
} else if (leap == 0 && m == 3 && d == 1) {
d = 28;
m--;
} else if (d == 1 && (m == 2 || m == 4 || m == 6 || m == 8 || m == 9 || m == 11)) {
d = 31;
m--;
} else if (d == 1 && (m == 5 || m == 7 || m == 10 || m == 12)) {
d = 30;
m--;
} else {
d--;
}
cout << "Yesterday's Date is " << d << "-" << m << "-" << y << endl;
return 0;
}
b)Write a C++ program to illustrate the concept of Virtual Base Class
#include<iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int a)
{
roll_number = a;
}
void put_number(void)
{
cout<<"Roll No: "<<roll_number<<"\n";
}
};
class test : virtual public student
{
protected:
float part1, part2;
public:
void get_marks(float x, float y)
{
part1 = x; part2 = y;
}
void put_marks(void)
{
cout<<"Marks Obtained: "<<"\n";
cout<<"Part1 = "<<part1<<"\n";
cout<<"Part2 = "<<part2<<"\n";
}
};
class sports : public virtual student
{
protected:
float score;
public:
void get_score(float s)
{
score = s;
}
void put_score(void)
{
cout<<"Sports Weithtage: "<<score<<"\n";
}
};
class result : public test, public sports
{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = part1 + part2 + score;
put_number();
put_marks();
put_score();
cout<<"Total Score: "<<total<<"\n";
}
int main(void)
{
result student1;
student1.get_number(99);
student1.get_marks(27.5,33.0);
student1.get_score(6.9);
student1.display();
return 0;
}
14.a) Write a C++ program to display the GCD of given two integers?
#include <iostream>
using namespace std;
int gcd(int a, int b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
int main() {
int num1, num2;
cout << "Enter two integers: ";
cin >> num1 >> num2;
int result = gcd(num1, num2);
cout << "GCD of " << num1 << " and " << num2 << " is " << result << endl;
return 0;
}
b) Write a C++ program to perform the subtraction between two complex numbers by overloading the binary
subtraction operator (-). Operator function as a friend to the class “complex”?
#include<iostream>
using namespace std;
class complex
{
int real,imag;
public:
complex():real(0),imag(0)
{}
complex(int a,int b)
{
real=a; imag=b;
}
void display(void);
friend complex operator-(complex,complex);
};
complex operator-(complex x,complex y)
{
complex z;
z.real=x.real-y.real;
z.imag=x.imag-y.imag;
return z;
}
void complex::display(void){
cout<<real<<”+i”<<imag<<endl;
}
int main(void)
{
int r,i;
cout<<"\nEnter the first complex number:";
cin>>r>>i;
complex A(r,i);
cout<<"\nEnter the second complex number:";
cin>>r>>i;
complex B(r,i);
complex C;
C=A-B;
A.display();
B.display();
cout<<"\nAfter subtraction the resultant complex number:";
C.display();
return 0;
}
15. a) Write a C++ program to allocate memory at run time for one dimensional integer array in a constructor, arrange
the data values in ascending order using selection sort algorithm.
#include <iostream>
using namespace std;
class SelectionSort {
private:
int *arr;
int size;
public:
SelectionSort(int n) {
size = n;
arr = new int[size];
cout << "Enter " << size << " integers: ";
for (int i = 0; i < size; ++i) {
cin >> arr[i];
}
}
void selectionSort() {
for (int i = 0; i < size - 1; ++i) {
int minIndex = i;
for (int j = i + 1; j < size; ++j) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
if (minIndex != i) {
int temp = arr[i];
arr[i] = arr[minIndex];
arr[minIndex] = temp;
}
}
}
void display() {
cout << "Sorted array: ";
for (int i = 0; i < size; ++i) {
cout << arr[i] << " ";
}
cout << endl;
}
~SelectionSort() {
delete[] arr;
}
};
int main() {
int n;
cout << "Enter the size of the array: ";
cin >> n;
SelectionSort obj(n);
obj.selectionSort();
obj.display();
return 0;
}
b)Write a C++ program to overload binary addition (+) operator to perform the addition between two
complex numbers?
#include<iostream>
using namespace std;
class complex
{
int real,imag;
public:
complex():real(0),imag(0){ }
complex(int a,int b):real(a),imag(b){ }
void display(void);
complex operator+(complex );
};
complex complex::operator+(complex x)
{
complex z;
z.real= this->real + x.real;
z.imag= this->imag + x.imag;
return z;
}
void complex::display(void)
{
cout<<real<<"+i"<<imag<<"\n";
}
int main(void)
{
int r,i;
cout<<"\nEnter the first complex number:";
cin>>r>>i;
complex A(r,i);
cout<<"\nEnter the second complex number:";
cin>>r>>i;
complex B(r,i);
complex C;
C=A+B;
A.display();
B.display();
cout<<"\nAfter addition the resultant complex number:";
C.display();
return 0;
}
16. Write a C++ program to check whether the given number is palindrome or not using while loop.
#include <iostream>
using namespace std;
int main() {
int n, original, reversed = 0, remainder;
cout << "Enter a number: ";
cin >> n;
original = n;
while (n != 0) {
remainder = n % 10;
reversed = reversed * 10 + remainder;
n /= 10;
}
if (original == reversed)
cout << original << " is a palindrome." << endl;
else
cout << original << " is not a palindrome." << endl;
return 0;
}
b) Write a C++ program to illustrate Multi level inheritance.
#include<iostream>
using namespace std;
class student
{
protected:
int roll_number;
public:
void get_number(int);
void put_number(void);
};
void student :: get_number(int a)
{
roll_number = a;
}
void student :: put_number(void)
{
cout<<"Roll Number: "<<roll_number<<"\n";
}
class test : public student
{
protected:
float sub1, sub2;
public:
void get_marks(float , float);
void put_marks(void);
};
void test :: get_marks(float x, float y)
{
sub1 = x; sub2 = y;
}
void test :: put_marks(void)
{
cout<<"Marks in SUB1 = "<<sub1<<"\n";
cout<<"Marks in SUB2 = "<<sub2<<"\n";
}
class result : public test
{
float total;
public:
void display(void);
};
void result :: display(void)
{
total = sub1 + sub2;
put_number();
put_marks();
cout<<"Total = "<<total<<"\n";
}
int main(void)
{
result student1;
student1.get_number(99);
student1.get_marks(75.0, 59.5);
student1.display();
return 0;
}
17. a) Write a C++ program to input an integer number and check whether the given number is Prime number or
composite number using “for” loop.
#include <iostream>
using namespace std;
int main() {
int num, flag = 0;
cout << "Enter a positive integer: ";
cin >> num;
for (int i = 2; i <= num / 2; ++i) {
if (num % i == 0) {
flag = 1;
break;
}
}
if (flag == 0)
cout << num << " is a prime number.";
else
cout << num << " is a composite number.";
return 0;
}
b) Write a C++ program to illustrate array of objects. Consider a complex class. Read ‘n’ complex numbers using suitable
constructors and display sum on ‘n’ complex numbers?
#include<iostream>
using namespace std;
int n;
class complex
{
int real,imag;
public:
complex():real(0),imag(0)
{}
complex(int x,int y):real(x),imag(y)
{}
void display(void);
friend complex add(complex );
~complex(){}
};
complex add(complex p)
{
static complex sum;
sum.real=sum.real+p.real;
sum.imag=sum.imag+p.imag;
return sum;
}
void complex::display(void)
{
cout<<real<<"+i"<<imag<<"\n";
}
int main(void) {
cout<<"\nEnter number of complex numbers:";
cin>>n;
int a,b;
complex q[20];
for(int i=0;i<n;i++)
{
cout<<"\nenter real and imag of complex number"<<i+1<<":";
cin>>a>>b;
q[i]=complex(a,b);
}
for(int i=0;i<n;i++)
{
cout<<"\n"<<i+1<<" Complex Number: ";
q[i].display();
}
complex res;
for(int i=0;i<n;i++)
{
res=add(q[i]);
}
cout<<"\nAfter Addition result is: ";
res.display();
return 0;
}
18. a) Write a C++ program to read decimal integer and convert it into octal and display the result without using format
specification %o using do – while loop.
#include <iostream>
using namespace std;
int main() {
int decimal, remainder, octal = 0, place = 1;
cout << "Enter a decimal integer: ";
cin >> decimal;
do {
remainder = decimal % 8;
octal += remainder * place;
place *= 10;
decimal /= 8;
} while (decimal != 0);
cout << "Octal equivalent: " << octal << endl;
return 0;
}
b) Write a C++ program to illustrate the “copy constructor” ?
#include<iostream>
using namespace std;
class complex
{
float real,imag;
public:
complex()
{ real= 0.0; imag=0.0;}
complex(float x, float y)
{ real = x; imag = y;}
complex(complex & Z)
{ real = Z.real;
imag = Z.imag; }
friend complex sum(complex, complex);
friend void show(complex);
~complex() {}
};
complex sum(complex c1,complex c2)
{ complex c3;
c3.real = c1.real+c2.real;
c3.imag = c1.imag+c2.imag;
return c3;}
void show(complex c)
{cout<<c.real<<"+i"<<c.imag<<"\n"; }
int main(void)
{
float r,i;
cout<<"\nEnter first complex number:";
cin>>r>>i;
complex A(r,i);
complex B(A);
complex C;
C = sum(A,B);
cout<<"A = ";
show(A);
cout<<"B = ";
show(B);
cout<<"Result = ";
show(C);
return 0;
}