- C++ Programming Examples
- C++ Programming Examples
- C++: Hello World
- C++: Get Input
- C++: Print Integer
- C++: Add two numbers
- C++: Add, Sub, Multiply, Div
- C++: Add Digits
- C++: Find Average and Percentage
- C++: Find Arithmetic Mean
- C++: Sum of n Natural Numbers
- C++: Sum of n Numbers
- C++: Square's Area and Perimeter
- C++: Rectangle's Area and Perimeter
- C++: Triangle's Area and Perimeter
- C++: Area and Circumference
- C++: Find Simple Interest
- C++: Fahrenheit to Celsius
- C++: Celsius to Fahrenheit
- C++: Print Prime Numbers
- C++: Reverse a Number
- C++: Swap Two Numbers
- C++: Print Multiplication Table
- C++: Find Factorial of a Number
- C++: Find Factors of a Number
- C++: Find HCF and LCM
- C++: Create a Calculator
- C++: Count Digits in a Number
- C++: First and Last Digit Sum
- C++: Product of Number Digits
- C++: Sum of Squares of Digits
- C++: Interchange Digits of Number
- C++ if-else Programs
- C++: Check Even or Odd
- C++: Check Prime or Not
- C++: Check Alphabet or Not
- C++: Check Vowel or Not
- C++: Check Leap Year or Not
- C++: Check Reverse equals Original
- C++: Check Perfect Number
- C++: Check Palindrome or Not
- C++: Check Armstrong or Not
- C++: Divisibility Test
- C++: Find Labor Wage
- C++: Find Discounted Price
- C++: Find Shipping Charge
- C++: Find Telephone Bills
- C++: Calculate Student Grade
- C++: Largest of Two Numbers
- C++: Largest of Three Numbers
- C++ Number Conversion
- C++: Decimal to Binary
- C++: Decimal to Octal
- C++: Decimal to Hexadecimal
- C++: Binary to Decimal
- C++: Binary to Octal
- C++: Binary to Hexadecimal
- C++: Octal to Decimal
- C++: Octal to Binary
- C++: Octal to Hexadecimal
- C++: Hexadecimal to Decimal
- C++: Hexadecimal to Binary
- C++: Hexadecimal to Octal
- C++ Pattern Programs
- C++: Pattern Programs
- C++: Print Diamond Pattern
- C++: Print Floyd's Triangle
- C++: Print Pascal's Triangle
- C++ Array Programs
- C++: 1D Array Program
- C++: Linear Search
- C++: Binary Search
- C++: Largest Element in an Array
- C++: Smallest Element in an Array
- C++: Find Second Largest Element
- C++: Find Second Smallest Element
- C++: Sum of All Elements
- C++: Multiply All Elements
- C++: Element in Even Position
- C++: Element in Odd Position
- C++: Print Even Numbers in Array
- C++: Print Odd Numbers in Array
- C++: Count Even or Odd Numbers
- C++: Sum of Even or Odd Numbers
- C++: Count Positive, Negative, Zero
- C++: Reverse an Array
- C++: Insert an Element
- C++: Delete an Element
- C++: Merge two Arrays
- C++: Bubble Sort
- C++: Selection Sort
- C++: Insertion Sort
- C++: Common Elements
- C++: 2D Array Programs
- C++: Add Two Matrices
- C++: Subtract Two Matrices
- C++: Transpose Matrix
- C++: Multiply Two Matrices
- C++: 3D Array Programs
- C++ String Programs
- C++: Print String
- C++: Find String Length
- C++: Compare Two Strings
- C++: Copy String
- C++: String Concatenation
- C++: Reverse a String
- C++: Delete Vowels from a String
- C++: Delete a Word from a String
- C++: Count Characters in a String
- C++: Count Words in a String
- C++: Frequency of Words
- C++: Remove Spaces from Strings
- C++: Sort a String
- C++: Uppercase to Lowercase
- C++: Lowercase to Uppercase
- C++: Swap Two Strings
- C++: Check the Anagram or Not
- C++: Capitalize All Words in a String
- C++: Get Numbers from a String
- C++ File Programs
- C++: Read a File
- C++: Write Content to a File
- C++: Append Data to a File
- C++: Read and Display File
- C++: Copy a File
- C++: Merge Two Files
- Count Characters in a File
- C++: Capitalize Every Word
- C++: List Files in Directory
- C++: Delete a File
- C++: Encrypt and Decrypt a File
- C++ Misc Programs
- C++: Print ASCII Value
- C++: Add Binary Numbers
- C++: Generate Random Numbers
- C++: Print a Smiling Face
- C++: Days into Years and Months
- C++: Add Two Numbers using Pointer
- C++: Print Fibonacci Series
- C++: Generate Armstrong Numbers
- C++: Find nCr and nPr
- C++: Get IP Address
- C++: Print Date and Time
- C++: Shutdown and Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
C++ Programming Examples
C++ program examples given here help you learn C++ programming practically.
Since there are an extensive number of programs that can be created using C++. Therefore, we have created a series of C++ programming examples that are divided into more than 100 articles. In each article, you will get one or more programs with well-explained code and its output.
We've listed more than 500 C++ programs along with their outputs, from the simplest one to shutting down your computer using a C++ program.
From the list of all programs, here is a list of some popular programs in C++:
- Add two Numbers
- Check Leap Year or Not
- Make Simple Calculator
- Interchange Digits of Number
- Find Largest of three Numbers
- Find Factorial of a Number
- Check Armstrong Number or Not
- Convert Decimal Number to Binary
- Insert Element in an Array
- Delete Element from an Array
- Delete Word from a String
- Encrypt & Decrypt Files
Note: Each and every program in C++ given here has been well tested and executed. You can check it out for yourself by visiting some of the programs given above.
But before starting the series of C++ programming examples, Let's first go through some of the interesting programs given in this article. Let's start with the simplest C++ program, as shown in the following example.
C++ Program Example 1
Here is the simplest C++ program that will print the string, "Hello Compiler, I am C++," on the output. Let's have a look at the program given below:
// C++ Programming Example No.1 #include<iostream> using namespace std; int main() { cout<<"Hello Compiler, I am C++"; cout<<endl; return 0; }
This program was built and runs under the Code::Blocks IDE. Here is its sample output:
The iostream is a header file that stands for input/output stream and provides basic input/output services for the C++ program. Like in the above program, cout is used to put some content on the output screen. It is defined in the iostream header file.
The following C++ statement:
using namespace std;
is used to provide standard (std) input and output namespaces. That is, after including this statement, we do not need to write std:: before every cout and cin.
endl stands for "end of line", and it is used to break the line and begin the next thing on a new line.
C++ Program Example 2
Here is the second example in C++ programming.
// C++ Programming Example No.2 #include<iostream> using namespace std; int main() { char str[30]; cout<<"Enter Your Name: "; cin>>str; cout<<"\nHello "<<str<<" Sir, This is fresherearth.com!"; cout<<endl; return 0; }
This is the initial output produced by the above program:
Now supply your name, say Albert, and press the ENTER key to see the following output:
C++ Program Example 3
Let's take a look at the third example of C++ given below. This program receives a number as input from the user and checks whether it is greater than 10 and less than 100 or not to print a message accordingly.
// C++ Programming Example No.3 #include<iostream> using namespace std; int main() { int num; cout<<"Gues a Number: "; cin>>num; if(num>10 && num<100) cout<<"\nWhat a True Guess!"; else cout<<"\nOpps!"; cout<<endl; return 0; }
Here is its sample run with user input: 14.
And here is another sample run with user input: 3.
C++ Program Example 4
This is the fourth example program in C++. This program asks the user to create a password and then receives input as two numbers. However, before adding these two numbers and printing the result, the program prompts you to enter the password you created earlier (in the same program):
If the user enters the correct password, then the program calculates and displays the result; otherwise, it doesn't display the result, but rather shows a message like "Wrong Password!"
// C++ Programming Example No. 4 #include<iostream> #include<string.h> using namespace std; int main() { char pass[20], ePass[20]; int numOne, numTwo, sum; cout<<"Create a Password: "; cin>>pass; cout<<"\nEnter Two Numbers to Add: "; cin>>numOne>>numTwo; cout<<"\nEnter the Password to See the Result: "; cin>>ePass; if(!strcmp(pass, ePass)) { sum = numOne + numTwo; cout<<endl<<numOne<<" + "<<numTwo<<" = "<<sum; } else cout<<"\nSorry! You've entered a Wrong Password!"; cout<<endl; return 0; }
Here is a sample run of this C++ program. Below is the initial output:
Now supply the following inputs:
- To create a new password, use fresherearth.
- 40 and 60 as two numbers
- fresherearth as a password to see the sum of two numbers entered
Here is the output after providing these inputs:
If you enter the wrong password, then you will not see the result; rather, the output produced by the above program is "Sorry! You've entered the wrong password!".
C++ Program Example 5
This is the fifth example program in C++. This program confirms whether the user is a robot or not.
// C++ Programming Example No. 5 #include<iostream> #include<string.h> using namespace std; int main() { char robotChk[10]; int val; cout<<"Are You a Robot ? "; cin>>robotChk; val = strcmp("yes", robotChk); if(val==0) cout<<"\nYou can't Proceed!"; else cout<<"\nYou're Welcome!"; cout<<endl; return 0; }
Here is the sample run of the above C++ program with user input, yes:
And here is another sample run with user input, no:
C++ Program Example 6
This is the last (sixth) example program in C++ that asks to enter the name of the user to check whether he or she is invited or not.
// C++ Programming Example No.6 #include<iostream> #include<string.h> using namespace std; int main() { char arr[6][20] = {"programmer", "developer", "engineer", "student", "teacher", "professor"}; char name[25]; int found=0; cout<<"Who are You ? "; cin>>name; for(int i=0; i<6; i++) { if(!strcmp(name, arr[i])) { cout<<"\nCongratulation!"; cout<<"\nYou're invited from fresherearth.com"; found=1; break; } else found++; } if(found != 1) cout<<"\nSorry!\nYou're not invited"; cout<<endl; return 0; }
Here is its sample run with user input, programmer:
Note: Don't panic if you're not getting an idea of what is going on behind the C++ codes given above. Because this is just a program that shows you how it looks. Starting with a very simple program, printing "Hello, world," to an advanced level program, you will learn about all of these things one by one in the following program (article).
For TurboC++ User
Because all programs given here are well-tested and compiled through the Code::Blocks IDE, Therefore, if you are a TurboC++ user, it is better to skip it and continue with the IDE used here. But if you don't, then do these things:
- Include .h after every header file's name. For example, replace iostream with iostream.h. If the header file already contains.h, do not make the change.
- Remove the statement, using namespace std;
- Change int main() to void main().
- getch() should be used instead of return 0.
Other Languages Programming Examples
You may also like to go through and learn about all these programs in other languages, such as:
« C++ Tutorial Next Program »