Go Program to Find the Factorial of a Number

Introduction

The factorial of a number is the product of all positive integers less than or equal to that number. It is denoted by n!, where n is the number. For example, the factorial of 5 (denoted as 5!) is 5 × 4 × 3 × 2 × 1 = 120. This guide will show you how to write a Go program to calculate the factorial of a given number.

Problem Statement

Create a Go program that:

  • Prompts the user to enter a non-negative integer.
  • Calculates the factorial of the entered number.
  • Displays the factorial.

Example:

  • Input: 5
  • Output: Factorial of 5 is 120

Solution Steps

  1. Import the fmt Package: Use import "fmt" to include the fmt package for formatted I/O operations.
  2. Write the Main Function: Define the main function, which is the entry point of every Go program.
  3. Declare Variables: Declare variables to store the number and the result of the factorial calculation.
  4. Input the Number: Use fmt.Scanln to take input from the user for the number.
  5. Calculate the Factorial: Use a loop to calculate the factorial by multiplying the numbers from 1 to the input number.
  6. Display the Factorial: Use fmt.Println to display the calculated factorial.

Go Program

package main

import "fmt"

/**
 * Go Program to Find the Factorial of a Number
 * Author: https://www.javaguides.net/
 */
func main() {
    // Step 1: Declare variables to hold the number and the factorial result
    var number int
    var factorial int = 1

    // Step 2: Prompt the user to enter a non-negative integer
    fmt.Print("Enter a non-negative integer: ")
    fmt.Scanln(&number)

    // Step 3: Calculate the factorial using a loop
    if number < 0 {
        fmt.Println("Factorial is not defined for negative numbers.")
    } else {
        for i := 1; i <= number; i++ {
            factorial *= i
        }

        // Step 4: Display the factorial result
        fmt.Printf("Factorial of %d is %d\n", number, factorial)
    }
}

Explanation

Step 1: Declare Variables

  • The variable number is declared to store the input number. The factorial variable is initialized to 1 to store the result of the factorial calculation.

Step 2: Input the Number

  • The program prompts the user to enter a non-negative integer using fmt.Print. The fmt.Scanln function reads the input and stores it in the number variable.

Step 3: Calculate the Factorial

  • The program checks if the input number is negative. If it is, the program prints a message indicating that the factorial is not defined for negative numbers. Otherwise, the program uses a for loop to multiply the numbers from 1 to number, storing the result in the factorial variable.

Step 4: Display the Factorial

  • The calculated factorial is displayed using the fmt.Printf function, which formats the output string.

Output Example

Example 1:

Enter a non-negative integer: 5
Factorial of 5 is 120

Example 2:

Enter a non-negative integer: 0
Factorial of 0 is 1

Example 3:

Enter a non-negative integer: -3
Factorial is not defined for negative numbers.

Conclusion

This Go program demonstrates how to calculate the factorial of a non-negative integer. It covers basic programming concepts such as loops, conditionals, and arithmetic operations. This example is useful for beginners learning Go programming and understanding the concept of factorials.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top