Let’s discuss 9 important C++ programs with complex logic and explanation.
C++ Programs : 1 Fibonacci Series
#include <iostream>
int main() {
int n = 10;
long long a = 0, b = 1;
std::cout << "Fibonacci Series up to " << n << " terms: ";
for (int i = 1; i <= n; ++i) {
std::cout << a << " "; // Print the current Fibonacci number
long long next = a + b; // Calculate the next Fibonacci number
a = b;
b = next;
}
return 0;
}
Output:
Fibonacci Series up to 10 terms: 0 1 1 2 3 5 8 13 21 34
C++ Programs : 2 Prime Number Checker
#include <iostream>
bool isPrime(int num) {
if (num <= 1) return false; // Check for numbers less than or equal to 1
for (int i = 2; i * i <= num; ++i) {
if (num % i == 0) return false; // Check for factors
}
return true;
}
int main() {
int num = 17;
if (isPrime(num)) {
std::cout << num << " is a prime number.";
} else {
std::cout << num << " is not a prime number.";
}
return 0;
}
Output:
17 is a prime number.
C++ Programs : 3 Palindrome Checker
#include <iostream>
#include <string>
#include <algorithm>
bool isPalindrome(const std::string& str) {
std::string reversed = str;
std::reverse(reversed.begin(), reversed.end()); // Reverse the string
return str == reversed; // Check if it's a palindrome
}
int main() {
std::string word = "racecar";
if (isPalindrome(word)) {
std::cout << word << " is a palindrome.";
} else {
std::cout << word << " is not a palindrome.";
}
return 0;
}
Output:
racecar is a palindrome.
C++ Programs : 4 Factorial Calculation
#include <iostream>
int factorial(int n) {
if (n <= 1) return 1; // Base case: factorial of 0 and 1 is 1
return n * factorial(n - 1); // Recursively calculate the factorial
}
int main() {
int num = 5;
std::cout << "Factorial of " << num << " is " << factorial(num);
return 0;
}
Output:
Factorial of 5 is 120
C++ Programs : 5 Matrix Multiplication
#include <iostream>
int main() {
int A[2][2] = {{1, 2}, {3, 4}};
int B[2][2] = {{2, 0}, {1, 3}};
int result[2][2] = {0};
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
for (int k = 0; k < 2; ++k) {
result[i][j] += A[i][k] * B[k][j]; // Calculate matrix multiplication
}
}
}
std::cout << "Matrix Multiplication Result:" << std::endl;
for (int i = 0; i < 2; ++i) {
for (int j = 0; j < 2; ++j) {
std::cout << result[i][j] << " "; // Print the result
}
std::cout << std::endl;
}
return 0;
}
Output:
Matrix Multiplication Result:
4 6
10 12
C++ Programs : 6 Binary Search
#include <iostream>
#include <vector>
int binarySearch(const std::vector<int>& arr, int target) {
int left = 0;
int right = arr.size() - 1;
while (left <= right) {
int mid = left + (right - left) / 2; // Calculate the middle index
if (arr[mid] == target) {
return mid; // Target found
}
if (arr[mid] < target) {
left = mid + 1; // Adjust the search range
} else {
right = mid - 1; // Adjust the search range
}
}
return -1; // Target not found
}
int main() {
std::vector<int> arr = {1, 3, 5, 7, 9, 11, 13};
int target = 7;
int result = binarySearch(arr, target);
if (result != -1) {
std::cout << target << " found at index " << result;
} else {
std::cout << target << " not found in the array.";
}
return 0;
}
Output:
7 found at index 3
C++ Programs : 7 Linked List
#include <iostream>
struct Node {
int data;
Node* next;
Node(int val) : data(val), next(nullptr) {}
};
int main() {
Node* head = new Node(1);
head->next = new Node(2);
head->next->next = new Node(3);
Node* current = head;
std::cout << "Linked List: ";
while (current != nullptr) {
std::cout << current->data << " "; // Print the linked list elements
current = current->next;
}
return 0;
}
Output:
Linked List: 1 2 3
C++ Programs : 8 Stack Implementation
#include <iostream>
#include <stack>
int main() {
std::stack<int> s;
s.push(1);
s.push(2);
s.push(3);
std::cout << "Stack Elements: ";
while (!s.empty()) {
std::cout << s.top() << " "; // Print and pop elements from the stack
s.pop();
}
return 0;
}
Output:
Stack Elements: 3 2 1
C++ Programs : 9 Queue Implementation
#include <iostream>
#include <queue>
int main() {
std::queue<int> q;
q.push(1);
q.push(2);
q.push(3);
std::cout << "Queue Elements: ";
while (!q.empty()) {
std::cout << q.front() << " "; // Print and dequeue elements from the queue
q.pop();
}
return 0;
}
Output:
Queue Elements: 1 2 3
One thought on “Master 9 C++ Programs with Complex Logic”