Question

python programming Design a function that uses recursion to raise a number to a power. The...

python programming

Design a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#To find power of n using recursion function name power
#First parameter (num) is number and second parameter (power_n) exponent
def power(num,power_n):
   #if power_n equals zero that means you need to return the value i.e. 1
    if power_n == 0:
        return 1
   #if power_n == 1 means exponent to that number is same number that you enetered
    elif power_n == 1:
        return num
   # otherwise below function call will execute
   #every time you call the function you need to decrement the value of power_n and pass it to function call
    else:
        return num * power(num,power_n-1)

NOTE: Below is the snapshot of code check for proper indentation

NOTE: Code with driver code and output

Driver code take cares of the negative number entered by the user and display the proper message

NOTE : OUTPUT

Add a comment
Know the answer?
Add Answer to:
python programming Design a function that uses recursion to raise a number to a power. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • c++ Write a function that uses recursion to raise a number to a power. The function...

    c++ Write a function that uses recursion to raise a number to a power. The function should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the function in a program. Write a function that accepts an integer argument and returns the sum of all the integers from 1 up to the number passed as an argument. For example, if 50 is passed as an argument, the function will...

  • Create a new NetBeans project from scratch named PowerDemo. Write a method that uses recursion to...

    Create a new NetBeans project from scratch named PowerDemo. Write a method that uses recursion to raise a number to a power. The method should accept two arguments: the number to be raised and the exponent. Assume that the exponent is a nonnegative integer. Demonstrate the method in a program. The main method will provide the testing code for using user input for the recursive method.

  • Write a python program (recursive.py) that contains a main() function. The main() should test the following...

    Write a python program (recursive.py) that contains a main() function. The main() should test the following functions by calling each one with several different values. Here are the functions: 1) rprint - Recursive Printing: Design a recursive function that accepts an integer argument, n, and prints the numbers 1 up through n. 2) rmult - Recursive Multiplication: Design a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times...

  • C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise...

    C++ Create three recursive functions that accomplish the following: Recursive Power Function this function will raise a number to a power. The function should accept two arguments, the number to be raised and the exponent. Assume that the exponent is a non-negative integer. String Reverser function that accepts a string object as its argument and prints the string in reverse order. Sum of Numbers this function accepts an integer argument and returns the sum of all the integers from 1...

  • Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters...

    Using Java: 1. Recursive Multiplication Write a recursive function that accepts two arguments into the parameters x and y. The function should return the value of x times y. Remember, multiplication can be performed as repeated addition as follows: 5×6=6+6+6+6+6 2. Recursive findings Write a recursive boolean method named reFinding. The method should search an array for a specified value, and return true if the value is found in the array, or false if the value is not found in...

  • (C programming) (Function arguments) Design a function prototype that can accept any type of argument and...

    (C programming) (Function arguments) Design a function prototype that can accept any type of argument and return an integer.

  • Need in Python! Write a function definition named  power  that takes two formal parameters which can be assumed...

    Need in Python! Write a function definition named  power  that takes two formal parameters which can be assumed to be any numeric type. The first parameter is called base, the second is called exponent. The function should return the result of the base raised to the exponent power.    You may not use any Python math library functions inside your function. You may create your own code to test the function in an IDE if you wish.   HINT: The exponent operator in...

  • Design a function named max that accepts two integer values as arguments and returns the value...

    Design a function named max that accepts two integer values as arguments and returns the value that is the greater of the two. For example, if 7 and 12 are passed as arguments to the function, the function should return 12. Use the function in a program that prompts the user to enter two integer values. The program should display the value that is the greater of the two. Need Variable lists, Psuedocode, ipo chart, Flow Chart, and a python...

  • In Python Use the Design Recipe to write a function factorial that when given a positive...

    In Python Use the Design Recipe to write a function factorial that when given a positive integer calculates the factorial. If given a negative integer or not an integer), the function should return None. If given 0, it should return 1. Include a docstring! Note: You may assume all arguments passed to this function will be numeric. Write three assertEqual statements to test your function. To test and receive credit for your assertEqual statements, copy them into Part E and...

  • This is for programming using Python with the JES software. Please write the code with the...

    This is for programming using Python with the JES software. Please write the code with the proper indentation. Thanks! Problem 1: Write a function that takes 2 arguments (integer or float) and computes the sum, difference, product, and quotient of the arguments. Once you calculate the values you should print out statements telling the user what arguments we used to compute the values and what the result of each calculation (your function should produce 5 lines of output in the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT