- C++ Programming Examples
- C++ Programming Examples
- C++ Hello World
- C++ Get Input
- C++ Print Integer
- C++ Add Two Numbers
- C++ Add Sub Mul Div
- C++ Add Digits
- C++ Find Average Perc
- C++ Find Arithmetic Mean
- C++ Sum of n Natural Numbers
- C++ Sum of n Numbers
- C++ Area Perimeter of Square
- C++ Area Perimeter of Rectangle
- C++ Area Perimeter of Triangle
- C++ Area Circum of Circle
- 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 Number
- C++ Find Factors of Number
- C++ Find HCF & LCM
- C++ Make Calculator
- C++ Count Digits in Number
- C++ Sum of First & Last Digit
- C++ Product of Digits of Number
- 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
- Check Reverse equal Original
- C++ Check Perfect Number
- C++ Check Palindrome or Not
- C++ Check Armstrong or Not
- C++ Divisibility Test
- C++ Find Wage of Labor
- 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
- Find Largest Element in Array
- Find Smallest Element in Array
- Find Second Largest Element
- Find Second Smallest Element
- C++ Sum of All Elements
- C++ Multiply All Elements
- C++ Element on Even Position
- C++ Element on Odd Position
- C++ Print Even Numbers in Array
- C++ Print Odd Numbers in Array
- C++ Count Even/Odd Numbers
- C++ Sum of Even/Odd Numbers
- C++ Count Positive Negative Zero
- C++ Reverse an Array
- C++ Insert Element in Array
- C++ Delete Element from Array
- 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 Length of String
- C++ Compare Two Strings
- C++ Copy String
- C++ Concatenate String
- C++ Reverse a String
- C++ Delete Vowels from String
- C++ Delete Word from String
- C++ Count Character in String
- C++ Count Word in String
- C++ Frequency of Word
- C++ Remove Spaces from String
- C++ Sort a String
- C++ Uppercase to Lowercase
- C++ Lowercase to Uppercase
- C++ Swap Two Strings
- C++ Check Anagram or Not
- C++ Capitalize All Words in String
- C++ Capitalize Specific Character
- C++ Get Numbers from String
- C++ File Programs
- C++ Read a File
- C++ Write Content to File
- C++ Append Data in File
- C++ Read & Display File
- C++ Copy a File
- C++ Merge Two Files
- Count Characters, Words in File
- C++ Capitalize All Words in File
- C++ List Files in Directory
- C++ Delete a File
- C++ Encrypt & Decrypt a File
- C++ Misc Programs
- C++ Print ASCII Value
- C++ Add Binary Numbers
- C++ Generate Random Numbers
- C++ Print Smiling Face
- C++ Days into Years, Months
- Add Two Numbers using Pointer
- C++ Print Fibonacci Series
- Generate Armstrong Numbers
- C++ Find nCr and nPr
- C++ Get IP Address
- C++ Print Date/Time
- C++ Shutdown, Restart Computer
- C++ Programming Tutorial
- C++ Tutorial
- C++ Programming Test
- C++ Programming Test
- Give Online Test
- All Test List
C++ Programming Examples
C++ program examples given here, helps you to learn C++ programming practically.
Since there are a 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 its output from simplest one to shutdown your computer using C++ program.
From the series of all programs, here are the 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 are well tested and executed. You can check it out with yourself through visiting some of the programs given above.
But before starting the series of C++ programming examples. Let's first go through some interesting programs given in this article. Let's start with the simplest C++ program, here 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 // ----codescracker.com---- #include<iostream> using namespace std; int main() { cout<<"Hello Compiler, I am C++"; cout<<endl; return 0; }
This program was build and run under Code::Blocks IDE. Here is its sample output:
The iostream is a header file, stands for input/output stream, provides basic input/output services for the C++ program. Like in above program, cout is used to put some content on output screen. It is defined in iostream header file.
The following C++ statement:
using namespace std;
is used to provide standard (std) input/output namespace. That is, after including this statement, we do not need to write std:: before every cout and cin
The endl stands for end of line, used to break the line and start next thing from new line.
C++ Program Example 2
Here is the second example in C++ programming.
// C++ Programming Example No.2 // ----codescracker.com---- #include<iostream> using namespace std; int main() { char str[30]; cout<<"Enter Your Name: "; cin>>str; cout<<"\nHello "<<str<<" Sir, This is codescracker.com!"; cout<<endl; return 0; }
This is the initial output produced by above program:
Now supply your name say Albert and press ENTER
key to see the following output:
C++ Program Example 3
Let's have a look at the third example of C++ given below. This program receives a number as input from user, and checks whether it is greater than 10 and less than 100 or not, to print message accordingly:
// C++ Programming Example No.3 // ----codescracker.com---- #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 of C++. This program asks from user to create a password, and then receives inputs as two numbers. But before adding these two numbers and printing its addition on output, the program asks to enter the password, that was created earlier (in the same program):
If user enters the correct password, then program calculates and displays the result, otherwise it doesn't displays the result, rather it shows a message like Wrong Password!:
// C++ Programming Example No. 4 // ----codescracker.com---- #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 the sample run of this C++ program. Below is the initial output:
Now supply the following inputs:
- codescracker as new password to create
- 40 and 60 as two numbers
- codescracker as password to see the addition result of entered two numbers
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 above program is Sorry! You've entered a Wrong Password!
C++ Program Example 5
This is the fifth example program of C++. This program confirms whether the user is a robot or not.
// C++ Programming Example No. 5 // ----codescracker.com---- #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 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 of C++ that asks to enter the name of user to check whether he/she is invited or not.
// C++ Programming Example No.6 // --codescracker.com-- #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 codescracker.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 be panic, if you're not getting an idea that what is going behind the C++ codes given above. Because these are just a program to show you how it looks like. From next program (article), you will learn about all these things one by one, starting with very simple program, that is, printing hello world to an advance level program.
For TurboC++ User
Because all programs given here are well-tested and compiled through Code::Blocks IDE. Therefore, if you are a TurboC++ user, then 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 header file already written with .h then don't make change
- Remove the statement, using namespace std;
- Replace int main() with void main()
- Replace return 0; with getch();
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 »