ไม่สามารถเล่นวิดีโอนี้
ขออภัยในความไม่สะดวก
Power of a Number Using Recursion understanding return in recursion || Lesson 35.2 || Python ||
ฝัง
- เผยแพร่เมื่อ 18 ส.ค. 2024
- #python#learningmonkey#pythoncoding#placements#pythontutorials#pythoncourse#pythonforbeginners#pythonfordatascience#pythonfullcourse#pythonprogramming#pythonfreecourse
Power of a Number Using Recursion Understanding return in Recursion.
In this class, we write a program to find the Power of a Number Using Recursion Understanding return in Recursion.
Power of a Number Using Recursion
The reader should have prior knowledge of recursive functions execution. Click here.
Take an example and understand the question better.
Example:
Two power five will be 2*2*2*2*2. multiply 2 five times. We get the value 32.
Two power -5. The output will be 1 / 2 power 5.
Input: Given base and exponent value.
Output: Calculate base power exponent value.
This example will help you understand the return statement in recursive functions.
Logic
The function will take the base and exponent values.
Recursively call the function with a change in exponent value.
Each recursive function call reduces the exponent value by 1.
Recursively call the function if the exponent is greater than 0.
Each time multiple by base value and the value return by the function call.
Check the below program and analyze the code for better practice.
Program
def power(base,exponent):
if exponent==0:
return 1
elif exponent gt 0:
return base * power(base,exponent-1)
else:
return 1/power(base,-exponent)
base=int(input("enter the number"))
exponent=int(input("enter power of a number"))
result=power(base,exponent)
print(result)
From the above program, return base * power(base, exponent-1).
The function has to return the value base* power(base,exponent-1).
In the above line of code, the return statement is again calling the function.
The above line execution will take a base value, and it will be multiplied by the value returned by the function power(base,exponent-1).
Then the value obtained after multiplication with base is returned to the calling function.
If the exponent is negative, we are returning 1/power(base,-exponent).
For beginners, A detailed explanation of recursion execution is provided in the video.
Link for playlists:
/ @learningmonkey
Link for our website: learningmonkey.in
Follow us on Facebook @ / learningmonkey
Follow us on Instagram @ / learningmonkey1
Follow us on Twitter @ / _learningmonkey
Mail us @ learningmonkey01@gmail.com
Thanks brother... Literally i search the logic of this problem more than 3- 4 places.. And nobody explain well but you made it clear... Thanks again ❤
Have a great learning in CSE
nice explination.
Thank you
Have a great learning in CSE
wow bro . that's awesome
Have a great learning in CSE
Great sir... thank u so much ❤
Have a great learning in CSE
good explanation.....thank you
Super sir
Have a great learning in CSE
Crystal clear bro
keep help us...thank you!
Very helpful, thank you!
thank you so much
Excellent
Thankyou
11^11 not show accurate answer. What can I do. Plz answer me
iits working
def power(base,exponent):
if exponent==0:
return 1
elif exponent >0:
return base * power(base,exponent-1)
else:
return 1/power(base,-exponent)
base=int(input("enter the number"))
exponent=int(input("enter power of a number"))
result=power(base,exponent)
print(result)
@@LearningMonkey thanks 😊 🙏
in the above program 2^-5 is not run
It's working. Post your code once
How will the n in the else part ever reach value 0 we have to add 1 while calling ( - exp +1)
It looks like infinite loop