The Wayback Machine - https://web.archive.org/web/20241109074742/https://www.geeksforgeeks.org/cpp-programming-examples/
Open In App

C++ Programming Examples

Last Updated : 15 Mar, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

C++ Programming Examples

C++ Tutorial

C++ Recent Articles

Topics:





 
 

Basic C++ Programs

  1. C++ Program For Hello World
  2. C++ Program to Print Your Own Name
  3. C++ Program to Get Input from the User
  4. C++ Program to Read Number Input From User
  5. C++ Program to Add Two Numbers
  6. C++ Program to Swap two numbers
  7. C++ Program to Find the Size of int, float, double, and char
  8. C++ Program to Multiply Two Floating-Point Numbers  
  9. C++ Program to Print the ASCII Value of a Character 
  10. C++ Program to Calculate Fahrenheit to Celsius
  11. C++ Program to Find Simple Interest
  12. C++ Program to Find Compound Interest
  13. C++ Program For Area And Perimeter of Rectangle

C++ Control Flow Programs

  1. C++ Program to Check Even or Odd Integers
  2. C++ Program to Find Largest Among 3 Numbers
  3. C++ Program to Check Whether a Character is a Vowel or Consonant
  4. C++ Program to Check if a Given Year is a Leap Year
  5. C++ Program to Print Multiplication Table of a Number
  6. C++ Program to Calculate Sum of First n Natural Numbers
  7. C++ Program to Find Factorial of a Number
  8. C++ Program to Reverse a Number
  9. C++ Program to Find GCD
  10. C++ Program to Find LCM
  11. C++ Program to Check Whether a Number is a Palindrome or Not
  12. C++ Program to Check Whether a Number is Prime or Not
  13. C++ Program to Display Prime Numbers Between Two Intervals
  14. C++ Program to Check Neon Numbers in a Given Range
  15. C++ Program to Check Armstrong Number
  16. C++ Program to Display Armstrong Numbers Between 1 to 1000
  17. C++ Program to For Fibonacci Number
  18. C++ Sum of Fibonacci Numbers at Even Indexes up to N Terms
  19. C++ Program to Calculate the Power of a Number
  20. C++ Program to Display Factors of a Natural Number
  21. C++ Program to Make a Simple Calculator

C++ Pattern Printing Programs

  1. C++ Program To Print Right Half Pyramid Pattern
  2. C++ Program To Print Left Half Pyramid Pattern
  3. C++ Program To Print Simple Full Pyramid Pattern
  4. C++ Program To Print Inverted Pyramid
  5. C++ Program To Print Triangle Pattern
  6. C++ Program To Print Number Pattern without reassigning
  7. C++ Program To Print Character Pattern
  8. C++ Program To Print Continuous Character Pattern
  9. C++ Program To Print Full Diamond Shape Pyramid
  10. C++ Program To Print Inverted Hollow Star Pyramid Pattern 
  11. C++ Program To Print Hollow Star Pyramid in a Diamond Shape
  12. C++ Program To Print Pascal’s Triangle
  13. C++ Program To Print Floyd’s pattern Triangle Pyramid
  14. C++ Program To Print Reverse Floyd Pattern Triangle Pyramid

C++ Function Programs

  1. C++ Program to Display Prime Numbers Between Two Intervals Using Function
  2. C++ Program to Check Whether a Number Can be Express as Sum of Two Prime Numbers
  3. C++ Program to Find the Sum of Natural Numbers using Recursion
  4. C++ Program to Calculate the Factorial of a Number Using Recursion
  5. C++ Program to Reverse a Sentence Using Recursion
  6. C++ Program to Calculate Power Using Recursion
  7. C++ Program For Variadic Function Templates

C++ Array Programs

  1. C++ Program to Check if Two Arrays Are Equal or Not
  2. C++ Program to Find the Maximum and Minimum in an Array
  3. C++ Program to Calculate the Average of all the Elements Present in an Array
  4. C++ Program to Merge Two Arrays
  5. C++ Program to Print a 2D Array
  6. C++ Program to Find Common Array Elements
  7. C++ Program to Remove Duplicate Elements From an Array
  8. C++ Program to Remove All Occurrences of an Element in an Array
  9. C++ Program For Array Rotation
  10. C++ Program to Copy All the Elements of One Array to Another in the Reverse Order

C++ Matrix Programs

  1. C++ Program to Add Two Matrices
  2. C++ Program to Check Whether Two Matrices Are Equal or Not
  3. C++ Program to Compute the Sum of Diagonals of a Matrix
  4. C++ Program to Print Boundary Elements of a Matrix
  5. C++ Program to Find the Transpose of a Matrix
  6. C++ Program to Find the Determinant of a Matrix
  7. C++ Program to Find the Normal and Trace of Matrix
  8. C++ Program to Multiply Two Matrices
  9. C++ Program to Rotate Matrix Elements of a Matrix
  10. C++ Program to Interchange Elements of First And Last Rows in Matrix
  11. C++ Program To Interchange Elements of First And Last Columns In Matrix

C++ Pointers Programs

  1. C++ Program for Pointers
  2. C++ Program for an Array of Pointers
  3. C++ Program for void Pointer
  4. C++ Program for Reference To a Pointer
  5. C++ Program for Function Pointer
  6. C++ Program for this Pointer
  7. C++ Program For Opaque Pointer

C++ String Programs

  1. C++ Program to Find the Length of a String
  2. C++ Program to Access Characters in a Given String
  3. C++ Program to Determine the Unicode Code Point at a given index
  4. C++ Program to Replace a Character in a String
  5. C++ Program to Compare Two Strings
  6. C++ Program to Add/Concatenate Two Strings
  7. C++ Program to Add 2 Binary Strings
  8. C++ Program to Remove Leading Zeros
  9. C++ Program to Compare Two Strings Lexicographically
  10. C++ Program to Reverse a String
  11. C++ Program to check if the String is Palindrome
  12. C++ Program to Print the First Letter of Each Word of a String
  13. C++ Program to Insert a String into Another String
  14. C++ Program to Splitting into a Number of Sub-Strings
  15. C++ Program to Reverse a String Using Stacks
  16. C++ Program to Check Whether the Given String is Pangram

C++ Conversion Programs

  1. C++ Program For Binary to Decimal Conversion
  2. C++ Program For Binary to Octal Conversion
  3. C++ Program For Octal to Decimal Conversion
  4. C++ Program For Decimal to Octal Conversion
  5. C++ Program For Hexadecimal to Decimal Conversion
  6. C++ Program For Decimal to Hexadecimal Conversion
  7. C++ Program For Decimal to Binary Conversion
  8. C++ Program For Boolean to String Conversion
  9. C++ Program For String to Double Conversion
  10. C++ Program For Double to String Conversion
  11. C++ Program For String to Long Conversion
  12. C++ Program For Long to String Conversion
  13. C++ Program For Int to Char Conversion
  14. C++ Program For Char to Int Conversion

C++ Searching and Sorting Programs

  1. C++ Program to Search an Element in an Array (Linear Search)
  2. C++ Program to Search an Element in an Array (Binary Search)
  3. C++ Program to Sort an Array(Selection Sort) 
  4. C++ Program to Sort an Array(Bubble Sort)
  5. C++ Program to Sort an Array(Insertion Sort)
  6. C++ Program of Merge Sort
  7. C++ Program to Sort a String
  8. C++ Program to Sort the 2D Array Across Rows
  9. C++ Program to Sort the Elements of an Array in Descending Order
  10. C++ Program to Sort the Elements of an Array in Ascending Order

C++ Structures Programs

  1. C++ Program to Pass or Return a Structure to/from a Function
  2. C++ Program to Store Information of a Student in a Structure
  3. C++ Program For Structure Sorting (By Multiple Rules)

C++ Class and Object Programs

  1. C++ Program to Create a Class and Object
  2. C++ Program to Show Encapsulation
  3. C++ Program to Show Inheritance
  4. C++ Program to Show Abstraction in Class
  5. C++ Program to Show Data Hiding in Class
  6. C++ Program to Show Polymorphism in Class
  7. C++ Program to Show Function Overloading
  8. C++ Program to Show Function Overriding
  9. C++ Program to Show Usage of Access Modifier
  10. C++ Program to Show Use of This Keyword in Class
  11. C++ Program to Show Usage of Static keyword
  12. C++ Program For Friend Functions
  13. C++ Program For Virtual Destructor
  14. C++ Program to Create Abstract Class
  15. C++ Program to Create Singleton Class
  16. C++ Program to Create an Interface
  17. C++ Program to Overload Increment ++ and Decrement
  18. C++ Program to Add Two Complex Numbers

C++ File Handling Programs

  1. C++ Program to Create a New File
  2. C++ Program to Create a Temporary File
  3. C++ Program to Write Into a File
  4. C++ Program to Rename a File
  5. C++ Program to Make a File Read-Only
  6. C++ Program to Compare Paths of Two Files
  7. C++ Program to Copy one File into Another File
  8. C++ Program to Append the Content of One Text File to Another
  9. C++ Program to Get the List of Files in a Directory
  10. C++ Program to Append a String in an Existing File
  11. C++ Program to Read Content From One File and Write it into Another File

C++ Exception Handling Programs

  1. C++ Program to Show Runtime Exceptions
  2. C++ Program to Show Types of Errors
  3. C++ Program to Handle the Exception Methods
  4. C++ Program to Handle the Exception Methods
  5. C++ Program to Handle the Checked Exceptions
  6. C++ Program to Handle the Unchecked Exceptions
  7. C++ Program to Handle Divide By Zero and Multiple Exceptions
  8. C++ Program to Show Unreachable Code Error
  9. C++ Program to Show Thread Interface and Memory Consistency Errors

C++ STL Programs

  1. C++ Program to Sort an Array Using STL
  2. C++ Program To Initialize A Vector
  3. C++ Program To Copy A Vector Using STL
  4. C++ Program For Merge Operations Using STL
  5. C++ Program To Show transform() Using STL
  6. C++ Program For Deque Using STL
  7. C++ Program For Priority Queue Using STL
  8. C++ Program For Map Using STL
  9. C++ Program For Pair Using STL
  10. C++ Program For Multiset using STL
  11. C++ Program To Reverse A Vector Using STL
  12. C++ Program To Reverse An Array Using STL
  13. C++ Program For Stack Of Pair Using STL
  14. C++ Program To Find Permutations Of A Given String Using STL
  15. C++ Program To Find All Permutations of an Array Using STL.
  16. C++ Program To Find Maximum And Minimum Elements In a Set Using STL
  17. C++ Program To Insert And Delete Elements in a Set Using STL
  18. C++ Program To Find Sum Of Elements Of a Vector Using STL
  19. C++ Program To Implement Different Methods To Copy in STL
  20. C++ Program To Implement Binary Search Functions Using STL
  21. C++ Program To Check If Two Vectors Contain the Same Elements Or Not

C++ Date and Time Programs

  1. C++ Program to Display Dates of Calendar Year in Different Formats
  2. C++ Program to Display Current Date and Time
  3. C++ Program to Convert the Local Time to GMT

C++ Miscellaneous Programs

  1. C++ Program to Find Quotient and Remainder
  2. C++ Program for sizeof() Operator
  3. C++ Program to Find Initials of a Name
  4. C++  Program to Find Power Without Using * and / Operators
  5. C++ Program to Find the Roots of the Quadratic Equation
  6. Generate Random Double Numbers in C++
  7. How to Hide and Show a Console Window in C++?
  8. How to Run a C++ Program Without Namespace?
  9. Build a custom Map using a Header File in C++
  10. C++ Program for Number of Unique Triplets Whose XOR is Zero
     


Previous Article
Next Article

Similar Reads

Introduction to Parallel Programming with OpenMP in C++
Parallel programming is the process of breaking down a large task into smaller sub-tasks that can be executed simultaneously, thus utilizing the available computing resources more efficiently. OpenMP is a widely used API for parallel programming in C++. It allows developers to write parallel code easily and efficiently by adding simple compiler dir
5 min read
Comparison of Java with other programming languages
Java is one of the most popular and widely used programming language and platform. A platform is an environment that helps to develop and run programs written in any programming language. Java is fast, reliable and secure. From desktop to web applications, scientific supercomputers to gaming consoles, cell phones to the Internet, Java is used in ev
4 min read
Web Programming in C++
CGI(COMMON GATEWAY INTERFACE) may be a set of standards that outline however data is changed from the online server, passing the online user’s request to Associate in Nursing application and to receive data back to the user. When any user requests for a web page, the server sends back the requested page. The Web server typically passes the form inf
5 min read
Hello World Program : First program while learning Programming
In this article, I'll show you how to create your first Hello World computer program in various languages. Along with the program, comments are provided to help you better understand the terms and keywords used in theLearning program. Programming can be simplified as follows: Write the program in a text editor and save it with the correct extension
6 min read
Importance of Testing In Competitive Programming
Many times while solving problems, we face issues like time limit exceeds, wrong solution, runtime error, and memory limit exceeds because after designing algorithm we don't test algorithm efficiency, correctness, time complexity and running the algorithm on a large set of inputs for testing purpose. This is where stress testing comes for rescue. S
3 min read
Comparison of Python with Other Programming Languages
Python is an easily adaptable programming language that offers a lot of features. Its concise syntax and open-source nature promote readability and implementation of programs which makes it the fastest-growing programming language in current times. Python has various other advantages which give it an edge over other popular programming languages su
3 min read
Writing code faster during Competitive Programming in C++
This article focuses on how to implement your solutions and implement them fast while doing competitive programming. Setup Please refer Setting up a C++ Competitive Programming Environment Snippets Snippet is a programming term for a small region of re-usable source Code. A lot of modern text editors like Sublime provide you a functionality to auto
3 min read
Top 10 Programming Languages for Blockchain Development
Do you ever wonder about the sudden emergence and popularity of Blockchain? What is it? Well, Blockchain is literally a chain of blocks as the name suggests! It’s just that the “blocks” in this context are digital information that is connected using cryptography. Each of these blocks contains a cryptographic hash function linking to the previous bl
7 min read
Why Java Language is Slower Than CPP for Competitive Programming?
Choosing the appropriate language while starting competitive programming is the most important factor. Generally, we choose that language that has short syntax and executes very fast or which is familiar to us, and we know all the working patterns of that particular whether it is Java or C++. Most of the programmers use C++ for competitive programm
5 min read
How to Use int as long long int for Competitive Programming?
Most of the time, input constraints in Competitive programming questions are bigger than the limits of int. Therefore, there is a need to use long int or even long long int. Here let us take two cases so that if a naive user writes correct logic still input is not getting accepted can get to know where the rectification is required. Case 1: Big int
3 min read
Different Ways to Setting Up Environment For C++ Programming in Mac
Xcode is an IDE developed by Apple themselves to develop apps for macOS and iOS or all other Operating Systems that Apple develops. It also has support for C/C++ built-in. Here, finding a C++ IDE for a macOS system is quite easy. To run a program in C++ in Mac we have to install Xcode or command-line tools for Xcode. Ways: Hence, there are two opti
3 min read
Most Critical Mistakes & Tips in Competitive Programming
The moment a beginner hears this word, an image pops up in one's mind where students are sitting in a room full of Computers and coding some out of the world stuff, We'll to be honest it's nothing too hard to grasp so we will be proposing tips that help a programmer to level up faster. So let's begin with the most underrated points on each topic wh
15+ min read
Setting up Sublime Text For Competitive Programming (C++) Using Fast Olympic Coding Plugin
Competitive Programming is a mind sport where time matters the most. For that, a fast and easy-to-use tool is required for writing code. Sublime text along with the CppFastOlympicCoding plugin provides the same. Sublime text is popular for its fast performance and aesthetic UI. The CppFastOlympicCoding plugin offers features such as: TestManager: W
4 min read
Which C++ libraries are useful for competitive programming?
C++ is one of the most recommended languages in competitive programming (please refer our previous article for the reason) C++ STL contains lots of containers which are useful for different purposes. In this article, we are going to focus on the most important containers from competitive programming and interview preparation point of view. vector :
3 min read
Introduction to GUI Programming in C++
In C++, Graphical User Interface (GUI) programming is important in modern application development where users have nice graphics for them to work with. Although C++ is commonly linked with system programming and game writing, it can be an excellent alternative to GUI writing. In this article, we will discuss GUI programming in C++, some popular GUI
5 min read
Tips and Tricks for Competitive Programmers | Set 2 (Language to be used for Competitive Programming)
This is a question asked quite often as in which language should be preferred to be efficient in competitive programming. It is something one should not worry about as what matters is the logic, not the language. Most of the languages are more or less same, but till now the most preferred language is C++, and here are the reasons. Python Simple and
5 min read
getchar_unlocked() – Faster Input in C/C++ For Competitive Programming
getchar_unlocked() is similar to getchar() with the exception that it is not thread-safe. This function can be securely used in a multi-threaded program if and only if they are invoked when the invoking thread possesses the (FILE*) object, as is the situation after calling flockfile() or ftrylockfile(). Syntax: int getchar_unlocked(void); Example:
2 min read
Bitwise Hacks for Competitive Programming
Prerequisite: It is recommended to refer Interesting facts about Bitwise Operators How to set a bit in the number 'num': If we want to set a bit at nth position in the number 'num', it can be done using the 'OR' operator( | ). First, we left shift '1' to n position via (1<<n)Then, use the 'OR' operator to set the bit at that position. 'OR' op
14 min read
Writing C/C++ code efficiently in Competitive programming
First of all you need to know about Template, Macros and Vectors before moving on the next phase! Templates are the foundation of generic programming, which involve writing code in a way that is independent of any particular type.A Macro is a fragment of code which has been given a name. Whenever the name is used, it is replaced by the contents of
6 min read
C++ tricks for competitive programming (for C++ 11)
We have discussed some tricks in the below post. In this post, some more tricks are discussed. Writing C/C++ code efficiently in Competitive programming Although, practice is the only way that ensures increased performance in programming contests but having some tricks up your sleeve ensures an upper edge and fast debugging. 1) Checking if the numb
5 min read
Common mistakes to be avoided in Competitive Programming in C++ | Beginners
Not using of 1LL or 1ll when needed // A program shows problem if we // don't use 1ll or 1LL #include <iostream> using namespace std; int main() { int x = 1000000; int y = 1000000; // This causes overflow even // if z is long long int long long int z = x*y; cout << z; return 0; } Output: -727379968 You might get some other negative valu
7 min read
Programming puzzle (Assign value without any control statement)
Given four integers 'a', 'b', 'y' and 'x', where 'x' can only be either zero or one. Your task is as follows: If 'x' is zero assign value 'a' to the variable 'y'If 'x' is one assign value 'b' to the variable 'y'. It is not allowed to use any conditional operator (including the ternary operator). Examples : Input : a = 3, b = 7, x = 1 Output : y = 7
6 min read
How to Setup VSCode with C, C++ and Python for Competitive Programming
VSCode is a Text editor that provides support for development operations and version control systems. It provides tools for a user to build hassle-free codes. VSCode can be downloaded and installed from visualstudio.com This article will show you how to, fetch test cases directly from the browser without copy-pasting and run them in VSCode with jus
5 min read
C++ Programming and STL Facts
C++ is widely used for competitive programming. It is preferred because of its reliability, efficient execution, short snippets, etc. It has become adaptive by most coders as it also provides the benefits of Standard Template Library(STL). C++ STL is the backbone of programming. The inbuilt functions reduce a code snippet to even a single line some
5 min read
Implement Secure Hashing Algorithm - 512 ( SHA-512 ) as Functional Programming Paradigm
Given a string S of length N, the task is to find the SHA-512 Hash Value of the given string S. Examples: Input: S = "GeeksforGeeks"Output: acc10c4e0b38617f59e88e49215e2e894afaee5ec948c2af6f44039f03c9fe47a9210e01d5cd926c142bdc9179c2ad30f927a8faf69421ff60a5eaddcf8cb9c Input: S = "hello world"Output:309ecc489c12d6eb4cc40f50c902f2b4d0ed77ee511a7c7a9bc
15 min read
10 C++ Programming Tricks That You Should Know
C++ Programming Language is a powerful, versatile, and compiled language, which means the source code is converted into machine language before the execution. In order to make your code as efficient and effective as possible, you should be aware of the following tricks and techniques. Hence, it's better to know some C++ tricks and tips to reduce a
10 min read
Customized Debugging in Sublime Text using C++ for Competitive Programming
Competitive Programming is a mental sport that enables us to code a given problem under provided constraints. The purpose of this article is to guide every individual on how they can debug their code efficiently during a contest.Prerequisite: Setting up Sublime Text for C++ Competitive Programming Environment Time is something that is precious and
15+ min read
Socket Programming in C/C++: Handling multiple clients on server without multi threading
This tutorial assumes you have a basic knowledge of socket programming, i.e you are familiar with basic server and client model. In the basic model, server handles only one client at a time, which is a big assumption if you want to develop any scalable server model. The simple way to handle multiple clients would be to spawn new thread for every ne
9 min read
Role of SemiColon in various Programming Languages
Semicolon is a punctuation mark (;) indicating a pause, typically between two main clauses, that is more pronounced than that indicated by a comma. In programming, Semicolon symbol plays a vital role. It is used to show the termination of instruction in various programming languages as well, like C, C++, Java, JavaScript and Python. In this article
5 min read
Fast I/O for Competitive Programming
In competitive programming, it is important to read input as fast as possible so we save valuable time. You must have seen various problem statements saying: " Warning: Large I/O data, be careful with certain languages (though most should be OK if the algorithm is well designed)" . The key for such problems is to use Faster I/O techniques. It is of
4 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg