Labs ICT
Pro Login

Examples

Palindrome Check

This function checks whether a given string reads the same forward and backward.

#include <iostream>
#include <algorithm>

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

int main() {
    std::cout << isPalindrome("racecar") << "\n";
    std::cout << isPalindrome("hello");
    return 0;
}
Try it Yourself →

Fibonacci Sequence

Generates the first N Fibonacci numbers using iteration.

#include <iostream>

void fibonacci(int n) {
    int a = 0, b = 1;
    for (int i = 0; i < n; i++) {
        std::cout << a << " ";
        int next = a + b;
        a = b;
        b = next;
    }
}

int main() {
    fibonacci(10);
    return 0;
}
Try it Yourself →

Bubble Sort

Simple sorting algorithm that repeatedly swaps adjacent elements if they are in the wrong order.

#include <iostream>

void bubbleSort(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (arr[j] > arr[j + 1]) {
                int temp = arr[j];
                arr[j] = arr[j + 1];
                arr[j + 1] = temp;
            }
        }
    }
}

int main() {
    int arr[] = {64, 34, 25, 12, 22, 11, 90};
    int n = sizeof(arr) / sizeof(arr[0]);
    bubbleSort(arr, n);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
Try it Yourself →