Code: Towers of hanoi - write a program to solve Towers of Hanoi -

#include<stdio.h>
int TOWER(int n, char beg, char aux, char end);

int TOWER(int n, char beg, char aux, char end)
         {
           if (n==1)
               printf("\nmove disk %d from %c to %c", n, beg, end);
           else
                {
                    TOWER(n-1,beg,end,aux);
                    printf("\nmove disk %d from %c to %c", n, beg, end);
                    TOWER(n-1,aux, beg, end);
                    return 0;
                }
         }
int main()
        {
            int n;
            printf("TOWER OF HANOI:\n");
            printf("\nHow many disk: ");
            scanf("%d", &n);
            if(n<1)
                {
                    printf("INVALID DISK MOVEMENT");
                    return 0;
                }
             TOWER(n,'A', 'B', 'C');
             return 0;
        }

3 comments: