Lesson 9 of 24

Recursion

10 min 30 XP

Recursion में function खुद को छोटे input पर तब तक call करता है जब तक base case उसे रोक न दे। Tower of Hanoi क्लासिक उदाहरण है: n disks के लिए 2ⁿ - 1 moves चाहिए।

Example: edit me and press Run

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

Challenge

+30 XP

Recursive long long fastPower(long long base, int exp) लिखें: exp सम हो तो half = fastPower(base, exp / 2) निकालें और half * half लौटाएँ। main() में base और exp पढ़ें और परिणाम print करें। Input: 2 40 → Output: 1099511627776

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