COMPUTER SCIENCE PYTHON AND C++
Monday, June 29, 2020
Write a Python Recursive program to calculate the value of 'a' to the power 'b'.
def
power
(
a
,
b
)
:
if
b
==
0
:
return
1
elif
a
==
0
:
return
0
elif
b
==
1
:
return
a
else
:
return
a
*
power
(
a
,
b
-
1
)
print
(
power
(
3
,
4
)
)
power
(
3
,
4
)=3*power(3,3)
power
(
3
,
4
)=3*3*power(3,2)
power
(
3
,
4
)=3*3*3*power(3,1)
power
(
3
,
4
)=3*3*3*3
power
(
3
,
4
)=81
No comments:
Post a Comment
Newer Post
Older Post
Home
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment