Last active
March 8, 2025 09:07
-
-
Save ChanchalS7/74d78eb3b1083e536e5bbecceef1cf56 to your computer and use it in GitHub Desktop.
Golang recursion question :
This file contains hidden or 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
Find the factorial of number using recusion in Golang : | |
Find the sum of natural number using recursion in Golang : | |
Find the reverse of string using recursion in Golang : | |
Program to check prime number using recursion in Golang : | |
Program to print fibonacci series nth term : | |
Program to generate permuation of string : | |
Program to find GCD of a numbers : | |
Program to find the N th term of fibonacci number using memoization : | |
Program to find the subset of a set : | |
Program to find the Tower of Hanoi problem : |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
// Online Go compiler to run Golang program online
// Print "Try programiz.pro" message
package main
import "fmt"
//factorial of a number
func fact(n int) int{
if n==0 {
return 1
}
return n*fact(n-1)
}
//natural number sum
func sum(n int) int {
if n==0 {
return 0
}
return n+sum(n-1)
}
//reverse of a string
func reverse(s string)string {
if len(s) == 0 {
return s
}
return string(s[len(s)-1])+reverse(s[:len(s)-1])
}
//prime number problem
func isPrime(n , i int) bool {
if i*i > n {
return true
}
if n%i == 0 {
return false
}
return isPrime(n, i+1)
}
func checkPrime(n int) bool {
if n<=1 {
return false
}
return isPrime(n,2)
}
// fibonacci series
func fib(n int )int {
if n<=1 {
return n
}
return fib(n-1)+fib(n-2)
}
//generate permutation of a string
func permute(s string)[]string{
var result []string
permuteHelper(s,"",&result)
return result
}
func permuteHelper(s,current string, result *[]string){
if len(s)== 0 {
*result = append(*result, current)
return
}
for i:=0; i<len(s); i++{
newStr := s[:i] +s[i+1:]
permuteHelper(newStr, current+string(s[i]),result)
}
// gcd of numbers
func gcd(a,b int) int{
if b==0 {
return a
}
return gcd(b,a%b)
}
//solve Nth fibonacci number using memoization
func fibonacci(n int , memo map[int]int) int {
if n<=1 {
return n
}
if _, found := memo[n]; found {
return memo[n]
}
memo[n]=fibonacci(n-1,memo)+fibonacci(n-2,memo)
return memo[n]
}
func main() {
fmt.Println("factorial of number =",fact(5))
fmt.Println("Sum of number=",sum(5))
fmt.Println("Reverse of string=",reverse("Hello"))
fmt.Println("Check number 11 is prime =",checkPrime(11))
fmt.Println("Fibonacci series is :",fib(5))
fmt.Println("GCD of number 28 and 35 is =",gcd(28,35))
fmt.Println("Permutation of string abc=",permute("abc"))
memo:=make(map[int]int)
fmt.Println("Nth term of fibnaocci:",fibonacci(10,memo))
}