Code Scrappers


Tower Of Hanoi C Program


                  #include<stdio.h>
                  #include<stdlib.h>

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

                  void main()
                  {
                      int n;

                      printf("enter the no. of disks : ");

                      scanf("%d", &n);

                      printf("\n moves required are :\n");

                      TOH('A', 'C', 'B', n);

                  }
                  void TOH(char frompole, char topole, char auxpole, int n)
                  {

                      if (n == 1)
                      {
                          printf("\n Move disk 1 from pole %c to pole %c \n", frompole, topole);

                          return;
                      }
                      TOH(frompole, auxpole, topole, n-1);

                      printf(" Move disk %d from pole %c to pole %c \n", n, frompole, topole);

                      TOH(auxpole, topole, frompole, n-1);
                  }

              



Output

          Enter the number of disks : 3

          moves required are :

           Move disk 1 from pole A to pole C
           Move disk 2 from pole A to pole B
           Move disk 1 from pole C to pole B
           Move disk 3 from pole A to pole C
           Move disk 1 from pole B to pole A
           Move disk 2 from pole B to pole C
           Move disk 1 from pole A to pole C