📖 Scenario: Imagine you are a chef who needs to calculate the number of ways to arrange a set of dishes on a menu. This is called a factorial calculation, which means multiplying a number by all the numbers below it down to 1.
🎯 Goal: You will write a Go program that calculates the factorial of a number using a recursive function. Recursion means the function calls itself to solve smaller parts of the problem until it reaches the simplest case.
📋 What You'll Learn
Create a variable to hold the number to calculate the factorial for
Create a recursive function called factorial that takes an integer and returns an integer
Use a base case in the factorial function to stop recursion when the input is 1
Use recursion to calculate the factorial for numbers greater than 1
Print the factorial result
💡 Why This Matters
🌍 Real World
Factorials are used in counting problems, like arranging items or calculating probabilities.
💼 Career
Understanding recursion and factorials helps in algorithm design and problem solving in software development.
Progress0 / 4 steps
1
Create the number variable
Create an integer variable called number and set it to 5.
DSA Go
Hint
Use number := 5 inside the main function.
2
Write the recursive factorial function
Write a recursive function called factorial that takes an integer n and returns an integer. Use a base case that returns 1 when n == 1.
DSA Go
Hint
Check if n == 1 and return 1. Otherwise, return n * factorial(n-1).
3
Call the factorial function
Inside the main function, create a variable called result and set it to the call factorial(number).
DSA Go
Hint
Use result := factorial(number) inside main.
4
Print the factorial result
Use fmt.Println to print the value of result inside the main function.
DSA Go
Hint
Use fmt.Println(result) to display the factorial.
Practice
(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The product of all positive integers up to that number
B. The sum of all positive integers up to that number
C. The number multiplied by 2
D. The number divided by 2
Solution
Step 1: Understand factorial definition
Factorial of a number n (written as n!) is the product of all positive integers from 1 to n.
Step 2: Compare options with definition
Only The product of all positive integers up to that number correctly describes factorial as a product, not sum or other operations.
Final Answer:
The product of all positive integers up to that number -> Option A
Quick Check:
Factorial = product of integers [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial doubles the number
Assuming factorial divides the number
2. Which of the following is the correct base case for a recursive factorial function in Go?
easy
A. if n == 0 { return 1 }
B. if n == 1 { return 0 }
C. if n == 0 { return 0 }
D. if n == 1 { return n + 1 }
Solution
Step 1: Identify base case for factorial
Factorial of 0 is defined as 1, so base case should return 1 when n is 0.
Step 2: Check options for correct base case
Only if n == 0 { return 1 } returns 1 when n == 0, which is correct base case.
Final Answer:
if n == 0 { return 1 } -> Option A
Quick Check:
Base case n=0 returns 1 [OK]
Hint: Factorial base case returns 1 at zero [OK]
Common Mistakes:
Returning 0 for base case
Returning wrong value for n=1
Missing base case causing infinite recursion
3. What is the output of this Go code?
func factorial(n int) int {
if n == 0 {
return 1
}
return n * factorial(n-1)
}
fmt.Println(factorial(3))