codescracker
c++

C++ Programming Examples



« C++ Tutorial Next Program »


C++ program examples given here, helps you to learn C++ programming practically.

Because there are a lot 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 the program on many topics one by one, 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. Each article contains one or more programs.

From the series of all programs, here are the list of some popular programs in C++:

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, given 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:

c++ programming examples with 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:

c++ programming examples

Now supply your name say Hritik and press ENTER key to see the following output:

example programs in c++

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:

c++ programs

And here is another sample run with user input, 3:

c++ simplest program

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:

c++ programs list

Now supply the following inputs:

Here is the output after providing these inputs:

c++ all programs

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:

c++ program check robot

And here is another sample run with user input, no:

c++ example

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:

c++ programming examples with output

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:

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 »