{

Golang Example - Find Factorial of a Number


Golang example calculate Factorial of a Number

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 number n. It calculates for positive numbers and factorial for negative numbers does not exist.

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.

Example program to find factorial of a number using 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 variable result.

Here to store the result, used 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 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  

Example Program 2 - Recursive function Calculate Factorial of a number

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 is 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, 5 is 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 number is 1, It returns 1 without 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 sequence of steps calculation in 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 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 is

Enter Number:5  
Factorial of 5 = 120  

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.