answersLogoWhite

0


Best Answer

/* hanoi.c */

#include <stdio.h>

#include <stdlib.h>

static long step;

static void Hanoi (int n, int from, int to,int spare)

{

if (n>1) Hanoi (n-1,from,spare,to);

printf ("Step %ld: move #%d %d-->%d\n", ++step, n, from, to);

if (n>1) Hanoi (n-1,spare,to,from);

}

int main (int argc, char **argv)

{

int n;

if (argc==1 (n= atoi(argv[1]))<=0) n= 5;

step= 0;

Hanoi (n, 1, 2, 3);

return 0;

}

User Avatar

Wiki User

13y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

#include<stdio.h>

#include<conio.h>

#include<math.h>

void hanoi(int x, char from,char to,char aux)

{

if(x==1)

{

printf("Move Disk From %c to %c\n",from,to);

}

else

{

hanoi(x-1,from,aux,to);

printf("Move Disk From %c to %c\n",from,to);

hanoi(x-1,aux,to,from);

}

}

int main(void)

{

int disk;

clrscr();

printf("Enter the number of disks you want to play with:");

scanf("%d",&disk);

double moves=pow(2,disk)-1;

printf("\nThe No of moves required is=%g \n",moves);

hanoi(disk,'A','C','B');

getch();

}

This answer is:
User Avatar

User Avatar

Wiki User

13y ago

#include <stdio.h>

void main()

{

void hanoi(char,char,char,int);

char t1='A',t2='B',t3='C';

int n;

printf("\n Enter the no. of disks on Tower A:");

scanf("%d",&n);

if(n<1)

{

printf("\n Nothing to move");

}

else

hanoi(t1,t2,t3,n);

}

void hanoi(char t1,char t2,char t3,int n)

{

static int step=0;

step++;

printf("\n %c %c %c %d",t1,t2,t3,n);

if(n==1)

{

printf("\n Move top disk from Tower %c ----> %c",t1,t2);

return;

}

hanoi(t1,t3,t2,n-1);

printf("\n %c %c %c %d",t1,t2,t3,n);

printf("\n Move top disk from Tower %c ----> %c",t1,t2);

printf("\n %c %c %c %d",t1,t2,t3,n);

hanoi(t3,t2,t1,n-1);

printf("\n %c %c %c %d steps=%d",t1,t2,t3,n,step);

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

by implementing ones of the algorithms that solve the tower of hanoi game, using the C language.

This answer is:
User Avatar

User Avatar

Wiki User

15y ago

You may get ideas from the attached link.

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Write a c program to implement tower of hanoi moves?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

The least number of moves in the tower of hanoi puzzle with five disks?

The number of moves required to solve the Hanoi tower is 2m + 1 . Therefore for a tower of five disks the minimum number of moves required is: 31.


Least number of moves in the tower of hanoi puzzle with five disks?

The number of moves required to solve the Hanoi tower is 2m + 1 . Therefore for a tower of five disks the minimum number of moves required is: 31.


Write an Algorithm for towers of hanoi?

#include#includevoid hanoi(int x, char from,char to,char aux){if(x==1){printf("Move Disk From %c to %c\n",from,to);}else{hanoi(x-1,from,aux,to);printf("Move Disk From %c to %c\n",from,to);hanoi(x-1,aux,to,from);}}int main(void){int disk;clrscr();printf("Enter the number of disks you want to play with:");scanf("%d",&disk);double moves=pow(2,disk)-1;printf("\nThe No of moves required is=%g \n",moves);hanoi(disk,'A','C','B');getch();}


What is the number of moves for 20 disks on the tower of hanoi?

1,048,575 moves and I know because I did the math.


How many moves does it take for the tower of hanoi if it has 7 disks?

127


How many moves does it take for the tower of hanoi if it has 52 disks?

100000000


What is the minimum amount of moves for 64 disks on tower of hanoi?

2^64-1 = 18446744073709551615


How do I write a Java program that moves the mouse only when CTRL held and mouse clicked that can run in the background?

You Don't.


How long would it take to the hanoi puzzle with 6 7 8 discs?

For any n-disc version of the Tower of Hanoi, the optimum solution for the puzzle takes a minimum of 2n-1 moves. In the case of 6, 7, 8-sized Towers of Hanoi, the puzzle would take: 26-1 = 63, 27-1 = 127, 28-1 = 255 moves.


What is the link between the number of discs and the minimum moves in the towers of hanoi puzzle and if i was given the number of discs could I work out the minimum moves without doing the puzzle?

If there are N discs, the minimum number of moves required is 2N - 1.


What is the least number of moves in the Tower of Hanoi puzzle with only 5 disks?

To move n disks, you need 2n-1moves. In this case, 31.


What is the solution of towers of hanoi problem?

This algorithm reads the value of number of discs and prints the move that are to be done for playing towers of hanoi.Pre: n is a positive integer which carries the value of number of discs.Post: prints the moves that are to be done for transferring discs in ascending order in another peg1.initialize value of n(positive integer greater than 1)2.moves = pow(2,disk)-13.if(n=1)i) moves disk from "from" to "to"4. end if5. elsei)hanoi(n-1,from,aux,to)ii) moves disc from "from" to "to''iii) hanoi(n-i,from,aux,to)6.end else7. prints the movesend towers_of_hanoi