×

Golang Tutorial

Golang Reference

Golang Programs

Golang Practice

Golang Miscellaneous

Golang Recursion | Find Output Programs | Set 1

This section contains the Golang recursion find output programs (set 1) with their output and explanations.
Submitted by Nidhi, on August 27, 2021

Program 1:

package main  
import "fmt"  

func calculateFactorial(num int)int{  
   if (num == 0)
      return 1;  
   return num*calculateFactorial(num-1);  
}

func main() {  
    var num = 5;
    var fact= 0;
    
    fact = calculateFactorial(num);
    fmt.Println("Factorial is: ",fact);  
}  

Output:

./prog.go:6:7: syntax error: unexpected return, expecting expression
./prog.go:10:6: syntax error: unexpected main, expecting (
./prog.go:11:5: syntax error: unexpected var at end of statement
./prog.go:11:9: num redeclared in this block
	prog.go:4:33: previous declaration

Explanation:

The above program will generate syntax errors because we did not use curly braces with the if statement.


Program 2:

package main

import "fmt"

func calculateFactorial(num int) int {
	if num == 0 {
		return 1
	}
	return num * calculateFactorial(num-1)
}

func main() {
	var num = 5
	var fact = 0

	fact = calculateFactorial(num)
	fmt.Println("Factorial is: ", fact)
}

Output:

Factorial is:  120

Explanation:

Here, we created a recursive function calculate factorial() to calculate the factorial of a given number. Then we called recursive function from main() function and printed the result on the screen.


Program 3:

package main

import "fmt"

func calculateFactorial(num int) int {
	if num == 0 {
		return 1
	}
	return num * calculateFactorial(num-1)
}

func main() {
	var num = 5
	var ptr *int = &num
	var fact = 0

	fact = calculateFactorial(*ptr)
	fmt.Println("Factorial is: ", fact)
}

Output:

Factorial is:  120

Explanation:

Here, we created two functions main() and calculateFactorial(). The calculateFactorial() is used to calculate the factorial of the specified number.

In the main() function, we created variable num and an integer pointer ptr. Then we initialized the pointer ptr with the address of variable num. After that, we called the calculateFactorial() function by passing the pointer and get the result.


Program 4:

package main

import "fmt"

func main() {

	var num = 0
	fmt.Println("Num: ", num)
	num++
	if num == 5 {
		return
	}
	main()
}

Output:

Num: 0 
Num: 0 
Num: 0 
Num: 0 
Num: 0 
.
.
.
Infinite time

Explanation:

Here, we called the main() function recursively. Every time a new num variable is created and initialized with 0. That's why the if condition will never be true. Then it will print "Num 0" infinite times.


Program 5:

package main

import "fmt"

var num = 0

func main() {
	fmt.Println("Num: ", num)
	num++
	if num == 5 {
		return
	}
	main()
}

Output:

Num:  0
Num:  1
Num:  2
Num:  3
Num:  4

Explanation:

Here, we created a global variable num initialized with 0, and called the main() function recursively. Every time value of num increased by 1 and print value of the num variable. When the value num is equal to 5 then the program gets terminated.

Golang Find Output Programs »





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.