Simple C++ Programs

Learn C++ with 10 simple programs for beginners, complete with source code, outputs, explanations, and key programming concepts.

10 Simple C++ Programs Every Beginner Should Practice

Learning C++ isn't about memorizing syntax—it's about solving problems. The best way to master the language is by writing small, practical programs that introduce one concept at a time.

Whether you're a computer science student preparing for coding interviews or just starting your programming journey, practicing beginner-friendly C++ programs helps you build a strong foundation before moving on to advanced topics like object-oriented programming, data structures, and algorithms.

In this guide, you'll find carefully selected C++ programs that cover the fundamentals of programming. Each example includes:

  • Complete C++ source code
  • Sample output
  • Step-by-step explanation
  • Time and space complexity
  • Common beginner mistakes
  • Key concepts you'll learn

By the end of this tutorial, you'll understand how to work with variables, input/output streams, arithmetic operators, conditional statements, and built-in operators such as sizeof().

What Are Simple C++ Programs?

Simple C++ programs are beginner-level coding exercises designed to teach core programming concepts. They focus on basic syntax, user input, variables, operators, conditions, loops, and functions.

These programs help beginners:

  • Understand C++ syntax
  • Develop logical thinking
  • Learn problem-solving
  • Prepare for coding interviews
  • Build confidence before working on projects

Looking to build your C++ skills further? Explore the C++ Programming for Beginners to Advanced course by Great Learning Academy Pro to master core concepts, object-oriented programming, pointers, memory management, and hands-on projects. 

Academy Pro

C++ Programming Course

Master key C++ programming concepts like variables, functions, OOP, and control structures. Build real-world projects such as a banking system and grade management tool.

Beginner to Advanced Level
8.1 hrs
Start Free Trial

Why Should Beginners Practice C++ Programs?

Every experienced programmer started with small coding exercises.

Writing simple programs helps you:

  • Learn faster through practice
  • Understand compiler errors
  • Improve debugging skills
  • Build programming logic
  • Gain confidence before solving complex problems

Instead of reading theory for hours, writing code every day is the fastest way to improve.

Prerequisites

Before starting these examples, make sure you know:

  • How to install a C++ compiler
  • How to create a C++ source file
  • Basic understanding of variables
  • How to run a C++ program

If you're completely new, don't worry—every example below explains each concept step by step.

Program 1: Hello World in C++

The "Hello World" program is traditionally the first program every programmer writes. It introduces the basic structure of a C++ program and demonstrates how to display output on the console.

C++ Code

c++
#include <iostream>

int main() {
    std::cout << "Hello, World!";
    return 0;
}

Output

output
Hello, World!

How It Works

  • #include <iostream> imports the input/output library.
  • main() is the starting point of every C++ program.
  • std::cout prints text on the console.
  • return 0; tells the operating system that the program finished successfully.

Time Complexity

O(1)

Space Complexity

O(1)

Common Mistakes

  • Forgetting the semicolon.
  • Missing the #include <iostream> header.
  • Writing cout without std:: when not using the std namespace.

Key Concepts

  • Program structure
  • Header files
  • main() function
  • cout

Program 2: Print User Input

Accepting user input is one of the first interactive features you'll learn in C++. This program reads an integer from the keyboard and displays it back to the user.

C++ Code

c++
#include <iostream>
using namespace std;

int main() {
    int number;

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

    cout << "You entered: " << number;

    return 0;
}

Output

output
Enter a number: 25

You entered: 25

How It Works

The cin object reads input from the keyboard and stores it in the variable number. The cout object then displays the stored value.

Time Complexity

O(1)

Space Complexity

O(1)

Common Mistakes

  • Using the wrong data type.
  • Forgetting cin >>.
  • Declaring variables after using them.

What You'll Learn

  • Variables
  • User input
  • Output
  • Integer data type

Program 3: Add Two Numbers

Adding two numbers is one of the simplest ways to understand arithmetic operators and user input.

C++ Code

c++
#include <iostream>
using namespace std;

int main() {

    int a, b;

    cout << "Enter two numbers: ";
    cin >> a >> b;

    cout << "Sum = " << a + b;

    return 0;
}

Output

output
Enter two numbers: 15 20

Sum = 35

Explanation

The program accepts two integers from the user and uses the + operator to calculate their sum.

Complexity

Time: O(1)

Space: O(1)

Concepts Covered

  • Arithmetic operators
  • Multiple user inputs
  • Integer addition

Beginner Tip

Always initialize variables before using them in calculations.

Program 4: Find Quotient and Remainder

Division in C++ produces two useful values:

  • Quotient
  • Remainder

The modulus (%) operator returns the remainder after division.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    int dividend, divisor;

    cout << "Enter dividend: ";

    cin >> dividend;

    cout << "Enter divisor: ";

    cin >> divisor;

    cout << "Quotient = " << dividend / divisor << endl;

    cout << "Remainder = " << dividend % divisor;

    return 0;

}

Sample Output

output
Enter dividend: 15

Enter divisor: 2

Quotient = 7

Remainder = 1

How It Works

The / operator performs integer division, while % calculates the remainder.

Time Complexity

O(1)

Space Complexity

O(1)

Common Mistakes

  • Dividing by zero.
  • Using % with floating-point numbers.

Program 5: Find the Size of Data Types

Different data types occupy different amounts of memory. C++ provides the sizeof() operator to determine how much memory a data type or variable uses.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    cout << "int: " << sizeof(int) << " bytes\n";

    cout << "float: " << sizeof(float) << " bytes\n";

    cout << "char: " << sizeof(char) << " byte\n";

    cout << "double: " << sizeof(double) << " bytes";

    return 0;

}

Output

output
int: 4 bytes

float: 4 bytes

char: 1 byte

double: 8 bytes

Note: Exact sizes may vary by compiler and system architecture, though the values above are common on modern platforms.

Why This Program Matters

Understanding memory usage is essential for writing efficient C++ programs. The sizeof() operator is also widely used in arrays, structures, templates, and systems programming.

Complexity

Time: O(1)

Space: O(1)

Concepts Covered

  • Data types
  • Memory allocation
  • sizeof() operator
  • Console output

Key Takeaways

After completing these first five programs, you should be comfortable with:

  • Writing a basic C++ program
  • Displaying output with cout
  • Reading user input using cin
  • Performing arithmetic operations
  • Understanding common data types
  • Using the sizeof() operator
  • Recognizing the structure of every C++ program

Ready to solve more challenging coding problems? Check out the Data Structures and Algorithms with C++ course by Great Learning Academy Pro to learn advanced problem-solving techniques and prepare for coding interviews. 

Academy Pro

C++ Data Structures and Algorithms Course Course

Master DSA (Data Structures and Algorithms) with C++. Build coding skills in arrays, trees, graphs, heaps, sorting, searching, hashing, and algorithm analysis for interviews.

Intermediate Level
9.17 hrs
Start Free Trial

Program 6: Swap Two Numbers

Swapping two numbers is one of the most common beginner exercises in C++. It introduces variables, assignments, and logical thinking. There are two popular approaches: using a temporary variable and swapping without one.

Method 1: Swap Using a Temporary Variable

This is the easiest and most readable approach.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    int a = 10, b = 20;

    int temp;

    cout << "Before Swapping\n";

    cout << "a = " << a << ", b = " << b << endl;

    temp = a;

    a = b;

    b = temp;

    cout << "\nAfter Swapping\n";

    cout << "a = " << a << ", b = " << b;

    return 0;

}

Output

output
Before Swapping

a = 10, b = 20

After Swapping

a = 20, b = 10

How It Works

The temporary variable stores the value of a before it's overwritten. Once a receives the value of b, the original value is restored from temp.

Time Complexity

O(1)

Space Complexity

O(1)

Method 2: Swap Without a Temporary Variable

Instead of using an extra variable, arithmetic operations can perform the swap.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    int a = 10, b = 20;

    a = a + b;

    b = a - b;

    a = a - b;

    cout << "a = " << a << endl;

    cout << "b = " << b;

    return 0;

}

Why Learn Both Methods?

While the arithmetic method is an interesting interview question, the temporary-variable approach is generally preferred because it is easier to read and avoids integer overflow.

Key Concepts

  • Variables
  • Assignment operator
  • Memory usage
  • Problem-solving

Common Mistakes

  • Forgetting the correct order of assignments
  • Using arithmetic swapping with very large integers, which may overflow

Program 7: Check Whether a Number Is Even or Odd

One of the simplest uses of conditional statements is determining whether a number is even or odd.

A number is even if it is divisible by 2; otherwise, it is odd.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    int number;

    cout << "Enter a number: ";

    cin >> number;

    if (number % 2 == 0)

        cout << "Even Number";

    else

        cout << "Odd Number";

    return 0;

}

Output

output
Enter a number: 18

Even Number

Explanation

The modulus (%) operator returns the remainder after division.

  • If the remainder is 0, the number is even.
  • Otherwise, it is odd.

Time Complexity

O(1)

Space Complexity

O(1)

Concepts Covered

  • if-else statement
  • Modulus operator
  • Decision making

Interview Tip

Checking whether a number is even or odd is one of the most frequently asked beginner programming questions.

Program 8: Check Whether a Character Is a Vowel or Consonant

This program introduces character variables and logical operators.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    char ch;

    cout << "Enter a character: ";

    cin >> ch;

    if (ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||

        ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U')

        cout << "Vowel";

    else

        cout << "Consonant";

    return 0;

}

Output

output
Enter a character: E

Vowel

How It Works

The program compares the entered character with all five vowels in both uppercase and lowercase.

If none match, it assumes the character is a consonant.

Time Complexity

O(1)

Space Complexity

O(1)

Common Mistakes

  • Forgetting uppercase vowels
  • Entering digits instead of letters
  • Not validating special characters

Key Concepts

  • Character data type
  • Logical OR operator
  • Conditional statements

Program 9: Check Whether a Year Is a Leap Year

Leap year calculations are a classic example of nested conditional logic.

A year is considered a leap year if:

  • It is divisible by 400, or
  • It is divisible by 4 but not divisible by 100

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    int year;

    cout << "Enter year: ";

    cin >> year;

    if ((year % 400 == 0) ||

       (year % 4 == 0 && year % 100 != 0))

        cout << "Leap Year";

    else

        cout << "Not a Leap Year";

    return 0;

}

Sample Output

output
Enter year: 2028

Leap Year

Why This Program Is Important

This example introduces multiple logical conditions, making it an excellent exercise for understanding real-world decision-making.

Complexity

Time: O(1)

Space: O(1)

Concepts Covered

  • Logical AND (&&)
  • Logical OR (||)
  • Nested conditions

Program 10: Find the Largest of Three Numbers

Finding the maximum value among multiple inputs is another popular beginner program.

C++ Code

c++
#include <iostream>

using namespace std;

int main() {

    int a, b, c;

    cout << "Enter three numbers: ";

    cin >> a >> b >> c;

    if (a >= b && a >= c)

        cout << "Largest = " << a;

    else if (b >= a && b >= c)

        cout << "Largest = " << b;

    else

        cout << "Largest = " << c;

    return 0;

}

Output

output
Enter three numbers:

25 12 18

Largest = 25

Explanation

The program compares the first number against the other two.

If it isn't the largest, it checks the second number.

If neither condition is true, the third number must be the largest.

Time Complexity

O(1)

Space Complexity

O(1)

Concepts Covered

  • Nested conditions
  • Comparison operators
  • Logical operators

Common Mistakes

  • Using > instead of >=
  • Forgetting the else if
  • Missing braces in complex conditions

Final Thoughts

Writing simple C++ programs is one of the fastest ways to build a strong programming foundation. Each exercise teaches a specific concept—whether it's taking user input, making decisions with conditional statements, working with loops, or solving basic mathematical problems.

The key is consistency. Rather than trying to learn everything at once, practice a few programs each day, experiment with the code, and gradually increase the complexity of your projects. As your confidence grows, you'll be ready to tackle more advanced C++ topics such as object-oriented programming, templates, and the Standard Template Library.

By mastering these 10 beginner-friendly programs, you'll develop the problem-solving skills and coding habits needed for academic success, technical interviews, and software development projects.

Frequently Asked Questions

Question: What is the easiest C++ program for beginners?

The easiest C++ program for beginners is the Hello World program. It introduces the basic structure of a C++ application, including the main() function, the <iostream> header, and the cout statement for output.

Question: How many C++ programs should beginners practice?

Beginners should practice at least 30 to 50 C++ programs covering topics like variables, operators, loops, functions, arrays, strings, and object-oriented programming. Regular practice improves coding skills and problem-solving ability.

Question: Is C++ difficult to learn?

C++ has a steeper learning curve than some modern programming languages because it introduces concepts like memory management, pointers, and object-oriented programming. However, learning one concept at a time and practicing consistently makes it much easier to master.

Question: Which IDE is best for C++ programming?

Some of the most popular C++ IDEs include Visual Studio Code, Code::Blocks, CLion, Visual Studio, and Dev-C++. Choose an IDE that provides syntax highlighting, debugging tools, and compiler support to make development easier.

Question: What should I learn after these beginner C++ programs?

After completing these programs, you should move on to arrays, strings, functions, recursion, pointers, structures, classes and objects, the Standard Template Library (STL), and data structures and algorithms. These topics build the skills required for interviews and real-world software development.

Avatar photo
Great Learning Editorial Team
The Great Learning Editorial Staff includes a dynamic team of subject matter experts, instructors, and education professionals who combine their deep industry knowledge with innovative teaching methods. Their mission is to provide learners with the skills and insights needed to excel in their careers, whether through upskilling, reskilling, or transitioning into new fields.

Go Beyond Learning. Get Job-Ready.

Build in-demand skills for today's jobs with free expert-led courses and practical AI tools.

Explore All Courses
Scroll to Top