This page contains C++ programs for beginners and practice, covering basic input and output, arithmetic, conditions, loops, number problems, strings, and pattern printing. Each linked example focuses on a specific programming problem so that you can study the logic and then practice writing the solution yourself.
C++ Programs for Beginners
If you are learning C++, begin with small programs such as Hello World, reading a number, addition, multiplication, and swapping values. Then move to programs that require conditions and loops, such as leap year, factorial, Fibonacci series, prime numbers, Armstrong numbers, palindromes, and pattern printing.
These programs are useful for practicing common C++ concepts including variables, operators, cin, cout, if statements, loops, strings, and arithmetic expressions.
Basic Structure Used in C++ Programs
A simple C++ program commonly includes the <iostream> header, a main() function, and statements that perform the required operation.
#include <iostream>
int main() {
std::cout << "Hello, World!";
return 0;
}
Output:
Hello, World!
As the programs become more complex, the statements inside main() may include user input, conditions, loops, functions, or string operations.
C++ Programs for Input, Output, and Basic Arithmetic
Start with these examples to practice reading values, displaying results, and using arithmetic operators.
- C++ Program – Hello World
- C++ Program – Print Number Entered by User
- C++ Program – Sum of Two Numbers
- C++ Program – Sum of Three Numbers
- C++ Program – Average of Three Numbers
- C++ Program – Multiply Two Numbers
- C++ Program – Find Quotient and Remainder
- C++ Program – Swap Two Numbers
- C++ Program – Power of a Number
C++ Programs Using Conditions
These programs practice decision-making with conditions. Typical solutions use comparison operators together with if, else if, and else.
C++ Number Programs Using Loops
Number-based problems are useful for practicing for and while loops, repeated arithmetic, divisibility checks, and digit-by-digit processing.
- C++ Program – Armstrong Number
- C++ Program – Convert Decimal to Binary
- C++ Program – Factorial of a Number
- C++ Program – Factors of a Number
- C++ Program – Fibonacci Series
- C++ Program – HCF/GCD of Two Numbers
- C++ Program – LCM of Two Numbers
- C++ Program – Sum of Natural Numbers
- C++ Program – Palindrome Number
- C++ Program – Prime Number
- C++ Program – Prime Numbers between two Numbers
- C++ Program – Reverse a Number
C++ String and Pattern Programs
After practicing numeric programs, use these examples to work with strings and nested loops.
General C++ Programs
The complete list below keeps all of the available C++ programming examples together for quick reference.
- C++ Program – Armstrong Number
- C++ Program – Average of Three Numbers
- C++ Program – Convert Decimal to Binary
- C++ Program – Factorial of a Number
- C++ Program – Factors of a Number
- C++ Program – Fibonacci Series
- C++ Program – Find the Maximum of Three Numbers
- C++ Program – Find Quotient and Remainder
- C++ Program – HCF/GCD of Two Numbers
- C++ Program – Hello World
- C++ Program – LCM of Two Numbers
- C++ Program – Leap Year Program
- C++ Program – Maximum of Three Numbers
- C++ Program – Multiply Two Numbers
- C++ Program – Sum of Two Numbers
- C++ Program – Sum of Three Numbers
- C++ Program – Sum of Natural Numbers
- C++ Program – Swap Two Numbers
- C++ Program – Palindrome Number
- C++ Program – Palindrome String
- C++ Program – Pattern Printing
- C++ Program – Power of a Number
- C++ Program – Prime Number
- C++ Program – Prime Numbers between two Numbers
- C++ Program – Print Number Entered by User
- C++ Program – Reverse a Number
How to Practice These C++ Programs
For each program, first identify the input, the expected result, and the steps required to transform the input into that result. Then write the C++ statements needed for those steps.
- Read the problem and identify the input values.
- Work out one example manually before writing code.
- Choose the required C++ concepts, such as arithmetic operators, conditions, or loops.
- Write and compile the program.
- Run the program with more than one input and compare the output with the expected result.
- After understanding the solution, rewrite the program without looking at the completed code.
C++ Program Practice Example with Input and Output
For example, suppose the task is to read two integers and print their sum. The input values are two integers, the operation is addition, and the output is their total.
#include <iostream>
int main() {
int a, b;
std::cin >> a >> b;
std::cout << a + b;
return 0;
}
Sample input:
12 8
Output:
20
The same problem-solving approach applies to the programs listed above: identify the required calculation or condition, translate it into C++ statements, and verify the result using suitable test values.
C++ Programs Editorial QA Checklist
- Check that each C++ program uses valid syntax and includes every header required by the code.
- Verify program output using the sample input shown in the corresponding example.
- For factorial, Fibonacci, prime-number, Armstrong-number, GCD, and LCM programs, test boundary cases such as
0,1, and small positive integers where applicable. - For palindrome and reverse-number programs, verify both matching and non-matching examples.
- For programs that divide numbers, confirm that invalid zero-divisor cases are handled where user input permits them.
- Check that loop conditions terminate correctly and do not skip the first or last required value.
- Confirm that the explanation matches the exact algorithm implemented in the linked C++ program.
TutorialKart.com