codescracker


c++

C++ Programming Examples



« C++ Tutorial Next Program »


C++ programs helps you to learn C++ programming practically. So here, we listed more than 100 C++ programs along with their output from simplest C++ program to shutdown your computer using C++ programs.

Here, we included two function clrscr() and getch() (mainly for Turbo C++ user) in approximately every C++ programs. The function clrscr() of conio.h library is used to clear (clean or format) the output screen to watch the program's output only. And the function getch() of conio.h library is used to hold the output screen (at last of the program or when the program finished) until a key is pressed, after pressing the key, your output screen closed and you will backed to the source code.

Let's start from simplest C++ program, given here in the following program.

C++ Program Example 1

Following is the simplest C++ program which will print the string "Hello Compiler, I am C++" on the output screen as shown here:

/* C++ Programming Examples */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();  // clear the screen
	cout<<"Hello Compiler, I am C++";
	getch();  // output screen hold until user press a key
}

When the above C++ program is compile and executed, it will produce the following result:

c++ programming examples with output

C++ Program Example 2

/* C++ Programming Examples */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	char str[20];
	cout<<"Enter your name : ";
	cin>>str;
	cout<<"Hello, "<<str<<" Sir, You are at codescracker.com";
	getch();
}

When you compile and run the above C++ program, you will get the following output after the sample run:

c++ programming examples

C++ Program Example 3

/* C++ Programming Examples */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int a=10, num;
	cout<<"Guess a Number : ";
	cin>>num;
	if(num>10 && num<100)
	{
		cout<<"What a mind!!";
	}
	else
	{
		cout<<"Opps..!!";
	}
	getch();
}

Here is the sample run of the above C++ program. Here is the 1st run:

c++ programs

This is the second run:

c++ simplest program

C++ Program Example 4

/* C++ Programming Examples */
		
#include<iostream.h>
#include<conio.h>
void main()
{
	clrscr();
	int a=10, b;
	cout<<"Enter 10:";
	cin>>b;
	if(b==a)
	{
		cout<<"What a good Employee!!";
	}
	else
	{
		cout<<"Sorry!, you are not selected..!!";
	}
	getch();
}

Here is the sample run of the above C++ program. Below is the first run:

c++ programs list

Now, here is the second run:

c++ all programs

C++ Program Example 5

/* C++ Programming Examples */
		
#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
	clrscr();
	char robchk[10];

	// answer in yes or other/no
	cout<<"Are you a Robot ? ";
	cin>>robchk;
	int a = strcmp("yes", robchk);
	if(a==0)
	{
		cout<<"Sorry!!.. You can't proceed.";
	}
	else
	{
		cout<<"Congrats!!. You can proceed.";
	}
	getch();
}

Here is the sample run of the above C++ program. Following is the first run:

c++ program check robot

Now is the second run:

c++ example

Other Languages Programming Examples

You may also like to go through all these programs list in other languages such as:


« C++ Tutorial Next Program »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test