How Reverse a Number in C++ Program

By // No comments:
A C++ program which reverse a number enter by a user and display it on the console window.

Explanation : 


  • We take a integer input from user .
  • Use for loop and apply condition until number is zero.
  • In for loop we apply math condition to reverse whole number. In math its called  modulus.
  • When loop is end we receive reverse of input .
    ( if you input is 1234 you receive 4321 )







#include<iostream>
using namespace std;

int main() {
int number, reverse = 0;
cout<<"Please Input a Number to Reverse and press Enter: ";
 cin>> number;     // Taking Integer Input 

   for( ; number!= 0 ; )
   {
      reverse = reverse * 10;
      reverse = reverse + number%10;
      number = number/10;
   }
   cout<<"Reversed Number is:  "<<reverse<<endl;
   system("pause");

}


Image of Code :

Tick Tac Toe Game Soruce Code

By // No comments:
Tic Tac is a paper-and-pencil game for two players with X and 0 the player who  succeed in placing  3 marks in horizontally ,vertically or diagonally win the game . We convert this game In to C++.
In this  game there are two players .Code is in very simple way read the full code below.

Game is develop by students of  Comsats University





#include <iostream>

using namespace std;



char square[10] = {'o','1','2','3','4','5','6','7','8','9'};



int checkwin();

void board();



int main()

{

 int player = 1,i,choice;



 char mark;

 do

 {

  board();

  player=(player%2)?1:2;



  cout << "Player " << player << ", enter a number:  ";

  cin >> choice;



  mark=(player == 1) ? 'X' : 'O';



  if (choice == 1 && square[1] == '1')



   square[1] = mark;

  else if (choice == 2 && square[2] == '2')



   square[2] = mark;

  else if (choice == 3 && square[3] == '3')



   square[3] = mark;

  else if (choice == 4 && square[4] == '4')



   square[4] = mark;

  else if (choice == 5 && square[5] == '5')



   square[5] = mark;

  else if (choice == 6 && square[6] == '6')



   square[6] = mark;

  else if (choice == 7 && square[7] == '7')



   square[7] = mark;

  else if (choice == 8 && square[8] == '8')



   square[8] = mark;

  else if (choice == 9 && square[9] == '9')



   square[9] = mark;

  else

  {

   cout<<"Invalid move ";



   player--;

   cin.ignore();

   cin.get();

  }

  i=checkwin();



  player++;

 }while(i==-1);

 board();

 if(i==1)



  cout<<"==>\aPlayer "<<--player<<" win ";

 else

  cout<<"==>\aGame draw";



 cin.ignore();

 cin.get();

 return 0;

}



/*********************************************



 FUNCTION TO RETURN GAME STATUS

 1 FOR GAME IS OVER WITH RESULT

 -1 FOR GAME IS IN PROGRESS

 O GAME IS OVER AND NO RESULT

**********************************************/



int checkwin()

{

 if (square[1] == square[2] && square[2] == square[3])



  return 1;

 else if (square[4] == square[5] && square[5] == square[6])



  return 1;

 else if (square[7] == square[8] && square[8] == square[9])



  return 1;

 else if (square[1] == square[4] && square[4] == square[7])



  return 1;

 else if (square[2] == square[5] && square[5] == square[8])



  return 1;

 else if (square[3] == square[6] && square[6] == square[9])



  return 1;

 else if (square[1] == square[5] && square[5] == square[9])



  return 1;

 else if (square[3] == square[5] && square[5] == square[7])



  return 1;

 else if (square[1] != '1' && square[2] != '2' && square[3] != '3'

                    && square[4] != '4' && square[5] != '5' && square[6] != '6'

                  && square[7] != '7' && square[8] != '8' && square[9] != '9')



  return 0;

 else

  return -1;

}





/*******************************************************************

     FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK

********************************************************************/





void board()

{

 system("cls");

 cout << "\n\n\tTic Tac Toe\n\n";



 cout << "Player 1 (X)  -  Player 2 (O)" << endl << endl;

 cout << endl;



 cout << "     |     |     " << endl;

 cout << "  " << square[1] << "  |  " << square[2] << "  |  " << square[3] << endl;



 cout << "_____|_____|_____" << endl;

 cout << "     |     |     " << endl;



 cout << "  " << square[4] << "  |  " << square[5] << "  |  " << square[6] << endl;



 cout << "_____|_____|_____" << endl;

 cout << "     |     |     " << endl;



 cout << "  " << square[7] << "  |  " << square[8] << "  |  " << square[9] << endl;



 cout << "     |     |     " << endl << endl;

}



/*******************************************************************

    END OF PROJECT

********************************************************************/
Tic Tac Game In C++

Finding Prime Number Program

By // No comments:
A Prime Number belongs to set of  natural numbers which have two divisor 1 and itself .

Example : 2 , 3 , 5  are primes . because 3 can only divide on 1 and itself.







#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
 int number; //Declare Integer Variable
 int count = 0; //For count the divisors 

 cout << "Enter Number To Check ";
 cin >> number;

 for (int i = 1; i<=number; i++)
 {
  if (number%i == 0)
  {
   count++;
  }
 }

 if (count == 2)
 {
  cout << "\n Number is Prime" << endl;
 }
 else
 {
  cout << "\n Number is Not Prime " << endl;
 }

 getche();


}

Understanding the logic behind the program Prime Number :

  • From definition we get that the prime number have only  two dividers.
  • To check total number of divided we check it from 1 to itself.
  • In C++ Program we declare two variables "number" to get the number from user and check it
    and "count" to count the number dividers and initialize the count with 0.
  • Now we take a FOR LOOP and start from 1 to itself. In FOR LOOP we take a IF condition
    and if condition gets true its increment in count by 1.
  • When LOOP is end it go to IF condition and check if count equals to 2  it print Prime Number else not a prime .
  • The numbers which are not Prime have dividers greater than 2.

Finding Prime Number In C++


Finding Factorial In C++

By // No comments:
Definition of factorial ?
Factorial is non-integer number is the product of all number 1 to itself.
denoted by n! .

Example :  If u want to find the factorial of  number 6 then the factorial will be 
1*2*3*4*5*6= 720 .



Logic behind the factorial programming in C++ :


  1. First of all from definition we get how factorial can be calculated.
  2. Declare a variable named factorial and initialize it with 1 .
  3. Second we have to get the number from user.
  4. We have to multiply all numbers up to that given number.
    if number is 4 factorial= 1*2*3*4
  5. For this purpose For LOOP is very help full  which start from 1 and end on that number.

    Note : If we start it from 0 factorial will be zero.


#include<iostream>
#include<conio.h>
using namespace std;

int main()
{
 int number;// Get the number from user
 int factorial=1; //declare and initial factorial with 1
 
 cout<<"Enter the Number to find factorial  ";
 cin>>number;
 
 for(int i=1;i<=number;i++)
 {
  factorial=factorial*i;
 }
 
 cout<<"\n Factorial of "<<number<<"! is " <<factorial<<endl;
 
 getch();
 
}

How to change console background and text color in c++

By // 1 comment:
In c++ console window background is black and text color is white by default. You can change this color to make console window more attractive. 

Syntax:

system("color 71");

color Code in C++

This 

This statement has two parts in left side '7' is background color 
code and on right side '1' is text color code. This statement can use in main function.

You can use following color codes  :

0 = Black
1 = Blue
2 = Green
3 = Aqua
4 = Red
5 = Purple
6 = Yellow
7 = White
8 = Gray
9 = Light Blue
A= Light Green
B= Light Aqua
C= Light Red
D= Light Purple
E= Light Yellow
F= Bright White

Program:

#include <iostream>
using namespace std;
 
int main()
{
system("color71");
 
   cout << "This statement have two parts in left side 7 is"<<endl;
    cout<<"background color code and on right side 1 is text color "<<endl;
 cout<<"code and you can use this in main function" <<endl;
 
   return 0;
}

changing backgorund and text color in C++

Digital Stop-Watch Program

By // No comments:
Digital Stop watch in C++ is not very difficult using libraries and build in functions which make this program so easy to create and understand even new beginner programmers can also understand it .We create this program as easy as possible if you have any query comment below . you can also convert it to simple c .


stop watch in C++




#include<conio.h>
#include<process.h>
#include<iostream>
#include<dos.h>
using namespace std;

int h=0,m=0,s=0,ms=0; // Hours mins sec and microseconds
char ch='p'; //For Menu to start and pause stop watch

int main()
{
 void watch(); // Build in Function of watch
 watch();

  while(1)
 {
  if(kbhit())
   ch=getch();
  if(ch=='s'||ch=='S')
   break;
  if(ch=='e'||ch=='E')
   exit(0);
 }

  while(1)
 {
  watch();
  

   if(kbhit())
   ch=getch();

   if(ch=='r'||ch=='R')
  {
   h=m=s=ms=0;
   watch();

    while(1)
   {
    if(kbhit())
     ch=getch();
    if(ch=='s'||ch=='S')
     break;
    if(ch=='e'||ch=='E')
     exit(0);
   }
  }
  else
   if(ch=='p'||ch=='P')
    while(1)
    {
     if(kbhit())
      ch=getch();
     if(ch=='s'||ch=='S')
      break;
     if(ch=='e'||ch=='E')
      exit(0);
     if(ch=='r'||ch=='R')
     {
      ch='c';
      h=m=s=ms=0;
      watch();
     }
    }
   else
    if(ch=='e'||ch=='E')
     exit(0);

   if(ms!=99)
   ms++;
  else
  {
   ms=0;
   if(s!=59)
    s++;
   else
   {
    s=0;
    if(m!=59)
     m++;
    else
    {
     m=0;
     h++;
    }
   }
  }
 }
}


int watch()
{
 system("cls");
 cout<<"\n\n\n\n\n\t\t\t\t-------------";
 cout<<"\n\t\t\t\t| Stopwatch |";
 cout<<"\n\t\t\t\t-------------";
 cout<<"\n\n\t\t\t\t  "<<h<<":"<<m<<":"<<s<<":"<<ms;

    cout<<"\n\t\t\t\t\t\t\t---------";
  cout<<"\n\t\t\t\t\t\t\t Menu";
 cout<<"\n\t\t\t\t\t\t\t---------";
 cout<<"\n\t\t\t\t\t\t\ts -> Start";
 cout<<"\n\t\t\t\t\t\t\tp -> Stop";
 cout<<"\n\t\t\t\t\t\t\tr -> Reset";
 cout<<"\n\t\t\t\t\t\t\te -> Exit";
 
}


This is a requested program you can also request a program 
on our group on facebook join today 

Pacman Game In C++

By // 1 comment:
pacman is one of the most famous game in all over the world . In this game there is a pac-man ( H) and Eater (E) .Eat the dots produced by the Eater to gain points . If your pac-man get collapse with eater game will over . You move your hero with arrow keys and keep running away from eater and eat the dots to increase points . You can select one of three difficulty  modes  to make game more interesting.





This program is in object oriented approach focus on to make it easy to understand if you have any query you can comment below. 



#include <iostream>
#include <stdio.h>
#include <windows.h>
#include <string>
#include <vector>

using namespace std;
char tmp_map[18][32];

char map[18][32] = 
{
 "+#############################+",
 "|                             |",
 "|                             |",
 "|## ########### ##   #########|",
 "|   |                         |",
 "| | |### |  |           |     |",
 "| |      |  | |###  |   |  |  |",
 "| | #####|  | |      ## |     |",
 "| |           |###  |      |  |",
 "| |##### ###         ##       |",
 "|          ######  ####### ###|",
 "|                             |",
 "|# ### ####      ###   #######|",
 "|                             |",
 "|                             |",
 "|                             |",
 "|                             |",
 "+#############################+"
 };

void ShowMap()
{
 for(int i = 0; i < 18; i++) 
 {
 printf("%s\n",map[i]) ;
 }
}

void gotoxy( short x, short y )
{
    HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE) ;
    COORD position = { x, y } ;

    SetConsoleCursorPosition( hStdout, position ) ;
}

class entity 
{
public:
 entity( int x, int y )
 {
  this ->x = x;
  this ->y = y;
 }

 void move_x( int p )

 {
  if( map[y][x+p] == ' ' ) x += p;
 }

 void move_y( int p )
 {
  if( map[y+p][x] == ' ' ) y += p;
 }

 void move( int p, int q ){
  x += p;
  y += q;
 }

 int get_x(){ return x; }
 int get_y(){ return y; }

 void draw( char p ){
  map[x][y] = p;
  gotoxy( x, y ); printf( "%c", p );
 }

private:
 int x;
 int y;
};

struct walk {
 short walk_count;
 short x;
 short y;
 short back;
};

struct target {
 short x;
 short y;
};

vector<target> walk_queue;

vector<walk> BFSArray;

void AddArray( int x, int y, int wc , int back )
{
 if( tmp_map[y][x] == ' ' || tmp_map[y][x] == '.' )
 {
  tmp_map[y][x] = '#';
  walk tmp;
  tmp.x = x;
  tmp.y = y;
  tmp.walk_count = wc;
  tmp.back = back;
  BFSArray.push_back( tmp );
 }
}

void FindPath( int sx, int sy, int x, int y ){
 memcpy( tmp_map, map, sizeof(map) );
 BFSArray.clear();
 walk tmp;
 tmp.x = sx;
 tmp.y = sy;
 tmp.walk_count = 0;
 tmp.back = -1;
 BFSArray.push_back( tmp );

 int i = 0;
 while( i < BFSArray.size() ){
  if( BFSArray[i].x == x && BFSArray[i].y == y ){
   walk_queue.clear();
   target tmp2;
   while( BFSArray[i].walk_count != 0 ){
    tmp2.x = BFSArray[i].x;
    tmp2.y = BFSArray[i].y;
    walk_queue.push_back( tmp2 );

    i = BFSArray[i].back;
   }

   break;
  }

  AddArray( BFSArray[i].x+1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
  AddArray( BFSArray[i].x-1, BFSArray[i].y, BFSArray[i].walk_count+1, i );
  AddArray( BFSArray[i].x, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
  AddArray( BFSArray[i].x, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );

  /*
   AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x+1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );

   AddArray( BFSArray[i].x+1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y+1, BFSArray[i].walk_count+1, i );
   AddArray( BFSArray[i].x-1, BFSArray[i].y-1, BFSArray[i].walk_count+1, i );
  */
  i++;
 }

 BFSArray.clear();
}


int main()
{
    bool running = true;
 int x = 15; // hero x
 int y = 16; // hero y
 int old_x;
 int old_y;

 int ex = 1;
 int ey = 1;


 int pts = 0;

 cout<<"Instruction:\n1. Arrow Keys to move your hero\n2. Eat the dots produced by the Eater to gain poins\n3. Don't get caught by the Eater\n\n";
 cout<<"H -> Hard\nN -> Normal\nE -> Easy\n\nInput : ";

 char diffi;
 int speedmod = 3;

 cin >> diffi;

 if( diffi == 'N' ){
  speedmod = 2;
 }else if( diffi == 'H' ){
  speedmod = 1;
 }

 system("cls");
    ShowMap();

 gotoxy( x, y ); cout << "H";

 int frame = 0;

 FindPath( ex,ey,x,y );

 while( running ){
  gotoxy( x, y ); cout << " ";

  old_x = x;
  old_y = y;

  if ( GetAsyncKeyState( VK_UP ) ){
   if( map[y-1][x] == '.' ){ y--; pts++; } else
   if( map[y-1][x] == ' ' ) y--;
  }
  if ( GetAsyncKeyState( VK_DOWN ) ){
   if( map[y+1][x] == '.' ){ y++; pts++; } else
   if( map[y+1][x] == ' ' ) y++;
  }
  if ( GetAsyncKeyState( VK_LEFT ) ){
   if( map[y][x-1] == '.' ){ x--; pts++; } else
   if( map[y][x-1] == ' ' ) x--;
  }
  if ( GetAsyncKeyState( VK_RIGHT ) ){
   if( map[y][x+1] == '.' ){ x++; pts++; } else
   if( map[y][x+1] == ' ' ) x++;
  }

  if( old_x != x || old_y != y ){
   FindPath( ex,ey,x,y );
  }

  gotoxy( x,y ); cout << "H";

  map[ey][ex] = '.';
  gotoxy( ex, ey ); cout << ".";

  if( frame%speedmod == 0 && walk_queue.size() != 0 ){
   ex = walk_queue.back().x;
   ey = walk_queue.back().y;
   walk_queue.pop_back();
  }

  gotoxy( ex, ey ); cout << "E";

  if( ex == x && ey == y ){
   break;
  }


  gotoxy( 32, 18 );
  gotoxy( 32, 1 ); cout << pts;
  Sleep( 100 );
  frame++;
 }

 system("cls");
 cout<<"You Lose and your score is : "<< pts<<endl ;
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();
 cin.get();

 return 0;
}