0% found this document useful (0 votes)
57 views2 pages

Prakhar Bhatnaga R 17csu139 Tower of Hanoi DATE:16/11/18 Code

The document contains C code that implements the Tower of Hanoi problem. It takes user input for the number of disks and uses a recursive towers function to print out the step-by-step sequence of moves to solve the problem, moving the disks from the initial peg to the final peg using the auxiliary peg. The output shows the moves for 3 disks as an example run of the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views2 pages

Prakhar Bhatnaga R 17csu139 Tower of Hanoi DATE:16/11/18 Code

The document contains C code that implements the Tower of Hanoi problem. It takes user input for the number of disks and uses a recursive towers function to print out the step-by-step sequence of moves to solve the problem, moving the disks from the initial peg to the final peg using the auxiliary peg. The output shows the moves for 3 disks as an example run of the program.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PRAKHAR BHATNAGA R 17CSU139

TOWER OF HANOI

DATE :16/11/18

CODE :
#include <stdio.h>

#include<stdlib.h>

void towers(int, char, char, char);

int main()

int num;

printf("Enter the number of disks : ");

scanf("%d", &num);

printf("The sequence of moves involved in the Tower of Hanoi are :\n");

towers(num, 'A', 'C', 'B');

return 0;

void towers(int num, char frompeg, char topeg, char auxpeg)

if (num == 1)

printf("\n Move disk 1 from peg %c to peg %c", frompeg, topeg);

return;

towers(num - 1, frompeg, auxpeg, topeg);

printf("\n Move disk %d from peg %c to peg %c", num, frompeg, topeg);

towers(num - 1, auxpeg, topeg, frompeg);

}
OUTPUT :
Enter the number of disks : 3

The sequence of moves involved in the Tower of Hanoi are :

Move disk 1 from peg A to peg C

Move disk 2 from peg A to peg B

Move disk 1 from peg C to peg B

Move disk 3 from peg A to peg C

Move disk 1 from peg B to peg A

Move disk 2 from peg B to peg C

Move disk 1 from peg A to peg C

Process returned 0 (0x0) execution time : 1.840 s

Press any key to continue.

You might also like