📖 Scenario: Imagine you are helping a friend understand two ways to solve problems: recursion and iteration. You will create simple examples to see how each works and when one might be better than the other.
🎯 Goal: You will build two methods to calculate the factorial of a number: one using recursion and one using iteration. Then, you will compare their outputs to understand when each method is useful.
📋 What You'll Learn
Create a recursive method called FactorialRecursive that calculates factorial
Create an iterative method called FactorialIterative that calculates factorial
Use a variable called number set to 5
Print the results of both methods clearly
💡 Why This Matters
🌍 Real World
Understanding recursion and iteration helps solve many programming problems like searching, sorting, and navigating data structures.
💼 Career
Many software jobs require knowing when to use recursion or iteration for efficient and clear code.
Progress0 / 4 steps
1
Create the input number
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 method
Write a method called FactorialRecursive that takes an integer n and returns the factorial using recursion. Use the base case when n is 1, return 1; otherwise return n * FactorialRecursive(n - 1).
DSA C#
Hint
Use a method with a base case for n == 1 and recursive call for other cases.
3
Write the iterative factorial method
Write a method called FactorialIterative that takes an integer n and returns the factorial using a for loop. Start with a variable result set to 1, then multiply it by each number from 1 to n.
DSA C#
Hint
Use a for loop from 1 to n multiplying a result variable.
4
Print the results of both methods
Print the factorial of number using both FactorialRecursive(number) and FactorialIterative(number). Use Console.WriteLine to show the results with clear labels.
DSA C#
Hint
Use Console.WriteLine with string interpolation to print both results.
Practice
(1/5)
1. Which scenario is best suited for using recursion instead of iteration?
easy
A. When memory usage must be minimal
B. When you want to avoid function call overhead
C. When the problem can be divided into similar smaller problems
D. When the problem requires simple repeated loops
Solution
Step 1: Understand recursion use case
Recursion works well when a problem can be broken down into smaller similar problems, like factorial or tree traversal.
Step 2: Compare with iteration needs
Iteration is better for simple repeated loops or when avoiding function call overhead is important.
Final Answer:
When the problem can be divided into similar smaller problems -> Option C
Quick Check:
Recursion = divide and conquer [OK]
Hint: Recursion fits problems breaking into smaller similar parts [OK]
Common Mistakes:
Thinking iteration is always better for all problems
Ignoring function call overhead in recursion
Confusing recursion with simple loops
2. Which of the following is the correct syntax for a recursive method in C#?
easy
A. void RecursiveMethod() { while(true) { RecursiveMethod(); } }
B. void RecursiveMethod() { if(condition) RecursiveMethod(); else return; }
C. void RecursiveMethod() { for(int i=0; i<10; i++) RecursiveMethod(); }
D. void RecursiveMethod() { RecursiveMethod(); }
Solution
Step 1: Identify base case in recursion
Recursive methods must have a base case to stop calling themselves, shown by the else return in void RecursiveMethod() { if(condition) RecursiveMethod(); else return; }.
Step 2: Check other options for missing base case
Options B, C, and D call themselves without stopping, causing infinite recursion.