Tutorials Logic, IN +91 8092939553 info@tutorialslogic.com
Navigation
Home About Us Contact Us Blogs FAQs
Tutorials
All Tutorials
Services
Academic Projects Resume Writing Interview Questions Website Development
Compiler Tutorials

C++ Important Programs

Classic C++ Programs

These programs cover the most commonly asked C++ problems in interviews and competitive programming. Each demonstrates key language features.

Fibonacci, Factorial, Prime Check
#include <iostream>
using namespace std;

// Iterative Fibonacci
void fibonacci(int n) {
    long long a = 0, b = 1;
    cout << "Fibonacci(" << n << "): ";
    for (int i = 0; i < n; i++) {
        cout << a << " ";
        long long c = a + b;
        a = b;
        b = c;
    }
    cout << endl;
}

// Recursive Fibonacci
long long fibRec(int n) {
    if (n <= 1) return n;
    return fibRec(n-1) + fibRec(n-2);
}

int main() {
    fibonacci(10);  // 0 1 1 2 3 5 8 13 21 34
    cout << "fib(10) = " << fibRec(10) << endl;  // 55
    return 0;
}
#include <iostream>
using namespace std;

long long factorial(int n) {
    if (n <= 1) return 1;
    return n * factorial(n - 1);
}

int main() {
    for (int i = 0; i <= 12; i++) {
        cout << i << "! = " << factorial(i) << endl;
    }
    return 0;
}
#include <iostream>
#include <cmath>
using namespace std;

bool isPrime(int n) {
    if (n < 2) return false;
    if (n == 2) return true;
    if (n % 2 == 0) return false;
    for (int i = 3; i <= sqrt(n); i += 2) {
        if (n % i == 0) return false;
    }
    return true;
}

int main() {
    cout << "Primes up to 50: ";
    for (int i = 2; i <= 50; i++) {
        if (isPrime(i)) cout << i << " ";
    }
    cout << endl;
    // 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
    return 0;
}
Palindrome, Reverse String, Anagram
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isPalindrome(const string &s) {
    string rev = s;
    reverse(rev.begin(), rev.end());
    return s == rev;
}

bool isNumPalindrome(int n) {
    int original = n, reversed = 0;
    while (n > 0) {
        reversed = reversed * 10 + n % 10;
        n /= 10;
    }
    return original == reversed;
}

int main() {
    cout << boolalpha;
    cout << isPalindrome("racecar") << endl;  // true
    cout << isPalindrome("hello")   << endl;  // false
    cout << isNumPalindrome(121)    << endl;  // true
    cout << isNumPalindrome(123)    << endl;  // false
    return 0;
}
#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

bool isAnagram(string a, string b) {
    sort(a.begin(), a.end());
    sort(b.begin(), b.end());
    return a == b;
}

int main() {
    cout << boolalpha;
    cout << isAnagram("listen", "silent") << endl;  // true
    cout << isAnagram("hello",  "world")  << endl;  // false
    cout << isAnagram("triangle", "integral") << endl;  // true
    return 0;
}
Bubble Sort, Binary Search, Matrix Multiplication
#include <iostream>
#include <vector>
using namespace std;

void bubbleSort(vector<int> &v) {
    int n = v.size();
    for (int i = 0; i < n-1; i++) {
        bool swapped = false;
        for (int j = 0; j < n-i-1; j++) {
            if (v[j] > v[j+1]) {
                swap(v[j], v[j+1]);
                swapped = true;
            }
        }
        if (!swapped) break;
    }
}

int binarySearch(const vector<int> &v, int target) {
    int lo = 0, hi = v.size() - 1;
    while (lo <= hi) {
        int mid = lo + (hi - lo) / 2;
        if (v[mid] == target) return mid;
        if (v[mid] < target) lo = mid + 1;
        else hi = mid - 1;
    }
    return -1;
}

int main() {
    vector<int> v = {64, 34, 25, 12, 22, 11, 90};
    bubbleSort(v);
    for (int n : v) cout << n << " ";  // 11 12 22 25 34 64 90
    cout << endl;

    cout << "Index of 25: " << binarySearch(v, 25) << endl;  // 2
    return 0;
}
#include <iostream>
using namespace std;

void multiplyMatrix(int A[][3], int B[][3], int C[][3], int n) {
    for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++) {
            C[i][j] = 0;
            for (int k = 0; k < n; k++)
                C[i][j] += A[i][k] * B[k][j];
        }
}

int main() {
    int A[3][3] = {{1,2,3},{4,5,6},{7,8,9}};
    int B[3][3] = {{9,8,7},{6,5,4},{3,2,1}};
    int C[3][3];

    multiplyMatrix(A, B, C, 3);

    cout << "A x B:" << endl;
    for (int i = 0; i < 3; i++) {
        for (int j = 0; j < 3; j++) cout << C[i][j] << "\t";
        cout << endl;
    }
    return 0;
}

Previous Next

Ready to Level Up Your Skills?

Explore 500+ free tutorials across 20+ languages and frameworks.