-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #83 from pratham0402/pratham
Factorial and Tower of hanoi
- Loading branch information
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
|
||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} |
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