C++ Codes
C++ Codes
public:
Complex(int r = 0, int i = 0)
{
real = r;
imag = i;
}
void print()
{
cout << real << " + i" << imag << '\n';
}
};
int main()
{
Complex c1(10, 5), c2(10, 5);
if (c1 == c2)
{
cout << "The complex numbers are equal.\n";
}
else
{
cout << "The complex numbers are not equal.\n";
}
return 0;
}
public:
// Constructor to initialize the number
PrimeChecker(int n)
{
number = n;
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
PrimeChecker primeObj(num);
if (primeObj.isPrime())
cout << "The number is prime.\n";
else
cout << "The number is not prime.\n";
return 0;
}
int main() {
string input;
cout << "Enter a string: ";
getline(cin, input);
cout << "Reversed string: ";
for (int i = input.length() - 1; i >= 0; i--) {
cout << input[i];
}
return 0;
}
int main()
{
string str;
cout << "Enter a string: ";
cin >> str;
Palindrome(str);
return 0;
}
int main() {
float principal, rate, time;
cout << "Enter principal amount: ";
cin >> principal;
cout << "Enter rate of interest: ";
cin >> rate;
cout << "Enter time (in years): ";
cin >> time;
return 0;
}
void print() { cout << "Result is: " << num << endl; }
};
int main()
{
int num1, num2;
cout << "Enter first number: ";
cin >> num1;
cout << "Enter second number: ";
cin >> num2;
8)WAP to overload ' << ' and ' >> ' operator.
Code:-
#include<iostream>
using namespace std;
class Simple
{
public:
int num;
};
int main()
{
Simple s;
cout << "Enter a number: ";
cin >> s;
cout << "The number is: " << s;
return 0;
}
int main()
{
int num;
cout << "Enter a number: ";
cin >> num;
if(Armstrong(num))
cout << num << " is an Armstrong number.";
else
cout << num << " is not an Armstrong number.";
return 0;
}
int main()
{
int a, b, c;
cout << "Enter three numbers: ";
cin >> a >> b >> c;
cout << "The maximum of the three numbers is: " << max << endl;
return 0;
}
int main()
{
MyClass obj;
cout << "Accessing private member of MyClass from main: " <<
obj.privateVar << endl;
return 0;
}
try {
if(num2 == 0)
{
throw "Division by zero condition!";
}
result = (double)num1 / num2;
cout << "The result is: " << result << endl;
}
catch(const char* msg)
{
cerr << "Error: " << msg << endl;
}
return 0;
}
12)print the following pattern using CONSTRUCTOR
1
23
456
78910
Code:-
#include <iostream>
using namespace std;
class Pattern
{
int counter;
public:
Pattern() : counter(1) {} // Constructor initializes counter to 1
void printPattern(int n)
{
for(int i = 1; i <= n; ++i)
{
for(int j = 1; j <= i; ++j)
{
cout << counter++;
}
cout << endl;
}
}
};
int main()
{
Pattern p;
int n;
cout << "Enter the number of lines for the pattern: ";
cin >> n;
p.printPattern(n); // Prints the pattern up to n lines
return 0;
}
// Copy constructor
Factorial(Factorial &f)
{
num = f.num;
fact = f.fact;
}
void display()
{
cout << "Factorial of " << num << " is " << fact << endl;
}
};
int main()
{
int number;
cout << "Enter a number : ";
cin >> number;
return 0;
}
return 0;
}
return 0;
}
if(num % 2 == 0)
{
cout << num << " is even." << endl;
cout << "Square root of " << num << " is " << sqrt(num);
}
else
{
cout << num << " is odd.";
}
return 0;
}
17)Write a program to accept two numbers from user and display sum
of two numbers using constructor.
Code:-
#include<iostream>
using namespace std;
class Sum
{
int num1, num2, sum;
public:
// Constructor to initialize and calculate sum
Sum(int n1, int n2)
{
num1 = n1;
num2 = n2;
sum = num1 + num2;
}
void display()
{
cout << "Sum of " << num1 << " and " << num2 << " is " << sum <<
endl;
}
};
int main()
{
int number1, number2;
return 0;
}
int main() {
int num1 = 16;
double num2 = 15.5;
return 0;
}
// Constructor
Number(int n = 0) : num(n) {}
return 0;
}
int main()
{
string input;
cout << "Enter a string: ";
cin >> input;
if(Palindrome(input))
{
cout << "\"" << input << "\" is a palindrome." << endl;
}
else
{
cout << "\"" << input << "\" is not a palindrome." << endl;
}
return 0;
}
// Base class
class Animal
{
public:
void eat()
{
cout << "Eating..." << endl;
}
};
// Derived class
class animal : public Animal
{
public:
void breathe()
{
cout << "Breathing..." << endl;
}
};
// Derived class
class Dog : public animal
{
public:
void bark()
{
cout << "Barking..." << endl;
}
};
int main()
{
Dog d;
d.eat(); // Inherited from Animal
d.breathe(); // Inherited from animal
d.bark(); // Own function
return 0;
}
class FactorialCalculator
{
public:
// Function to calculate factorial
unsigned long long calculate(int n)
{
if (n <= 1) return 1;
return n * calculate(n - 1);
}
};
int main()
{
FactorialCalculator fc;
int number;
cout << "Factorial of " << number << " is " << fc.calculate(number) <<
endl;
return 0;
}
int main() {
string str1, str2;
return 0;
}
int main() {
float radius;
return 0;
}
class Fibonacci
{
public:
Fibonacci(int n)
{
int a = 0, b = 1, c;
cout << "Fibonacci Series: ";
if (n >= 1) cout << a << " ";
if (n >= 2) cout << b << " ";
for(int i = 3; i <= n; ++i)
{
c = a + b;
cout << c << " ";
a = b;
b = c;
}
cout << endl;
}
};
int main() {
int number;
cout << "Enter the number of terms for Fibonacci Series: ";
cin >> number;
Fibonacci fib(number);
return 0;
}