Tuesday, December 18, 2018

Golang Example - Find Factorial of a Number

This blog post covers two programs to calculate factorial of a number
  • Using Recursion function
  • Using Iterative Function
Factorial of a number n is called n! which is equal to product of all integers less than or equal to number n. This will be always calculated for positive numbers and factorial of negative numbers not exists. Factorial of a number=0 is 1.
For example Factorial of 5 is equal to 5!=5*4*3*2*1. 
Facttorial of n=n!=1*2*3*4*..n
To understand this example, You should have following features in Go language

Example program to find factorial of a number using Iteration Function

In the below program, take the input from user keyboard and store it in number variable using Scanln function
Declared function - IterativeFactorial with input parameter of type and return type is uint64 which calculates factorial of a given number
Inside  IterativeFactorial function, We have used for loop to iterate all the numbers from 1 to number, and product of each iterated numbers 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
This will be used to store the bigger numbers of a factorial calculation result.
The result is returned to main function
Finally result is printed to console using 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 above program is executed, 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 user console and stored in variable number using Scanln function.
Declared Recurisive function RecursiveFactorial, Recusrive functions are functions which will be called inside a same function.

For Example if input number is supplied is 5, The recursive function works as below

Intially, RecursiveFactorial(5) is called from main function.
Inside a function, 5 is greater than equal to 1, 5 is multiple of RecursiveFactorial(4). This is called in same function called recusrive function
During each recurisive call, number value is decremented by 1  and executes until it reaches 1

When number is 1, It returns 1 without recurisve 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 recusive 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

Related article


EmoticonEmoticon