Monday, 25 January 2016

C Program For Tower Of Hanoi Problem Solution Using Recursive Function.

Solution:

#include<stdio.h>
/*
    *
    * Prosen Ghosh
    * American International University - Bangladesh (AIUB)
    *
*/
void tower(int n,char firstPillar[],char midPillar[],char lastPillar[]){

    if(n == 1){
        printf("\nMove Disk %d To Pillar %s To Pillar %s",n,firstPillar,midPillar);
    }
    else{
        tower(n-1,firstPillar,lastPillar,midPillar);
        printf("\nMove Disk %d To Pillar %s To Pillar %s",n,firstPillar,midPillar);
        tower(n-1,lastPillar,midPillar,firstPillar);
    }
}
int main(){

    int n ;
    char name[100];
    printf("Enter Disk Number: ");
    scanf("%d",&n);
    tower(n,"First","Mid","Last");
}

No comments:

Post a Comment