C++ Programs for Beginners: 40+ Examples With Full Code
Short answer: below are 40+ C++ example programs for beginners, grouped from easiest to hardest. Each one links to a full walkthrough with complete, working code you can run. Work down the list in order — every group builds on the one before it.
How to Use This List
Reading example code feels productive. It mostly isn’t. What builds skill is attempting the program first, getting stuck, and then reading the solution.
For each program:
- Read the title and try to write it yourself — even a broken attempt.
- Run it. Fix the errors you can.
- Only then open the walkthrough and compare.
You can run everything here in your browser with an online C++ compiler, or on your own machine — see how to run a .cpp file.
Three Examples to Start With
Even or odd — conditions and the % operator:
#include <iostream>
int main() {
int n;
std::cout << "Enter a number: ";
std::cin >> n;
if (n % 2 == 0) std::cout << n << " is even\n";
else std::cout << n << " is odd\n";
}
Factorial — a loop that builds up a result:
#include <iostream>
int main() {
int n = 5;
long long result = 1;
for (int i = 2; i <= n; ++i) {
result *= i;
}
std::cout << n << "! = " << result << '\n'; // 5! = 120
}
Reverse a string — working with text:
#include <algorithm>
#include <iostream>
#include <string>
int main() {
std::string word = "hello";
std::reverse(word.begin(), word.end());
std::cout << word << '\n'; // olleh
}
If those three made sense, you’re ready for the list. If any of them didn’t, start with C++ Hello World explained and the loops tutorial.
1. Starter Programs
One idea each: input, output, conditions and simple loops.
| Program | What you practise |
|---|---|
| Even or odd | Conditions, the % operator |
| Swap two numbers | Variables, temporaries, std::swap |
| Leap year checker | Combining conditions with && and || |
| Temperature converter | Arithmetic, double, formatting output |
| Multiplication table | for loops |
| FizzBuzz | Loops plus conditions — the classic test |
| Grade calculator | if / else if chains |
| Calculator | switch, input handling |
2. Number Programs
Loops, recursion and a little maths.
| Program | What you practise |
|---|---|
| Factorial | Loops vs recursion |
| Fibonacci | Recursion, iteration, memoisation |
| Prime numbers | Nested loops, early exit |
| Palindrome | Reversing numbers and strings |
| Armstrong numbers | Extracting digits, pow |
| Sum of digits | % and / to take a number apart |
| Reverse a number | while loops |
| GCD and LCM | The Euclidean algorithm |
| Powers and exponents | Why ^ isn’t power, std::pow |
| Binary to decimal | Number bases |
3. String Programs
Working with text and characters.
| Program | What you practise |
|---|---|
| Reverse a string | std::string, std::reverse |
| Count vowels | Looping over characters |
| Count words | std::stringstream |
| Uppercase and lowercase | toupper, tolower, std::transform |
4. Array and Matrix Programs
Storing and processing collections.
| Program | What you practise |
|---|---|
| Sum of array elements | Loops, std::accumulate |
| Find max and min | std::max_element, std::min_element |
| Matrix multiplication | 2D arrays, triple loops |
| Transpose a matrix | 2D indexing |
5. Searching and Sorting
The algorithms every course and interview covers.
| Program | What you practise |
|---|---|
| Linear search | The simplest search |
| Binary search | Halving a sorted range |
| Bubble sort | Swapping neighbours |
| Selection sort | Finding the minimum repeatedly |
| Insertion sort | Building a sorted section |
| Merge sort | Divide and conquer, recursion |
| Quick sort | Partitioning |
| Heap sort | Heaps |
6. Games and Projects
Bigger programs that combine everything above.
| Program | What you practise |
|---|---|
| Number guessing game | Random numbers, loops, input |
| Rock paper scissors | Game logic, enum |
| Menu-driven program | Program structure, functions |
| Tic tac toe | 2D arrays, win checks |
| Lottery program | Arrays, random numbers |
| Bank account | Classes and encapsulation |
| Traffic light simulation | OOP basics |
| Blackjack | Multiple classes working together |
Ready for something bigger? See the guided beginner projects.
A Suggested Four-Week Plan
| Week | Work through |
|---|---|
| 1 | Starter programs |
| 2 | Number programs |
| 3 | Strings, arrays, and the first three searching and sorting programs |
| 4 | The rest of the sorting algorithms, then one game |
Half an hour a day is enough. Consistency beats long weekend sessions.
Take Your C++ Further
These programs teach you to write C++. If you want the concepts underneath explained properly — especially pointers, memory and OOP, which the later programs lean on — the C++ Better Explained Ebook covers them in plain English. Just $19.
👉 Get the C++ Better Explained Ebook — $19
Related Articles
- C++ Projects for Beginners — four larger guided projects.
- Online C++ Compilers — run any of these in your browser.
- C++ Loops Tutorial — the concept most of these programs depend on.
- How to Start Learning C++ — the order to learn things in.
- C++ Cheat Sheet — the syntax at a glance.