Skip to content
Question 88 of 97

Q.Write a Recursive function in Python RecsumNat(N), to return the sum of the first N natural numbers. For example, if N is 10 then the function should return (1 + 2 + 3 + ... + 10 = 55).

(OR)
Write a Recursive function in Python Power(X,N), to return the result of X raised to the power N where X and N are non-negative integers. For example, if X is 5 and N is 3 then the function should return the result of (5)3 i.e. 125.
Uttar Pradesh UpmspCBSE Class XII Board 2020Subjective· 3mImportance★★★★★est
91% · 88/97 Questions
🔒 Locked · start free trial →

You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.

Start your 14-day free trial to unlock the full solution →

Both parts use recursion with a base case: Part (a) sums the first N natural numbers (RecsumNat(10)=55) and Part (b) computes X raised to N (Power(5,3)=125).

Part (a)

The sum of the first N natural numbers can be defined recursively as sum(N) = N + sum(N-1), with base case sum(0) = 0.

def RecsumNat(N):
    if N == 0:            # base case
        return 0
    return N + RecsumNat(N - 1)

print(RecsumNat(10))
``` …

Unlock everything free for 14 days

  • Full step-by-step solutions
  • Concept-first explanations
  • Methods, shortcuts & mistakes
  • PYQ mapping + timed mock tests

Full access for 14 days. No credit card required.