From cead139bca5cd4253fade86c5d763d64639d34a6 Mon Sep 17 00:00:00 2001 From: Prathamesh Yadav Date: Mon, 11 Oct 2021 12:25:11 +0530 Subject: [PATCH 1/2] Added factorial and tower of hanoi program in Go --- .../Recursive Algorithms/Go/factorial.go | 21 +++++++++++++++ .../Recursive Algorithms/Go/tower_of_hanoi.go | 26 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 Algorithms/Recursive Algorithms/Go/factorial.go create mode 100644 Algorithms/Recursive Algorithms/Go/tower_of_hanoi.go diff --git a/Algorithms/Recursive Algorithms/Go/factorial.go b/Algorithms/Recursive Algorithms/Go/factorial.go new file mode 100644 index 0000000..cab4d08 --- /dev/null +++ b/Algorithms/Recursive Algorithms/Go/factorial.go @@ -0,0 +1,21 @@ +package main + +import "fmt" + +func factorial(num int) int { + if num == 0 || num == 1 { + return 1 + } + return num * factorial(num-1) +} + +func main() { + + var num int + + fmt.Print("Enter the number : ") + fmt.Scanln(&num) + fmt.Print("Factorial of given number : ") + fmt.Print(factorial(num)) + +} diff --git a/Algorithms/Recursive Algorithms/Go/tower_of_hanoi.go b/Algorithms/Recursive Algorithms/Go/tower_of_hanoi.go new file mode 100644 index 0000000..b81b0eb --- /dev/null +++ b/Algorithms/Recursive Algorithms/Go/tower_of_hanoi.go @@ -0,0 +1,26 @@ +package main + +import ( + "fmt" + "strconv" +) + +func TOH_moves(num int, start, inter, end string) { + if num == 1 { + fmt.Println("Move disk 1 from " + start + " to " + end) + return + } + TOH_moves(num-1, start, end, inter) + fmt.Println("Move disk " + strconv.Itoa(num) + " from " + start + " to " + end) //strconv.Itoa() used for type casting int to string + TOH_moves(num-1, inter, end, start) + +} + +func main() { + + var num int + + fmt.Print("Enter the number of disk : ") + fmt.Scanln(&num) + TOH_moves(num, "A", "B", "C") +} From 79c35de5a9c0ab1a33232de0a644f3cd6e4433c2 Mon Sep 17 00:00:00 2001 From: Prathamesh Yadav Date: Mon, 11 Oct 2021 12:32:58 +0530 Subject: [PATCH 2/2] README.me updated --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 82fc52f..0879dfb 100644 --- a/README.md +++ b/README.md @@ -81,8 +81,8 @@ Binary Search | |✔️| ✔️| ✔️ | | |✔ Section/Language | C | C++ | Java | Python | Javascript | Scala | Go | -----------------|----|-----|------|--------|------------|-------| -- | Fibonacci |✔️ | | |✔️ | | | -Factorial |✔️ | | | ✔️ | | | -Tower of Hanoi |✔️ | | | | | | +Factorial |✔️ | | | ✔️ | | | | ✔️ | +Tower of Hanoi |✔️ | | | | | | | ✔️ | GCD |✔️ | | | | | | | ✔️ | LCM | | | | | | Pacal Traingle |✔️ | | | | |