C++ Programming Examples
Writing C++ programs yourself is the best way to learn the C++ language. C++ programs are also asked in the interviews. This article covers the top practice problems for basic C++ programs on topics like control flow, patterns, and functions to complex ones like pointers, arrays, and strings.

Basic C++ Programs
Start with foundational tasks such as printing statements, taking input, and performing basic arithmetic operations:
- Print Hello World
- Print Your Own Name
- Get Input from the User
- Sum of Two Numbers
- Swap two numbers
- Find the Size of int, float, double, and char
- Multiply Two Floating-Point Numbers
- Print the ASCII Value of a Character
- Convert Fahrenheit to Celsius
- Calculate Simple Interest
- Calculate Compound Interest
- Calculate Area and Perimeter of Rectangle
C++ Control Flow Programs
After basic operations, this section introduces decision-making structures and looping techniques that allow you to control the flow of your program.
- Check Number is Even or Odd
- Find Largest Number Among 3 Numbers
- Check Whether a Character is a Vowel or Consonant
- Check if a Given Year is a Leap Year
- Print Multiplication Table of a Number
- Calculate Sum of First n Natural Numbers
- Find Factorial of a Number
- Reverse a Number
- Find GCD
- Find LCM
- Check Whether a Number is a Palindrome or Not
- Check Whether a Number is Prime or Not
- Display Prime Numbers Between Two Intervals
- Check Neon Numbers in a Given Range
- Check Armstrong Number
- Display Armstrong Numbers Between 1 to 1000
- Find nth Fibonacci Number
- Sum of Fibonacci Numbers at Even Indexes up to N Terms
- Calculate the Power of a Number
- Display Factors of a Natural Number
- Make a Simple Calculator
C++ Pattern Printing Programs
Once you're familiar with loops, it's time to apply them by solving problems that involve printing various patterns and shapes.
- Print Right Half Pyramid Pattern
- Print Left Half Pyramid Pattern
- Print Simple Full Pyramid Pattern
- Print Inverted Pyramid
- Print Triangle Pattern
- Print Number Pattern without reassigning
- Print Character Pattern
- Print Continuous Character Pattern
- Print Full Diamond Shape Pyramid
- Print Inverted Hollow Star Pyramid Pattern
- Print Hollow Star Pyramid in a Diamond Shape
- Print Pascal's Triangle
- Print Floyd's pattern Triangle Pyramid
- Print Reverse Floyd Pattern Triangle Pyramid
C++ Function Programs
Now that you’re comfortable with patterns, move on to writing functions, exploring overloading, recursion, and how functions improve code reusability.
- Display Prime Numbers Between Two Intervals Using Function
- Check Whether a Number Can be Express as Sum of Two Prime Numbers
- Find the Sum of Natural Numbers using Recursion
- Calculate the Factorial of a Number Using Recursion
- Reverse a Sentence Using Recursion
- Calculate Power Using Recursion
- Variadic Function Templates
C++ Array Programs
This section focuses on problems related to arrays, including initialization, traversal, manipulation, and operations like summing elements or finding the largest/smallest element.
- Check if Two Arrays Are Equal or Not
- Maximum and Minimum in an Array
- Calculate the Average of all the Elements Present in an Array
- Merge Two Arrays
- Print a 2D Array
- Find Common Array Elements
- Remove Duplicate Elements From an Array
- Remove All Occurrences of an Element in an Array
- Prefix Sum Array
- Rotate the Array D times
- Copy All the Elements of One Array to Another in the Reverse Order
C++ Matrix Programs
After arrays, extend your skills to 2D matrices, learning how to handle more complex data structures and perform matrix operations.
- Add Two Matrices
- Check Whether Two Matrices Are Equal or Not
- Compute the Sum of Diagonals of a Matrix
- Print Boundary Elements of a Matrix
- Find the Transpose of a Matrix
- Find the Determinant of a Matrix
- Find the Normal and Trace of Matrix
- Multiply Two Matrices
- Rotate Matrix Elements of a Matrix
- Interchange Elements of First And Last Rows in Matrix
- Prefix sum of Matrix
C++ Pointers Programs
With arrays and matrices understood, explore memory management with pointers, learning dynamic allocation and manipulation of memory.
- C++ Program for Pointers
- C++ Program for an Array of Pointers
- C++ Program for void Pointer
- C++ Program for Reference To a Pointer
- C++ Program for Function Pointer
- C++ Program for this Pointer
- C++ Program For Opaque Pointer
C++ String Programs
Problems in this section focus on string manipulation, including operations like concatenation, comparison, reversal, and finding substrings.
- Find the Length of a String
- Access any Characters in a Given String
- Determine the Unicode Code Point at a given index
- Replace a Character in a String
- Compare Two Strings
- Add/Concatenate Two Strings
- Add 2 Binary Strings
- Remove Leading Zeros
- Compare Two Strings Lexicographically
- Reverse a String
- check if the String is Palindrome
- Print the First Letter of Each Word of a String
- Insert a String into Another String
- Splitting into a Number of Sub-Strings
- Reverse a String Using Stacks
- Check Whether the Given String is Pangram
C++ Conversion Programs
In this section we tackle data conversion problems, like changing between different types and formats, to better handle user input and outputs.
- Binary to Decimal Conversion
- Binary to Octal Conversion
- Octal to Decimal Conversion
- Decimal to Octal Conversion
- Hexadecimal to Decimal Conversion
- Decimal to Hexadecimal Conversion
- Decimal to Binary Conversion
- Boolean to String Conversion
- String to Double Conversion
- Double to String Conversion
- String to Long Conversion
- Long to String Conversion
- Int to Char Conversion
- Char to Int Conversion
C++ Structures Programs
Now that you know how to handle basic data types, explore structures to group related data together and manage complex data more effectively.
- Pass or Return a Structure to/from a Function
- Store Information of a Student in a Structure
- Structure Sorting (By Multiple Rules)
C++ Class and Object Programs
With structures covered, learn object-oriented programming by creating classes and objects, which allow you to model real-world entities.
- Create a Class and Object
- Show Encapsulation
- Show Inheritance
- Show Abstraction in Class
- Show Data Hiding in Class
- Show Polymorphism in Class
- Show Function Overloading
- Show Function Overriding
- Show Usage of Access Modifier
- Show Use of This Keyword in Class
- Show Usage of Static keyword
- Friend Functions
- Virtual Destructor
- Create Abstract Class
- Create Singleton Class
- Create an Interface
- Overload Increment ++ and Decrement
- Add Two Complex Numbers
C++ STL Programs
Explore the Standard Template Library (STL) to use pre-built data structures and algorithms that simplify your code and improve performance.
- Reverse A Vector Using STL
- Copy A Vector Using STL
- Check If Two Vectors Contain the Same Elements or Not
- Convert a Vector of Pairs to a Map
- Show transform () Using STL
- Merge Two STL Maps
- Stack Of Pair Using STL
- Find the Frequency of an Element in a Multiset
- How to Initialize Multiset with Custom Comparator
- Find Permutations of a Given String Using STL
- Find All Permutations of an Array Using STL.
- Find Maximum and Minimum Elements in a Set Using STL
- C++ Program to Find Next Greater Element
- Reverse Priority
- Implement Binary Search Functions Using STL
C++ Searching and Sorting Programs
In this section, we implement algorithms for searching and sorting data, optimizing performance and efficiency.
- Search an Element in an Array (Linear Search)
- Search an Element in an Array (Binary Search)
- Sort a String
- Sort the 2D Array Across Rows
- Sort the Elements of an Array in Descending Order
- Sort the Elements of an Array in Ascending Order
- Sort an Array of 0s, 1s, and 2s
- C++ Program to Sort an array in wave form
- Sorting array except elements in a subarray
- Search an element in a sorted and rotated array
C++ File Handling Programs
After mastering classes and objects, dive into file handling, enabling your programs to read from and write to external files for persistent storage.
- Create a New File
- Create a Temporary File
- Write Into a File
- Rename a File
- Make a File Read-Only
- Compare Paths of Two Files
- Copy one File into Another File
- Append the Content of One Text File to Another
- Get the List of Files in a Directory
- Append a String in an Existing File
- Read Content From One File and Write it into Another File
C++ Exception Handling Programs
As your programs become more complex, learn to handle errors gracefully with exception handling, ensuring your code can manage unexpected conditions.
- Show Runtime Exceptions
- Show Types of Errors
- Handle the Exception Methods
- Handle the Exception Methods
- Handle the Checked Exceptions
- Handle the Unchecked Exceptions
- Handle Divide by Zero and Multiple Exceptions
- Show Unreachable Code Error
- Show Thread Interface and Memory Consistency Errors
C++ Date and Time Programs
In this section, we see how to handle data and times in our programs.
- Display Dates of Calendar Year in Different Formats
- Display Current Date and Time
- Convert the Local Time to GMT
C++ Miscellaneous Programs
- C++ Program to Find Quotient and Remainder
- How to use sizeof() Operator
- Find Initials of a Name
- Find Power Without Using * and / Operators
- Find the Roots of the Quadratic Equation
- Generate Random Double Numbers in C++
- How to Hide and Show a Console Window in C++?
- How to Run a C++ Program Without Namespace?
- Build a custom Map using a Header File in C++
- Number of Unique Triplets Whose XOR is Zero