21. Create a class that represents a complex number with methods to add and multiply complex numbers.
Required Input:
Complex c1(2, 3); Complex c2(4, 5);
Expected Output:
Sum: 6 + 8i
Product: -7 + 22i
Code In Cpp
#include <iostream>
class Complex {
private:
double real, imag;
public:
Complex(double r, double i);
Complex add(Complex other);
Complex multiply(Complex other);
void display();
};
int main() {
// Logic to demonstrate Complex functionality
return 0;
}
Run Code?
Click Run Button to view compiled output
22. Write a program to reverse a string using a stack data structure.
Required Input:
std::string str = "Hello";
Expected Output:
Reversed string: olleH
Code In Cpp
#include <iostream>
#include <stack>
void reverseString(std::string str);
int main() {
// Logic to demonstrate string reversal
return 0;
}
Run Code?
Click Run Button to view compiled output
23. Write a program to rotate an array to the right by a given number of positions.
Required Input:
int arr[] = {1, 2, 3, 4, 5}; int k = 2;
Expected Output:
Rotated array: 4 5 1 2 3
Code In Cpp
#include <iostream>
void rotateArray(int arr[], int size, int k);
int main() {
// Logic to demonstrate array rotation
return 0;
}
Run Code?
Click Run Button to view compiled output
24. Write a program to implement a simple binary search on a sorted array.
Required Input:
int arr[] = {1, 3, 5, 7, 9}; int target = 5;
Expected Output:
Element found at index: 2
Code In Cpp
#include <iostream>
int binarySearch(int arr[], int size, int target);
int main() {
// Logic to demonstrate binary search
return 0;
}
Run Code?
Click Run Button to view compiled output
25. Create a program to check if a given string is a palindrome.
Required Input:
std::string str = "madam";
Expected Output:
madam is a palindrome.
Code In Cpp
#include <iostream>
bool isPalindrome(std::string str);
int main() {
// Logic to demonstrate palindrome checking
return 0;
}
Run Code?
Click Run Button to view compiled output
26. Implement a program to find the intersection of two arrays.
Required Input:
int arr1[] = {1, 2, 3}; int arr2[] = {2, 3, 4};
Expected Output:
Intersection: 2 3
Code In Cpp
#include <iostream>
#include <unordered_set>
void findIntersection(int arr1[], int size1, int arr2[], int size2);
int main() {
// Logic to demonstrate intersection finding
return 0;
}
Run Code?
Click Run Button to view compiled output
27. Write a program to sort an array using the bubble sort algorithm.
Required Input:
int arr[] = {5, 2, 9, 1};
Expected Output:
Sorted array: 1 2 5 9
Code In Cpp
#include <iostream>
void bubbleSort(int arr[], int size);
int main() {
// Logic to demonstrate bubble sort
return 0;
}
Run Code?
Click Run Button to view compiled output
28. Create a program to implement a simple text-based banking system with deposit and withdrawal functions.
Required Input:
Account account; account.deposit(100);
Expected Output:
Balance: 100
Balance after withdrawal: 50
Code In Cpp
#include <iostream>
class Account {
private:
double balance;
public:
Account();
void deposit(double amount);
void withdraw(double amount);
double getBalance();
};
int main() {
// Logic to demonstrate banking functions
return 0;
}
Run Code?
Click Run Button to view compiled output
29. Implement a program to calculate the power of a number using recursion.
Required Input:
int base = 2; int exp = 3;
Expected Output:
2 raised to the power 3: 8
Code In Cpp
#include <iostream>
int power(int base, int exp);
int main() {
// Logic to demonstrate power calculation
return 0;
}
Run Code?
Click Run Button to view compiled output
30. Create a program to read and display a 2D matrix.
Required Input:
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
Expected Output:
1 2 3
4 5 6
Code In Cpp
#include <iostream>
void displayMatrix(int matrix[][3], int rows, int cols);
int main() {
// Logic to demonstrate matrix display
return 0;
}
Run Code?
Click Run Button to view compiled output