📖 Scenario: Imagine you want to find the factorial of a number. Factorial means multiplying the number by all smaller numbers down to 1. For example, factorial of 4 is 4 x 3 x 2 x 1 = 24.
🎯 Goal: You will write a Rust program that calculates the factorial of a number using a function that calls itself (recursion). This helps understand how problems can be solved by breaking them into smaller parts.
📋 What You'll Learn
Create a variable called number with the value 5
Create a recursive function called factorial that takes an u32 and returns an u32
Use a base case in factorial to return 1 when the input is 0
Use recursion in factorial to multiply the number by factorial of number minus one
Print the result of factorial(number) with a message
💡 Why This Matters
🌍 Real World
Calculating factorials is useful in math, statistics, and computer science for permutations and combinations.
💼 Career
Understanding recursion is important for solving problems that can be broken down into smaller similar problems, common in software development and algorithm design.
Progress0 / 4 steps
1
Create the number variable
Create a variable called number and set it to 5.
DSA Rust
Hint
Use let number: u32 = 5; inside the main function.
2
Write the factorial function
Write a recursive function called factorial that takes a parameter n of type u32 and returns u32. Use a base case to return 1 if n is 0.
DSA Rust
Hint
Use if n == 0 { 1 } else { n * factorial(n - 1) } inside the function.
3
Call factorial function with number
Inside the main function, create a variable called result and set it to the value returned by calling factorial(number).
DSA Rust
Hint
Use let result = factorial(number); inside main.
4
Print the factorial result
Add a println! statement inside the main function to print: "Factorial of 5 is: {}" using the variable result.
DSA Rust
Hint
Use println!("Factorial of 5 is: {}", result); to show the output.
Practice
(1/5)
1. What does the factorial of a number represent in mathematics?
easy
A. The number multiplied by 2
B. The sum of all positive integers up to that number
C. The number divided by 2
D. The product of all positive integers up to that number
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
Only The product of all positive integers up to that number correctly describes factorial as a product, not sum or division.
Final Answer:
The product of all positive integers up to that number -> Option D
Quick Check:
Factorial = product of integers [OK]
Hint: Factorial multiplies numbers down to 1 [OK]
Common Mistakes:
Confusing factorial with sum
Thinking factorial divides the number
Assuming factorial doubles the number
2. Which of the following is the correct Rust function signature for a recursive factorial function?
easy
A. fn factorial(n: String) -> u32
B. fn factorial(n: u32) -> String
C. fn factorial(n: u32) -> u32
D. fn factorial(n: i32) -> bool
Solution
Step 1: Identify input and output types
Factorial takes a positive integer (u32) and returns a positive integer (u32).
Step 2: Match function signature
fn factorial(n: u32) -> u32 matches input and output types correctly for factorial.
Final Answer:
fn factorial(n: u32) -> u32 -> Option C
Quick Check:
Input and output types match factorial [OK]
Hint: Factorial input/output are unsigned integers [OK]
Common Mistakes:
Using String type for input or output
Using signed integer when unsigned is better
Returning bool instead of number
3. What is the output of this Rust code?
fn factorial(n: u32) -> u32 {
if n == 0 { 1 } else { n * factorial(n - 1) }
}
fn main() {
println!("{}", factorial(4));
}