Showing posts with label c. Show all posts
Showing posts with label c. Show all posts

Wednesday, February 18, 2015

C Program for Tower of Hanoi Problem

The Tower of Hanoi (also called the Tower of Brahma or Lucas Tower and sometimes pluralised) is a mathematical game or puzzle. It consists of three rods, and a number of disks of different sizes which can slide onto any rod. The puzzle starts with the disks in a neat stack in ascending order of size on one rod, the smallest at the top, thus making a conical shape as shown in below image.


C Program for Tower of Hanoi Problem


Also Read: Graphs: Introduction and Terminology
Also Read: C Program to Create a Binary Tree Using Recursion [Linked Representation]

The objective of the puzzle is to move the entire stack to another rod, obeying the following simple rules:
  1. Only one disk can be moved at a time.
  2. Each move consists of taking the upper disk from one of the stacks and placing it on top of another stack i.e. a disk can only be moved if it is the uppermost disk on a stack.
  3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum number of moves required to solve a Tower of Hanoi puzzle is 2n-1, where n is the number of disks.

Animated solution of the Tower of Hanoi Puzzle for T(4,3)


Tower of Hanoi Problem Animation


C Program using recursion is given below which finds solution for Tower of Hanoi Problem

#include<stdio.h>

void TOH(int,char,char,char);

void main()
{
    int n;
    printf("How many plates?");
    scanf("%d",&n);
    TOH(n,A,B,C);
}

void TOH(int n,char x,char y,char z)
{
    if(n>0)
    {
        TOH(n-1,x,z,y);
        printf("
%c -> %c",x,y);

        TOH(n-1,z,y,x);
    }
}

Output

Source: Wikipedia

6J4XXVUMND9H
Read more »

Tuesday, February 17, 2015

C program to print the following design


C++ program to print the following design:

#include<iostream.h>
#include<conio.h>

void main()
{
clrscr(); //to clear the screen
int i,j,k,n;
  cout<<"How many lines?";
cin>>n;
  n*=2;

for(i=0;i<n;i+=2)
{
cout<<"
";

for(j=1;j<i;j+=2)
cout<<" ";
for(k=n-1;k>i;--k)
cout<<"*";
}
getch(); //to stop the screen
}
Read more »

C program to print following trianlge using character

C program to print following trianlge using character *

#include<stdio.h>
#include<conio.h>

void main()
{
int i,j,k,n;
clrscr(); //to clear the screen
printf("How many lines?");
scanf("%d",&n);

for(i=0;i<n;++i)
{
printf("
");

for(j=0;j<i;++j)
printf(" ");
for(k=n;k>i;--k)
printf("*");
}
getch(); //to stop the screen
}
Read more »

Monday, February 16, 2015

C program to convert decimal number to binary number

C program to convert decimal number to binary number

#include<stdio.h>

#include<conio.h>

void main()

{
int d,n,i,j,a[50];
clrscr();

printf("Enter a number:");

scanf("%d",&n);

if(n==0)

 printf("
The binary conversion of 0 is 0"):

else
{
printf("
The binary conversion of %d is 1",n);


for(i=1;n!=1;++i)

{
d=n%2;
a[i]=d;
n=n/2;
}

for(j=i-1;j>0;--j)

printf("%d",a[j]);
} getch();
}
Read more »

C program to print the truth table for XY Z

C program to print the truth table for XY+Z

#include<stdio.h>
#include<conio.h>

void main()
{
int x,y,z;
clrscr(); //to clear the screen
printf("X Y Z XY+Z");

for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf("

%d %d %d 1",x,y,z);

else
printf("

%d %d %d %d",x,y,z,x*y+z);

}
getch(); //to stop the screen
}
Read more »

C program to find average of list of numbers entered through keyboard


#include<stdio.h>
#include<conio.h>

void main()
{
int i,n,sum=0,e;
float avg;
clrscr(); //to clear the screen

  printf("How many elements:");
scanf("%d",&n);
printf("Enter all the elements one by one:");
for(i=0;i<n;++i)
{
scanf("%d",&e);
sum+=e;
}
avg=sum/n;
printf("
Average=%f",avg);

getch(); //to stop the screen
}
Read more »

Sunday, February 15, 2015

C Program to convert a decimal number to binary number


#include<iostream.h>
#include<conio.h>

void main()
{
clrscr();
int d,n,i,j,a[50];

cout<<"Enter a number:";
cin>>n; cout<<"
The binary conversion of "<<n<<" is 1";
for(i=1;n!=1;++i)
{
d=n%2;
a[i]=d;
n=n/2;
}

for(j=i-1;j>0;--j)
cout<<a[j];

getch();

}
Read more »

C C TIC TAC TOE GAME

This is the main code of my C/C++ TIC-TAC-TOE GAME using the boolean algorithm without calculating all the probability. Using the rand() function to maintain the IA and forcing to play some cases with a chance of 99% , the other 1% is to make the game dynamic (realistic). Custom Cursor Algorithm by using simple loops and some C++ Basic Features. Have FUN!

Also Read: High/Low Game
Also Read: C++ Program to create an Analog Clock

C/C++ TIC-TAC-TOE GAME

C/C++ TIC-TAC-TOE GAME



#include <stdio.h>
#include <time.h>
#include <Windows.h>
#include <stdlib.h>
#include <conio.h>


void setcolor(unsigned short color)
{
    HANDLE hcon = GetStdHandle(STD_OUTPUT_HANDLE);
    SetConsoleTextAttribute(hcon,color);
}


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

    SetConsoleCursorPosition( hStdout, position ) ;
}

void swap(int *first, int *second)
{
    int temp = *first;
    *first = *second;
    *second = temp;
}


 int SYGLE[3][3] = { ,-,-,-,-,-,-,-,-, };
 bool BOOLSWITCH[3][3]= { true, true, true ,true , true , true , true , true , true };
 bool SWITCHPLACE [40][15];

 int compteur=0;

 int score_IA=0;
 int score_P =0;

 int count_P=0;
 int ROLL_;
 int ROLL;

 int x;
 int y;
 char tempo;

 int H=30;
 int V=5;

 char drawing[11];

const void draw()
{
  int b = 28;
  int a = 5;

  drawing[0]=205;
  drawing[1]=186;
  drawing[2]=188;
  drawing[3]=187;
  drawing[4]=200;
  drawing[5]=201;
  drawing[6]=206;
  drawing[7]=204;
  drawing[8]=185;
  drawing[9]=202;
  drawing[10]=203;

 for(a=3 ; a < 16 ; a++)
 {
   gotoxy(b,a);
   printf("%c",drawing[1]);

   if(b==40 && a==15)
   goto end;

   if(a==15)
   {
    a=3;
    b=b+4;
   }

 }

end: ;

 a=3;
 for(b=28 ; b < 41 ; b++)
 {
   gotoxy(b,a);
   printf("%c",drawing[0]);

   if(b==40 && a==15)
   goto end2;

   if(b==40)
   {
    b=27;
    a=a+4;
   }

 }
end2: ;

gotoxy(28,3);
printf("%c",drawing[5]);
gotoxy(40,3);
printf("%c",drawing[3]);
gotoxy(28,15);
printf("%c",drawing[4]);
gotoxy(40,15);
printf("%c",drawing[2]);

gotoxy(28,11);
printf("%c",drawing[7]);
gotoxy(28,7);
printf("%c",drawing[7]);

gotoxy(40,11);
printf("%c",drawing[8]);
gotoxy(40,7);
printf("%c",drawing[8]);

gotoxy(32,7);
printf("%c",drawing[6]);
gotoxy(36,7);
printf("%c",drawing[6]);
gotoxy(32,11);
printf("%c",drawing[6]);
gotoxy(36,11);
printf("%c",drawing[6]);


gotoxy(32,3);
printf("%c",drawing[10]);
gotoxy(36,3);
printf("%c",drawing[10]);
gotoxy(32,15);
printf("%c",drawing[9]);
gotoxy(36,15);
printf("%c",drawing[9]);
}

void reboot()
{
    SYGLE[0][0]=-;
    SYGLE[1][0]=-;
    SYGLE[2][0]=-;
    SYGLE[0][1]=-;
    SYGLE[1][1]=-;
    SYGLE[2][1]=-;
    SYGLE[0][2]=-;
    SYGLE[1][2]=-;
    SYGLE[2][2]=-;

    BOOLSWITCH[0][0]=-;
    BOOLSWITCH[1][0]=-;
    BOOLSWITCH[2][0]=-;
    BOOLSWITCH[0][1]=-;
    BOOLSWITCH[1][1]=-;
    BOOLSWITCH[2][1]=-;
    BOOLSWITCH[0][2]=-;
    BOOLSWITCH[1][2]=-;
    BOOLSWITCH[2][2]=-;

    SWITCHPLACE[30][5]=false;
    SWITCHPLACE[34][5]=false;
    SWITCHPLACE[38][5]=false;
    SWITCHPLACE[30][9]=false;
    SWITCHPLACE[34][9]=false;
    SWITCHPLACE[38][9]=false;
    SWITCHPLACE[30][13]=false;
    SWITCHPLACE[34][13]=false;
    SWITCHPLACE[38][13]=false;

    H=30;
    V=5;
    x=0;
    y=0;
}

const void CHECK()
{
            if(H==30 && V == 5)                                                  // Where to Switch
            {
             x=0;
             y=0;
            }

            if( H==34 && V == 5)
            {
             x=1;
             y=0;
            }
            if( H==38 && V == 5)
            {
             x=2;
             y=0;
            }
            if( H==30 && V == 9)
            {
             x=0;
             y=1;
            }
            if( H==34 && V == 9)
            {
             x=1;
             y=1;
            }
            if( H==38 && V == 9)
            {
             x=2;
             y=1;
            }
            if( H==30 && V == 13)
            {
             x=0;
             y=2;
            }
            if( H==34 && V == 13)
            {
             x=1;
             y=2;
            }
            if( H==38 && V == 13)
            {
             x=2;
             y=2;
            }
}

 void gotoxied()
{
    srand (time(NULL));

    for( x ; x<=2 ;x++ )
    {
        setcolor(3);
        gotoxy(H,V);
        printf("%c",SYGLE[x][y]);
        H=H+4;
        if(x==2)
        {
            x=-1;
            y=y+1;
            H=30;
            V=V+4;
        }
        if(y==3)
        break;
    }

    gotoxy(30,5);
      H=30;
      V=5;
      x=0;
      y=0;
}


 void IA()
 {

  SWITCHPLACE[H][V];
  ROLL = 1+rand() %100 ;
  //ROLL_ = 1+rand() %100 ;


while(1)
{

setcolor(9);
if(count_P >= 2)
{
 if(ROLL > 1)                                                                                        // DEF IA
 {
      if(SYGLE[0][y] == X && SYGLE[1][y] == X && BOOLSWITCH[2][y]==true)                    // Case 1 FOR CURSOR
     {
        SYGLE[2][y]=O;
        gotoxy(38,V);
        printf("%c",SYGLE[2][y]);
        H=38;
        x=2;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[2][y]=false;
        break;
     }

      if(SYGLE[2][y] == X && SYGLE[1][y] == X && BOOLSWITCH[0][y]==true)                    // Case 2 FOR CURSOR
     {
        SYGLE[0][y]=O;
        gotoxy(30,V);
        printf("%c",SYGLE[0][y]);
        H=30;
        x=0;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[0][y]=false;
        break;
     }

       if(SYGLE[x][0] == X && SYGLE[x][1] == X && BOOLSWITCH[x][2]==true)                    // Case 3 FOR CURSOR
     {
        SYGLE[x][2]=O;
        gotoxy(H,13);
        printf("%c",SYGLE[x][2]);
        V=13;
        y=2;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[x][2]=false;
        break;
     }

       if(SYGLE[x][2] == X && SYGLE[x][1] == X && BOOLSWITCH[x][0]==true)                    // Case 4 FOR CURSOR
     {
        SYGLE[x][0]=O;
        gotoxy(H,5);
        printf("%c",SYGLE[x][0]);
        V=5;
        y=0;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[x][0]=false;
        break;
     }

       if(SYGLE[1][1] == X && SYGLE[2][0] == X && BOOLSWITCH[0][2]==true)                    // Case Diagonale 1 FOR CURSOR
     {
        SYGLE[0][2]=O;
        gotoxy(30,13);
        printf("%c",SYGLE[0][2]);
        V=13,H=30;
        x=0,y=2;


        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[0][2]=false;
        break;
     }

        if(SYGLE[0][2] == X && SYGLE[1][1] == X && BOOLSWITCH[2][0]==true)                    // Case Diagonale 2 FOR CURSOR
     {
        SYGLE[2][0]=O;
        gotoxy(38,5);
        printf("%c",SYGLE[2][0]);
        V=5,H=38;
        x=2,y=0;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[2][0]=false;
        break;
     }

         if(SYGLE[0][0] == X && SYGLE[1][1] == X && BOOLSWITCH[2][2]==true)                    // Case Diagonale 3 FOR CURSOR
     {
        SYGLE[2][2]=O;
        gotoxy(38,13);
        printf("%c",SYGLE[2][2]);
        V=13,H=38;
        x=2,y=2;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[2][2]=false;
        break;
     }

        if(SYGLE[2][2] == X && SYGLE[1][1] == X && BOOLSWITCH[0][0]==true)                    // Case Diagonale 4 FOR CURSOR
     {
        SYGLE[0][0]=O;
        gotoxy(30,5);
        printf("%c",SYGLE[0][0]);
        V=5,H=30;
        x=0,y=0;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[0][0]=false;
        break;
     }

       if(SYGLE[0][y] == X && SYGLE[2][y] == X && BOOLSWITCH[1][y]==true)                    // Case Midle Normal
     {
        SYGLE[1][y]=O;
        gotoxy(34,V);
        printf("%c",SYGLE[1][y]);
        H=34;
        x=1;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[1][y]=false;
        break;
     }

       if(SYGLE[x][2] == X && SYGLE[x][0] == X && BOOLSWITCH[x][1]==true)                    // Case Midle Normal
     {
        SYGLE[x][1]=O;
        gotoxy(H,9);
        printf("%c",SYGLE[x][1]);
        V=9;
        y=1;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[x][1]=false;
        break;
     }

      if(SYGLE[0][2] == X && SYGLE[2][0] == X && BOOLSWITCH[1][1]==true)                    // Case Midle diago /
     {
        SYGLE[1][1]=O;
        gotoxy(34,9);
        printf("%c",SYGLE[1][1]);
        V=9,H=34;
        y=1,x=1;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[1][1]=false;
        break;
     }

       if(SYGLE[0][0] == X && SYGLE[2][2] == X && BOOLSWITCH[1][1]==true)                    // Case Midle diago
     {
        SYGLE[1][1]=O;
        gotoxy(34,9);
        printf("%c",SYGLE[1][1]);
        V=9,H=34;
        y=1,x=1;

        gotoxy(H,V);
        SWITCHPLACE[H][V]=true;
        BOOLSWITCH[1][1]=false;
        break;
     }

 }
     else
     goto begin;

}

begin: ;                                                                                // Basic first random part
    setcolor(9);
    x = rand() %3;
    y = rand() %3;

    H = rand() %9 + 30;
    V = 5+ rand() %13;

    if( H == 30 && V == 5 || H==34 && V == 5 || H == 38 && V==5 || H == 30 && V==9 || H == 34 && V==9 || H == 38 && V==9 || H == 30 && V==13 || H == 34 && V==13 || H == 38 && V==13)

       if(SWITCHPLACE[H][V] !=true && BOOLSWITCH[x][y]!=false)
       {

            CHECK();

            SYGLE[x][y]=O;
            gotoxy(H,V);
            printf("%c",SYGLE[x][y]);
            gotoxy(H,V);

            SWITCHPLACE[H][V] = true;
            BOOLSWITCH[x][y] =false;

            goto end;
        }
  }

 end: ;
}                                                                                                 /// End basic part


 void PlayerT()
 {

     if(x==3)
     {
      x=x-1;
      goto inner;
     }

      else
      setcolor(4);
      SYGLE[x][y]=X;
      SWITCHPLACE[H][V]=true;
      printf("%c",SYGLE[x][y]);
      BOOLSWITCH[x][y]=false;
      gotoxy(H,V);
      count_P++;

 inner: ;
 }


static void todown()
{

if(y==2)
goto inner;

for(y;y<3;y++)
{

             if(BOOLSWITCH[x][y] == false && BOOLSWITCH[x][y+1] == true )
             {

                SYGLE[x][y+1]= ;
                gotoxy(H,V+4);
                printf("%c",SYGLE[x][y+1]);
                gotoxy(H,V+4);
                y=y++;
                V=V+4;

                break;

             }

             if(BOOLSWITCH[x][y] == true && BOOLSWITCH[x][y+1] == false )
             {

                SYGLE[x][y]=-;
                printf("%c",SYGLE[x][y]);
                gotoxy(H,V+4);
                y=y++;
                V=V+4;

                break;

             }

             if(BOOLSWITCH[x][y]==false && BOOLSWITCH[x][y+1]== false)
             {

                gotoxy(H,V+4);
                y=y++;
                V=V+4;

                break;

             }

        if(SYGLE[x][2]!= )                 /// BEGINING DIRECTION
        {

             swap(&SYGLE[x][y],&SYGLE[x][y+1]);
             gotoxy(H,V);
             printf("%c",SYGLE[x][y]);
             gotoxy(H,V+4);
             printf("%c",SYGLE[x][y+1]);
             gotoxy(H,V+4);
             V=V+4;
             y=y++;
             break;
        }

}

inner: ;
}

static void toup()
{

if(y==0)
goto inner;

 for(y;y>=0;y--)
{
             if(BOOLSWITCH[x][y] == false && BOOLSWITCH[x][y-1] == true )
             {

                SYGLE[x][y-1]= ;
                gotoxy(H,V-4);
                printf("%c",SYGLE[x][y-1]);
                gotoxy(H,V-4);
                y=y--;
                V=V-4;

                break;

             }

             if(BOOLSWITCH[x][y] == true && BOOLSWITCH[x][y-1] == false )
             {

                SYGLE[x][y]=-;
                printf("%c",SYGLE[x][y]);
                gotoxy(H,V-4);
                y=y--;
                V=V-4;

                break;

             }

             if(BOOLSWITCH[x][y]==false && BOOLSWITCH[x][y-1]== false)
             {

                gotoxy(H,V-4);
                y=y--;
                V=V-4;

                break;

             }


            if(SYGLE[x][0]!= )
            {

             swap(&SYGLE[x][y],&SYGLE[x][y-1]);
             gotoxy(H,V);
             printf("%c",SYGLE[x][y]);
             gotoxy(H,V-4);
             printf("%c",SYGLE[x][y-1]);
             gotoxy(H,V-4);
             V=V-4;
             y=y--;
             break;

            }
}

inner: ;
}


static void toleft()
{
if(x==0)
goto inner;

for(x;x>=0;x--)
{

           if(BOOLSWITCH[x][y] == false && BOOLSWITCH[x-1][y] == true )
             {

                SYGLE[x-1][y]= ;
                gotoxy(H-4,V);
                printf("%c",SYGLE[x-1][y]);
                gotoxy(H-4,V);
                x=x--;
                H=H-4;

                break;

             }

             if(BOOLSWITCH[x][y] == true && BOOLSWITCH[x-1][y] == false )
             {

                SYGLE[x][y]=-;
                printf("%c",SYGLE[x][y]);
                gotoxy(H-4,V);
                x=x--;
                H=H-4;

                break;

             }

             if(BOOLSWITCH[x][y]==false && BOOLSWITCH[x-1][y]== false)
             {

                gotoxy(H-4,V);
                x=x--;
                H=H-4;

                break;

             }

            if(SYGLE[0][y]!= )
            {

             swap(&SYGLE[x][y],&SYGLE[x-1][y]);
             gotoxy(H,V);
             printf("%c",SYGLE[x][y]);
             gotoxy(H-4,V);
             printf("%c",SYGLE[x-1][y]);
             gotoxy(H-4,V);
             H=H-4;
             x=x--;
             break;

           }
}

inner: ;
}


static void toright()
{

if(x==2)
goto inner;

for(x;x<3;x++)
{

            if(BOOLSWITCH[x][y] == false && BOOLSWITCH[x+1][y] == true )
             {

                SYGLE[x+1][y]= ;
                gotoxy(H+4,V);
                printf("%c",SYGLE[x+1][y]);
                gotoxy(H+4,V);
                x=x++;
                H=H+4;

                break;

             }

             if(BOOLSWITCH[x][y] == true && BOOLSWITCH[x+1][y] == false )
             {

                SYGLE[x][y]=-;
                printf("%c",SYGLE[x][y]);
                gotoxy(H+4,V);
                x=x++;
                H=H+4;

                break;

             }

             if(BOOLSWITCH[x][y]==false && BOOLSWITCH[x+1][y]== false)
             {

                gotoxy(H+4,V);
                x=x++;
                H=H+4;

                break;

             }


             if(SYGLE[2][y]!= )                                         /// CURSOR FIRST.
             {
                swap(&SYGLE[x][y],&SYGLE[x+1][y]);
                gotoxy(H,V);
                 printf("%c",SYGLE[x][y]);
                gotoxy(H+4,V);
                printf("%c",SYGLE[x+1][y]);
                gotoxy(H+4,V);
                H=H+4;
                x=x++;
                break;
             }
}

inner: ;
}



main()
{
    draw();

    gotoxy(0,23);
    printf("USE: NUMPAD 4,6,8,2 ASE ARROWS.
USE: CAPITAL x AS SYMBOL.");
    gotoxy(31,0);
    printf("WELCOME!.");
    gotoxy(60,23);
    printf("PLAYER SCORE : %d",score_P);
    gotoxy(60,24);
    printf("IA SCORE : %d",score_IA);

startgame: ;

    if(tempo==r)
    {
    compteur++;
    reboot();
    gotoxied();
    }

    if(compteur %2!= 0)
    goto IA_TURN;

    gotoxied();


while(1)
{

        tempo=0;
        tempo = _getch();

        if(tempo == 54)                               // Cases.
        toright();

        if(tempo == 52)
        toleft();

        if(tempo == 50)
        todown();

        if(tempo == 56)
        toup();


          if(tempo==X)                                  // IA Case.
          {

             if(BOOLSWITCH[x][y]==false)
             goto ClearZone;
             PlayerT();

             if(SYGLE[0][0]==X && SYGLE[1][0]==X && SYGLE[2][0]==X || SYGLE[0][1]==X && SYGLE[1][1]==X && SYGLE[2][1]==X || SYGLE[0][2]==X && SYGLE[1][2]==X && SYGLE[2][2]==X || SYGLE[0][0]==X && SYGLE[0][1]==X && SYGLE[0][2]==X || SYGLE[1][0]==X && SYGLE[1][1]==X && SYGLE[1][2]==X || SYGLE[2][0]==X && SYGLE[2][1]==X && SYGLE[2][2]==X || SYGLE[2][0]==X && SYGLE[1][1]==X && SYGLE[0][2]==X || SYGLE[0][0]==X && SYGLE[1][1]==X && SYGLE[2][2]==X )
             {
               setcolor(4);
               gotoxy(50,9);
               printf("Player WIN.");
               score_P++;
               Sleep(2000);
               gotoxy(50,9);
               printf("                  ");
               goto endgame;
             }

              if( BOOLSWITCH[0][0]==false && BOOLSWITCH[1][0]==false && BOOLSWITCH[2][0]==false && BOOLSWITCH[0][1]==false && BOOLSWITCH[1][1]==false && BOOLSWITCH[2][1]==false && BOOLSWITCH[0][2]==false && BOOLSWITCH[1][2]==false && BOOLSWITCH[2][2]==false )
             {
                setcolor(7);
                gotoxy(50,9);
                printf("DRAW GAME!");
                Sleep(1000);
                gotoxy(50,9);
                printf("             ");
                goto endgame;
             }
             Sleep(1000);
             IA_TURN: ;
             IA();

              if(SYGLE[0][0]==O && SYGLE[1][0]==O && SYGLE[2][0]==O || SYGLE[0][1]==O && SYGLE[1][1]==O && SYGLE[2][1]==O || SYGLE[0][2]==O && SYGLE[1][2]==O && SYGLE[2][2]==O || SYGLE[0][0]==O && SYGLE[0][1]==O && SYGLE[0][2]==O || SYGLE[1][0]==O && SYGLE[1][1]==O && SYGLE[1][2]==O || SYGLE[2][0]==O && SYGLE[2][1]==O && SYGLE[2][2]==O || SYGLE[2][0]==O && SYGLE[1][1]==O && SYGLE[0][2]==O || SYGLE[0][0]==O && SYGLE[1][1]==O && SYGLE[2][2]==O )
             {
               setcolor(1);
               gotoxy(50,9);
               printf("IA SCORES !");
               score_IA++;
               Sleep(2000);
               gotoxy(50,9);
               printf("             ");
               goto endgame;
             }

              if( BOOLSWITCH[0][0]==false && BOOLSWITCH[1][0]==false && BOOLSWITCH[2][0]==false && BOOLSWITCH[0][1]==false && BOOLSWITCH[1][1]==false && BOOLSWITCH[2][1]==false && BOOLSWITCH[0][2]==false && BOOLSWITCH[1][2]==false && BOOLSWITCH[2][2]==false )
             {
                setcolor(7);
                gotoxy(50,9);
                printf("DRAW GAME!");
                Sleep(1000);
                gotoxy(50,9);
                printf("             ");
                goto endgame;
             }

             ClearZone:;
          }

}

endgame:;

gotoxy(60,23);
printf("PLAYER SCORE : %d",score_P);
gotoxy(60,24);
printf("IA SCORE : %d",score_IA);

setcolor(7);
gotoxy(0,0);
printf("PRESS: r TO REMATCH.
PRESS: q TO QUIT.");

while(1)
{
    tempo=_getch();
    if(tempo ==r)
    goto startgame;

    if(tempo == q)
    goto exit;
}
exit:;
}
Read more »

C program that reads a file containing integers and appends at its end the sum of all the integers

C program that reads a file containing integers and appends at its end the sum of all the integers

#include<stdio.h>
#include<conio.h>
#include<process.h>

main()
{
int a,i,n,sum=0;
FILE *fp;
clrscr();

//Writing numbers to the file
fp=fopen("DATA","w");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

printf("How many numbers?");
scanf("%d",&n);
printf("Enter numbers in the file:
");


for(i=0;i<n;++i)
{
scanf("%d",&a);
putw(a,fp);
}
fclose(fp);

//Reading the file and doing sum
fp=fopen("DATA","r");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

while((a=getw(fp))!=EOF)
sum+=a;

fclose(fp);

//Appending sum to the file
fp=fopen("DATA","a");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

putw(sum,fp);
fclose(fp);

//Displaying file after append
fp=fopen("DATA","r");
if(fp==NULL)
{
printf("File could not open!!");
exit(0);
}

printf("
File after append:
");

while((a=getw(fp))!=EOF)
printf("%d ",a);

fclose(fp);
getch();
}
Read more »

C Program to print following triangle


 *
**
***
****
*****


code:
#include<iostream.h>
#include<conio.h>


void main()
{
clrscr();
int n,i,j;
cout<<"Ener size of Triangle(ex-4):";
cin>>n;
cout<<"
";



for(i=0;i<n;++i)
{
for(j=0;j<=i;++j)
cout<<"*";
cout<<"
";

}
getch();
}
Read more »

Friday, February 13, 2015

C Program to print a man using graphics

C++ Program to print a man using graphics


#include<graphics.h>
#include<iostream.h>
#include<stdlib.h>
#include<stdio.h>
#include<conio.h>
void main()
{
   int gdriver=DETECT,gmode;
   initgraph(&gdriver, &gmode, "c:\turboc3\bgi");

   //for head
   ellipse(320,95,360,0,25,20);
   line(298,85,341,85);
   circle(310,90,2);
   circle(330,90,2);
   arc(320,100,200,-20,10);

   //for neck
   line(313,115,313,125);
   line(328,115,328,125);

   //For centre part
   arc(320,225,72,107,100);
   line(290,129,290,200);
   line(350,129,350,200);
   line(290,193,350,193);
   line(290,200,350,200);


   //for legs
   line(290,200,285,280);
   line(320,225,305,280);
   line(322,225,335,280);
   line(350,200,355,280);

   //for right hand
   line(290,129,255,165);
   line(255,165,290,200);
   line(290,149,275,165);
   line(275,165,290,182);

   //for left hand
   line(350,129,385,165);
   line(385,165,350,200);
   line(350,149,365,165);
   line(365,165,350,182);

   //for shoes
   line(285,280,275,287);
   line(275,287,305,287);
   line(305,280,305,287);

   line(335,280,335,287);
   line(335,287,365,287);
   line(355,280,365,287);


   //for name
   settextstyle(2,HORIZ_DIR,4);
   outtextxy(293,150,"The Crazy");
   outtextxy(292,160,"Programmer");

   getch();
   closegraph();
}
Read more »

Thursday, February 12, 2015

String in C Part 1

Till now I told you about the arrays in C language. I have also explained the close relation between arrays and pointers. Now lets move this journey to one step forward. Today I will tell you about the strings in C language. In this tutorial I will cover all the basics related to strings.


String in C

Earlier we used integer arrays to store group of integer elements. Similarly in C language we use strings to store group of characters. They are used to manipulate text such as words and sentences. Remember strings are similar to arrays but they are not same. Sometimes strings are also called character arrays.

A string always ends with a null character i.e.
Read more »

Tuesday, February 10, 2015

C program to print truth table of XY Z

C++ program to print truth table of XY+Z

#include<iostream.h>
#include<conio.h>

void main()
{
int x,y,z;
clrscr();
cout<<"X Y Z XY+Z";

for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
cout<<"

"<<x<<" "<<y<<" "<<z<<" 1";
else
cout<<"

"<<x<<" "<<y<<" "<<z<<" "<<x*y+z;
}
getch();
}
Read more »