Golang Factorial of a Number using the iterative and recursive function
- Admin
- Dec 31, 2023
- Golang-examples
This blog post covers two programs to calculate the factorial of a number.
- Using the Recursion function
- Using Iterative Function
Factorial
of a number n is called n! which is equal to the product of all integers less than or equal to the number n.
It calculates for positive numbers and factorial for negative numbers does not exist.
The factorial of a number=0 is 1.
For example, a Factorial of 5 is equal to 5!=5*4*3*2*1.
Factorial of n=n!=1*2*3*4*..n
To understand this example, You should have the following features in the Go language.
- Golang For Loop control structure
- Golang For Loop Range form
- Golang Operator Guide
- Golang Recursion Examples
- Golang Datatype guide
How to find the factorial of a number using the golang Iteration Function
In the below program, take the input from the user keyboard and store it in a number of variables using the Scanln
function.
Declared function - IterativeFactorial with an input parameter of type and return type is uint64
which calculates the factorial of a given number.
Inside the IterativeFactorial function, We have used for loop to iterate all the numbers from 1 to number, and the product of each iterated number is stored in a variable result.
Here to store the result, used the uint64
data type which is unsigned 64-bit integers, values ranging from 0 to 2 power 64-1
It uses to store the bigger numbers of a factorial calculation result.
The result returns to the main function.
Finally, the result prints to the console using the Println
function.
package main
import (
"fmt"
)
func IterativeFactorial(number int) uint64 {
var result uint64 = 1
if number < 0 {
} else {
for i: = 1;
i <= number;
i++{
result *= uint64(i)
}
}
return result
}
func main() {
var number int
fmt.Print("Enter Number:")
fmt.Scanln( & number)
fmt.Printf("Factorial of %d = %d", number, IterativeFactorial(number))
}
When the above program executes and the output is
Enter Number:5
Factorial of 5 = 120
Enter Number:6
Factorial of 6 = 720
How to Calculate the Factorial of a number using the golang Recursive function
First, read the number from the user console and store it in the variable number using the Scanln
function.
Declared Recursive function RecursiveFactorial
, Recursive functions are functions that are called inside the same function.
For Example, if the input number supplied is 5, The recursive function works as below
Initially, RecursiveFactorial(5)
is called from the main function.
Inside a function, 5 is greater than equal to 1, and 5 is a multiple of RecursiveFactorial(4)
. It is called the same function. hence, It is called a recursive function
During each recursive call, the number value is decremented by 1 and executes until it reaches 1
When a number is 1, It returns 1 without a recursive call
In this line of code return uint64(number) * RecursiveFactorial(number-1)
, if int is not converted to uint64(uint64(int))
, It throws invalid operation: number * RecursiveFactorial(number - 1) (mismatched types int and uint64)
Here is a sequence of steps calculation in a recursive function
RecursiveFactorial(5)
\--5\*RecursiveFactorial(4)
--5\*4\*RecursiveFactorial(3)
--5\*4\*3\*RecursiveFactorial(2)
--5\*4\*3\*2\*RecursiveFactorial(1)
\--5\*4\*3\*2\*1 = 120
Here is a program to calculate Factorial using a recursive function
package main
import (
"fmt"
)
func main() {
var number int
fmt.Print("Enter Number:")
fmt.Scanln( & number)
fmt.Printf("Factorial of %d = %d\n", number, RecursiveFactorial(number))
}
func RecursiveFactorial(number int) uint64 {
if number >= 1 {
return uint64(number) * RecursiveFactorial(number - 1)
} else {
return 1
}
}
Output:
Enter Number:5
Factorial of 5 = 120