10 Mixed C++ Challenges to Test Your Skills

⏱️ Estimated Reading Time: ~7 min read
0
(0)

Problem 1: Power Calculation

Write a function that calculates ab using values entered from the keyboard.

solution:

#include <iostream>
#include <cmath>

using namespace std;

float power(float base, float exponent) {
    return pow(base, exponent);
}

int main() {
    float a, b;

    cout << "Enter the number a: ";
    cin >> a;

    cout << "Enter the power b: ";
    cin >> b;

    cout << a << " to the power " << b << " is " << power(a, b) << endl;

    return 0;
}

Problem 2: Calculate Percentage

Write a function that calculates a percentage of a number (e.g., 321% of 3 is 9.63).

solution:

#include <iostream>

using namespace std;

float calculatePercentage(float percent, float number) {
    return (number * percent) / 100.0f;
}

int main() {
    float percent, number;

    cout << "Percentage >> ";
    cin >> percent;

    cout << "Number >> ";
    cin >> number;

    cout << percent << "% of " << number << " = " << calculatePercentage(percent, number) << endl;

    return 0;
}

Problem 3: Factorial Table (1 to 10)

Write a program that displays a table of factorials from 1 to 10 using a custom function.

solution:

#include <iostream>

using namespace std;

long long factorial(int n) {
    long long result = 1;
    for (int i = 1; i <= n; ++i) {
        result *= i;
    }
    return result;
}

int main() {
    for (int i = 1; i <= 10; ++i) {
        cout << i << "! = " << factorial(i) << endl;
    }

    return 0;
}

Create a 5×5 array filled with random integers from 30 to 60, then write functions to find the minimum and maximum elements.

solution:

#include <iostream>
#include <array>
#include <random>
#include <algorithm>
#include <iomanip>

constexpr std::size_t SIZE = 5;
using Matrix = std::array<std::array<int, SIZE>, SIZE>;

void fillAndShowArray(Matrix& arr) {
    std::random_device rd;
    std::mt19937 gen(rd());
    std::uniform_int_distribution<int> dist(30, 60);

    for (auto& row : arr) {
        std::cout << "| ";
        for (auto& val : row) {
            val = dist(gen);
            std::cout << std::setw(2) << val << " ";
        }
        std::cout << "|\n";
    }
}

int findMinElement(const Matrix& arr) {
    int minVal = arr[0][0];
    for (const auto& row : arr) {
        auto minIt = std::min_element(row.begin(), row.end());
        minVal = std::min(minVal, *minIt);
    }
    return minVal;
}

int findMaxElement(const Matrix& arr) {
    int maxVal = arr[0][0];
    for (const auto& row : arr) {
        auto maxIt = std::max_element(row.begin(), row.end());
        maxVal = std::max(maxVal, *maxIt);
    }
    return maxVal;
}

int main() {
    Matrix matrix{};

    fillAndShowArray(matrix);

    std::cout << "\nMinimum: " << findMinElement(matrix) << "\n";
    std::cout << "Maximum: " << findMaxElement(matrix) << "\n";

    return 0;
}

Problem 5: Modular Quadratic Equation Solver

Write a function that calculates the roots of a quadratic equation based on coefficients (a, b, c), handling invalid cases (e.g., a = 0 or negative discriminant).

solution:

#include <iostream>
#include <cmath>

void solveQuadratic(double a, double b, double c) {
    if (a == 0) {
        std::cout << "Invalid input: 'a' cannot be 0 for a quadratic equation.\n";
        return;
    }

    double discriminant = (b * b) - (4 * a * c);

    if (discriminant < 0) {
        std::cout << "No real roots exist.\n";
    } else if (discriminant == 0) {
        double x = -b / (2 * a);
        std::cout << "Single real root: x = " << x << "\n";
    } else {
        double x1 = (-b + std::sqrt(discriminant)) / (2 * a);
        double x2 = (-b - std::sqrt(discriminant)) / (2 * a);
        std::cout << "Two real roots:\nx1 = " << x1 << "\nx2 = " << x2 << "\n";
    }
}

int main() {
    double a, b, c;

    std::cout << "Enter coefficient a: ";
    std::cin >> a;
    std::cout << "Enter coefficient b: ";
    std::cin >> b;
    std::cout << "Enter coefficient c: ";
    std::cin >> c;

    solveQuadratic(a, b, c);

    return 0;
}

Problem 6: Print String of Asterisks

Write a program and function to output a line of asterisks based on user input length.

solution:

#include <iostream>

void printAsterisks(int length) {
    for (int i = 0; i < length; ++i) {
        std::cout << '*';
    }
    std::cout << std::endl;
}

int main() {
    int length = 0;

    std::cout << "Enter the length of the string: ";
    if (std::cin >> length && length > 0) {
        printAsterisks(length);
    } else {
        std::cout << "Invalid length entered.\n";
    }

    return 0;
}

Problem 7: Repeat Custom Character

Create a function that outputs a specified character repeated a user-defined number of times.

solution:

#include <iostream>

using namespace std;

void printCustomSymbol(int count, char symbol) {
    for (int i = 0; i < count; ++i) {
        cout << symbol;
    }
    cout << endl;
}

int main() {
    int length;
    char symbol;

    cout << "Enter the length of the string >> ";
    cin >> length;

    cout << "Enter character >> ";
    cin >> symbol;

    printCustomSymbol(length, symbol);

    return 0;
}

Problem 8: Windows Console Frame Drawer

Write a function to draw a hollow rectangle on the Windows console at coordinates (x, y) given a width and height.

solution:

#include <iostream>
#include <windows.h>

using namespace std;

void gotoxy(int x, int y) {
    COORD coord;
    coord.X = static_cast<SHORT>(x);
    coord.Y = static_cast<SHORT>(y);
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void drawRectangle(int startX, int startY, int width, int height) {
    const char borderChar = '#';

    for (int row = 0; row < height; ++row) {
        gotoxy(startX, startY + row);
        for (int col = 0; col < width; ++col) {
            if (row == 0 || row == height - 1 || col == 0 || col == width - 1) {
                cout << borderChar;
            } else {
                cout << ' ';
            }
        }
    }
}

int main() {
    int width, height, x, y;

    cout << "Enter x-coordinate >> ";
    cin >> x;
    cout << "Enter y-coordinate >> ";
    cin >> y;
    cout << "Enter width >> ";
    cin >> width;
    cout << "Enter height >> ";
    cin >> height;

    system("cls");
    drawRectangle(x, y, width, height);

    gotoxy(0, y + height + 1);
    return 0;
}

Problem 9: Direct Linear & Branching Quadratic Solver

Complete the quadratic equation calculation within main() using simple conditional branching.

solution:

#include <iostream>
#include <cmath>

using namespace std;

int main() {
    double a, b, c;

    cout << "Enter coefficient a: ";
    cin >> a;
    cout << "Enter coefficient b: ";
    cin >> b;
    cout << "Enter coefficient c: ";
    cin >> c;

    if (a == 0) {
        if (b == 0) {
            cout << (c == 0 ? "Infinite solutions.\n" : "No solution.\n");
        } else {
            cout << "Linear equation root: x = " << -c / b << endl;
        }
    } else {
        double discriminant = (b * b) - (4 * a * c);

        if (discriminant < 0) {
            cout << "No real roots exist.\n";
        } else if (discriminant == 0) {
            cout << "Single real root: x = " << -b / (2 * a) << endl;
        } else {
            double x1 = (-b + sqrt(discriminant)) / (2 * a);
            double x2 = (-b - sqrt(discriminant)) / (2 * a);
            cout << "Two real roots:\nx1 = " << x1 << "\nx2 = " << x2 << endl;
        }
    }

    return 0;
}

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?


Explore More IT Terms


Share this term: Facebook X LinkedIn WhatsApp Email

Leave a Reply

Your email address will not be published. Required fields are marked *