Go language program to print factorial of given number

Views: 143   

Following program shows you how to print factorial of given number.
In this program we get input from user and prints factorial of that number using for loop

package main

import "fmt"

func main() {

  var input int
  var result int
  fmt.Println("Enter a number:")
  fmt.Scanf("%d", &input)
  result = 1
  for i:= 1; i <= input; i++ {
    result = result * i
  }
  fmt.Println(input , " factorial is: " , result)
  }

Output:

Enter a number:
 3
3  factorial is:  6
On By