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
- Import the fmt Package: Use import "fmt"to include the fmt package for formatted I/O operations.
- Write the Main Function: Define the mainfunction, which is the entry point of every Go program.
- Declare Variables: Declare variables to store the number and the result of the factorial calculation.
- Input the Number: Use fmt.Scanlnto take input from the user for the number.
- Calculate the Factorial: Use a loop to calculate the factorial by multiplying the numbers from 1 to the input number.
- Display the Factorial: Use fmt.Printlnto 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 numberis declared to store the input number. Thefactorialvariable is initialized to1to 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. Thefmt.Scanlnfunction reads the input and stores it in thenumbervariable.
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 forloop to multiply the numbers from1tonumber, storing the result in thefactorialvariable.
Step 4: Display the Factorial
- The calculated factorial is displayed using the fmt.Printffunction, 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.