15 C++ Programs for Beginners

    Atul Kabra6 min readUpdated

    The fastest way to learn C++ is to type small programs and run them. Below are 15 short, complete, commented programs that build from your very first line of output up to a tiny class. Each one compiles as-is with a modern C++ compiler. Read the comments, type them out yourself, then tweak them — change the numbers, add a feature, break it on purpose to see what the compiler says.

    1. Hello, World

    #include <iostream>
    
    int main() {
        std::cout << "Hello, World!\n";   // print text, then a newline
        return 0;                         // 0 means "success"
    }
    

    2. Add two numbers

    #include <iostream>
    
    int main() {
        int a, b;
        std::cout << "Enter two numbers: ";
        std::cin >> a >> b;               // read two ints from the user
        std::cout << "Sum = " << (a + b) << "\n";
    }
    

    3. Even or odd

    #include <iostream>
    
    int main() {
        int n;
        std::cin >> n;
        // % gives the remainder; 0 remainder means even
        std::cout << (n % 2 == 0 ? "Even" : "Odd") << "\n";
    }
    

    4. Largest of three numbers

    #include <iostream>
    #include <algorithm>   // std::max
    
    int main() {
        int a, b, c;
        std::cin >> a >> b >> c;
        int biggest = std::max({a, b, c});   // max of an initializer list
        std::cout << "Largest = " << biggest << "\n";
    }
    

    5. Factorial of a number

    #include <iostream>
    
    int main() {
        int n;
        std::cin >> n;
        unsigned long long result = 1;
        for (int i = 2; i <= n; ++i) {   // multiply 2 * 3 * ... * n
            result *= i;
        }
        std::cout << n << "! = " << result << "\n";
    }
    

    6. Check if a number is prime

    #include <iostream>
    
    int main() {
        int n;
        std::cin >> n;
        bool isPrime = (n > 1);
        for (int i = 2; i * i <= n; ++i) { // only test up to the square root
            if (n % i == 0) {
                isPrime = false;
                break;
            }
        }
        std::cout << (isPrime ? "Prime" : "Not prime") << "\n";
    }
    

    7. Sum of digits

    #include <iostream>
    
    int main() {
        int n, sum = 0;
        std::cin >> n;
        n = (n < 0) ? -n : n;        // work with the absolute value
        while (n > 0) {
            sum += n % 10;           // add the last digit
            n /= 10;                 // drop the last digit
        }
        std::cout << "Digit sum = " << sum << "\n";
    }
    

    8. Reverse a number

    #include <iostream>
    
    int main() {
        int n, reversed = 0;
        std::cin >> n;
        while (n != 0) {
            reversed = reversed * 10 + n % 10; // build the reverse digit by digit
            n /= 10;
        }
        std::cout << "Reversed = " << reversed << "\n";
    }
    

    9. Multiplication table

    #include <iostream>
    
    int main() {
        int n;
        std::cin >> n;
        for (int i = 1; i <= 10; ++i) {
            std::cout << n << " x " << i << " = " << (n * i) << "\n";
        }
    }
    

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    10. Fibonacci series

    #include <iostream>
    
    int main() {
        int count;
        std::cin >> count;
        long long a = 0, b = 1;
        for (int i = 0; i < count; ++i) {
            std::cout << a << " ";
            long long next = a + b;   // each term is the sum of the previous two
            a = b;
            b = next;
        }
        std::cout << "\n";
    }
    

    11. Swap two numbers (with std::swap)

    #include <iostream>
    #include <utility>   // std::swap
    
    int main() {
        int x = 5, y = 9;
        std::swap(x, y);             // clean, standard way to swap
        std::cout << "x=" << x << " y=" << y << "\n";  // x=9 y=5
    }
    

    12. Count vowels in a string

    #include <iostream>
    #include <string>
    
    int main() {
        std::string text;
        std::getline(std::cin, text);
        int vowels = 0;
        for (char c : text) {
            char lower = std::tolower(static_cast<unsigned char>(c));
            if (lower == 'a' || lower == 'e' || lower == 'i'
                || lower == 'o' || lower == 'u') {
                ++vowels;
            }
        }
        std::cout << "Vowels: " << vowels << "\n";
    }
    

    13. Palindrome check

    #include <iostream>
    #include <string>
    #include <algorithm>
    
    int main() {
        std::string s;
        std::cin >> s;
        std::string reversed = s;
        std::reverse(reversed.begin(), reversed.end()); // reverse a copy
        std::cout << (s == reversed ? "Palindrome" : "Not a palindrome") << "\n";
    }
    

    14. Average of numbers in a vector

    #include <iostream>
    #include <vector>
    #include <numeric>   // std::accumulate
    
    int main() {
        std::vector<int> marks{72, 95, 60, 88};
        int total = std::accumulate(marks.begin(), marks.end(), 0); // sum
        double average = static_cast<double>(total) / marks.size();
        std::cout << "Average = " << average << "\n";
    }
    

    15. A simple class

    #include <iostream>
    #include <string>
    
    class Rectangle {
        double width, height;          // private data
    public:
        Rectangle(double w, double h) : width(w), height(h) {}
        double area() const { return width * height; }
    };
    
    int main() {
        Rectangle r{3.0, 4.0};
        std::cout << "Area = " << r.area() << "\n";   // 12
    }
    

    How to practise these well

    • Type them, do not copy-paste. The muscle memory of writing #include and std::cout is part of learning.
    • Compile after every change so you catch errors while they are small.
    • Modify each program. Make the factorial handle 0, make the prime checker print all primes up to N, make the class add a perimeter().
    • Read the compiler errors. They are your fastest teacher.

    Common mistakes

    • Forgetting #include. Each feature needs its header — <iostream> for I/O, <vector> for vectors, <string> for strings, <algorithm> for sort/reverse.
    • Reading input wrong. std::cin >> s stops at the first space; use std::getline when you want a whole line.
    • Integer division surprises. 7 / 2 is 3, not 3.5. Cast to double (as in program 14) for real division.
    • Off-by-one loops. Decide carefully between < and <= in your loop conditions.

    FAQ

    What is the best first C++ program to write? Start with Hello World (program 1). It confirms your compiler works and teaches the structure of main() and std::cout.

    Do I need to memorise these? No. Understand the logic, then re-derive them. The patterns — loops, conditionals, accumulating a result — matter far more than any single program.

    Keep learning

    Turn these exercises into real projects with mentors in Jalgaon — join the waitlist for our C++ Programming course.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides