Skip to content
C++ Better Explained
Go back
C++ Programs for Beginners: 40+ Examples With Full Code

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:

  1. Read the title and try to write it yourself — even a broken attempt.
  2. Run it. Fix the errors you can.
  3. 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.

Practising the fundamentals? The C++ Better Explained Ebook explains the concepts behind these programs — and the parts that trip most beginners up, like pointers, memory and OOP — in 87 plain-English pages. Just $19.

1. Starter Programs

One idea each: input, output, conditions and simple loops.

ProgramWhat you practise
Even or oddConditions, the % operator
Swap two numbersVariables, temporaries, std::swap
Leap year checkerCombining conditions with && and ||
Temperature converterArithmetic, double, formatting output
Multiplication tablefor loops
FizzBuzzLoops plus conditions — the classic test
Grade calculatorif / else if chains
Calculatorswitch, input handling

2. Number Programs

Loops, recursion and a little maths.

ProgramWhat you practise
FactorialLoops vs recursion
FibonacciRecursion, iteration, memoisation
Prime numbersNested loops, early exit
PalindromeReversing numbers and strings
Armstrong numbersExtracting digits, pow
Sum of digits% and / to take a number apart
Reverse a numberwhile loops
GCD and LCMThe Euclidean algorithm
Powers and exponentsWhy ^ isn’t power, std::pow
Binary to decimalNumber bases

3. String Programs

Working with text and characters.

ProgramWhat you practise
Reverse a stringstd::string, std::reverse
Count vowelsLooping over characters
Count wordsstd::stringstream
Uppercase and lowercasetoupper, tolower, std::transform

4. Array and Matrix Programs

Storing and processing collections.

ProgramWhat you practise
Sum of array elementsLoops, std::accumulate
Find max and minstd::max_element, std::min_element
Matrix multiplication2D arrays, triple loops
Transpose a matrix2D indexing

5. Searching and Sorting

The algorithms every course and interview covers.

ProgramWhat you practise
Linear searchThe simplest search
Binary searchHalving a sorted range
Bubble sortSwapping neighbours
Selection sortFinding the minimum repeatedly
Insertion sortBuilding a sorted section
Merge sortDivide and conquer, recursion
Quick sortPartitioning
Heap sortHeaps

6. Games and Projects

Bigger programs that combine everything above.

ProgramWhat you practise
Number guessing gameRandom numbers, loops, input
Rock paper scissorsGame logic, enum
Menu-driven programProgram structure, functions
Tic tac toe2D arrays, win checks
Lottery programArrays, random numbers
Bank accountClasses and encapsulation
Traffic light simulationOOP basics
BlackjackMultiple classes working together

Ready for something bigger? See the guided beginner projects.

A Suggested Four-Week Plan

WeekWork through
1Starter programs
2Number programs
3Strings, arrays, and the first three searching and sorting programs
4The 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


📋

Free Download: The 10 Mistakes Every C++ Beginner Makes

A free 1-page checklist that shows the exact traps that slow down every C++ beginner — so you can avoid them from day one.

🔒 No spam. Unsubscribe anytime.


Share this post on:

Written by

Sahil Bora

Software Engineer. Author and creator of C++ Better Explained.


Previous Post
How to Fix Expected ';' Errors in C++
Next Post
Shallow Copy vs Deep Copy in C++ (With Examples)

Keep Learning