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) করে। Interviewer-রা এই তুলনা খুব পছন্দ করেন।

Challenge

+30 XP

Loop ছাড়া 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.