📖 Scenario: You are building a simple calculator that can find the factorial of a number. Factorial means multiplying a number by all the numbers below it down to 1. For example, 4 factorial (written as 4!) is 4 x 3 x 2 x 1 = 24.
🎯 Goal: Learn how to write a recursive function in C# to calculate the factorial of a given number.
📋 What You'll Learn
Create an integer variable called number with the value 5
Create a recursive function called Factorial that takes an integer parameter n
The function should return 1 if n is 0 or 1
Otherwise, the function should return n multiplied by Factorial(n - 1)
Call the Factorial function with number and store the result in result
Print the value of result
💡 Why This Matters
🌍 Real World
Calculating factorials is useful in math problems, probability, and computer science algorithms like permutations and combinations.
💼 Career
Understanding recursion is important for many programming jobs, especially those involving algorithms, data structures, and problem solving.
Progress0 / 4 steps
1
Create the number variable
Create an integer variable called number and set it to 5.
DSA C#
Hint
Use int number = 5; to create the variable.
2
Write the recursive Factorial function
Write a recursive function called Factorial that takes an integer parameter n. It should return 1 if n is 0 or 1. Otherwise, it should return n * Factorial(n - 1).
DSA C#
Hint
Use an if statement to check if n is 0 or 1, then return 1. Otherwise, return n * Factorial(n - 1).
3
Call the Factorial function and store the result
Call the Factorial function with the variable number and store the result in an integer variable called result.
DSA C#
Hint
Use int result = Factorial(number); to call the function and save the answer.
4
Print the factorial result
Print the value of the variable result using Console.WriteLine.
DSA C#
Hint
Use Console.WriteLine(result); to show the factorial on the screen.
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 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 base case for a recursive factorial function in C#?
easy
A. if (n == 0) return 1;
B. if (n == 1) return 1;
C. if (n == 2) return 2;
D. if (n == -1) return 1;
Solution
Step 1: Identify factorial base case
By definition, 0! = 1, so the base case should return 1 when n is 0.
Step 2: Check options
Only if (n == 0) return 1; returns 1 when n == 0, which is correct base case for recursion.
Final Answer:
if (n == 0) return 1; -> Option A
Quick Check:
Base case: 0! = 1 [OK]
Hint: Base case for factorial is n == 0 returning 1 [OK]
Common Mistakes:
Using n == 1 as base case
Returning wrong value in base case
Not handling negative inputs
3. What is the output of the following C# code?
static int Factorial(int n) {
if (n == 0) return 1;
return n * Factorial(n - 1);
}
Console.WriteLine(Factorial(4));