10 Mixed C++ Challenges to Test Your Skills
- Problem 1: Power Calculation
- Problem 2: Calculate Percentage
- Problem 3: Factorial Table (1 to 10)
- Problem 4: 5×5 Matrix Min and Max Search
- Problem 5: Modular Quadratic Equation Solver
- Problem 6: Print String of Asterisks
- Problem 7: Repeat Custom Character
- Problem 8: Windows Console Frame Drawer
- Problem 9: Direct Linear & Branching Quadratic Solver
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;
}
Problem 4: 5×5 Matrix Min and Max Search
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;
}Explore More IT Terms
#
- Using an integrating factor
- Equations in total differentials
- Bernoulli's equation
- Linear differential equations of the first order
- 10 Mixed C++ Challenges to Test Your Skills
- 50 Terms Every Programmer Should Know
- 7 Levels of Using the Zip Function in Python
- 7 Python Code Bugs You Need to Fix
- 70+ Free Resources for Learning Programming
A
- A Guide to SQL Query Formatting for Beginners
- A/B testing
- Abstract Data Type (ADT)
- AES Encryption Algorithm: How It Works and Where It's Used
- Agile
- Algorithm
- Algorithm Analysis
- Algorithm Complexity
- Algorithm complexity: deep parsing O(log n)
- Algorithm vs. Program
- Algorithms and Data Structures in C#
- An overview of the C # programming language
- An overview of the Python programming language
- Anaconda Python
- Android
- Android App Bundle
- Android SDK
- Angular
- Ansible
- Apache
- Apache Airflow
- Apache Kafka
- Apache Tomcat
- App Store
- AppCode
- Applications of microcontrollers: From simple circuits in electronics to complex systems
- Applications of the derivative
- Arduino: How to Program It: Basics for Beginners
- Array-based stack
- ArrayList
- ASCII
- ASP.NET
- Assembly Language Lessons
B
C
D
- Data Analytics: applications of data analysis in companies
- Data Engineer - Who is it, what does a data engineer do, and an overview of the profession
- Data modeling: what it is, types, and process steps.
- Data preprocessing: a complete guide for beginners and professionals.
- Data structure
- Data Structures and Algorithms (DSA)
- Data types vs. Data structures
- Database Tests with Answers
- Deep Learning
- Defining Aliases
- Defining Arrays
- Deque
- Developing a Website from Scratch
- Differential Equations
- Differentiation of functions
- Digital data: understand the importance of this asset for businesses.
- Double integrals
- Doubly linked lists
- DSA Tutorial
E
F
G
H
- Handling errors and exceptions
- Heads or Tails? How Probability Theory Is Used in IT
- History of the development of computer science
- Homogeneous equations
- Homogeneous vs. non-homogeneous structures
- How to effectively organize your workflow
- How to Learn Java: Tips for Beginner Developers
- How to Learn PHP: A Beginner's Guide
- How to Use S3 Storage in Kubernetes with CSI
- HTML
- HTML and CSS: Definition, Application, and Operating Principles
- HTML and CSS. Layout from Scratch: What to Learn, Where to Learn, and How Long Will It Take?
- HTML Frame Structure
- HTML Link Formatting
I
- if..else construction
- Infinite sequences and series
- Information properties
- Inheritance in Java: A Complete Guide to Principles and Implementation
- Inserting an Image
- Integration of functions
- Interactive Python Tutorial – Learn Programming from Scratch
- Interpreter
- Interview Problem: Finding a Deleted Element in O(N)
- Interview Scare: The FizzBuzz Challenge
- Introduction to C++
- Introduction to Machine Learning
- Introduction to Networking | Network Fundamentals Part 1
- Introduction to Number Systems (Binary, Octal, Hexadecimal) | Math for CS Foundations #1
- IT Specialist Resume (CV)
J
K
L
M
- Machine Learning
- Machine Learning Basic Tool: NumPy
- Machine Learning Basic Tool: Pandas
- Machine Learning Mathematics
- Mathematics for programmers: what is really needed?
- MD5 encryption algorithm: What is it and why is it needed?
- Microcontroller and Microprocessor - what's the difference?
- ML Engineer: Who They Are, What They Do, How Much They Earn, and How to Become a Neural Network Specialist
- Monte Carlo Simulation: How It Works and What It's For
O
P
- PHP lessons
- Private DNS server and its configuration
- Program code
- Programmer's Dictionary
- Programming
- Programming with pseudocode
- Python Code Formatting Guide: PEP8
- Python for data analysis: how to do it and main libraries
- Python Lessons
- Python Superstar: 5 Ways to Use the * Operator
- Python vs. Julia: Should You Replace Python with Julia?
R
S
- SFML Graphics Library Tutorials
- Sorting Algorithms in Programming: Types, Descriptions, and Comparisons
- SQL commands: see what they are, what the main ones are + examples
- SQL Interview Questions and Tasks
- SQL Lessons
- SQL Stored Procedures
- SQL Syntactic Sugar: The COALESCE Function
- Stack
- Start in analytics: Python or R
- Static vs. dynamic data structures
- Statistical analysis: importance for decision making.
- String formatting in Python
- Structure of computer science
- Swift Lessons
- switch/match construct
- Syntax
T
- Terms in programming
- Text and paragraph formatting tags
- The concept of information and its transmission
- The Future of Python: Key Trends and Insights from Global Researc
- The Infrastructure of Code: A Complete Guide to Repositories for Languages, Frameworks, and Compilers
- The pip package manager in Python
- The role of informatization in the development of society
- Transfers
- Tutorials / Articles
- TypeScript: What It Is and Why Developers Need It
W
- What are databases, and why do they need DBMS and SQL?
- What do Linux distributions consist of?
- What is .NET and what is it used for?
- What is a data structure?
- What is a GPU in a computer, in simple terms?
- What is a quantum computer: 100,500 problems in one second
- What Is an Algorithm?
- What is Arduino: How it Works and the Platform's Capabilities
- What is Big Data? Introduction, Types, Characteristics, and Examples
- What is FizzBuzz Challenge?
- What is Golang and what is it used for?
- What is Haskell and what is it used for?
- What is Kotlin and what is it used for?
- What is Linux? The History of Linux
- What is machine learning, and how does it work?
- What is Power BI: everything about the data analytics software
- What is recursion, recursive and iterative process in programming?
- What is the C++ programming language?
- What is the OSI Model: A Complete Explanation of the Seven Layers and Their Role in Networking
- What's the difference between x86 and ARM processors?
- Where to start learning the C programming language?
- Which Linux distribution should you choose? A Linux distribution overview

