Lesson 15 of 23

Recursion

10 min 30 XP

Recursive function लहान input सह स्वतःलाच call करते. Base case नसेल तर stack overflow होतो आणि प्रोग्राम crash होतो.

Example: edit me and press Run

Output Loading C (first time takes a few seconds)
Press Run to see the result here.

Recursive fib(40) खूप धीमे आहे (O(2ⁿ)); loop किंवा memoization ते O(n) करते. Interviewers ना ही तुलना खूप आवडते.

Challenge

+30 XP

Loops शिवाय recursive function int sumDigits(int n) लिहा. main() मध्ये n वाचा आणि निकाल print करा. Input: 9875 → Output: 29

Output Loading C (first time takes a few seconds)
Press Run to see the result here.