Created
March 28, 2023 17:50
-
-
Save R3DHULK/784552ef3c56f7787f3827a0a8a87cb2 to your computer and use it in GitHub Desktop.
Tower Of Hanoi Written In Go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
) | |
func main() { | |
var numDisks int | |
fmt.Print("Enter the number of disks: ") | |
fmt.Scanln(&numDisks) | |
moves := towerOfHanoi(numDisks, "A", "B", "C") | |
fmt.Println("Moves:", moves) | |
} | |
func towerOfHanoi(numDisks int, from string, to string, aux string) int { | |
if numDisks == 1 { | |
fmt.Println("Move disk 1 from", from, "to", to) | |
return 1 | |
} | |
moves := towerOfHanoi(numDisks-1, from, aux, to) | |
fmt.Println("Move disk", numDisks, "from", from, "to", to) | |
moves++ | |
moves += towerOfHanoi(numDisks-1, aux, to, from) | |
return moves | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment