Question

PYTHON: Write a function that takes in a number as a parameter and calculates the square...

PYTHON: Write a function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter. The function must perform the process recursively.

Homework Answers

Answer #1

Code:

def square(n): # defining a recursive function
    if n==0: # defining exit case
        return 1
    else:
        square(n-1) # calling the function recursively
        print(n*n)  # printing the values

# driver code
n = int(input("Enter the number: "))  # taking input
square(n)  # calling the function

Output:

Know the answer?
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
Similar Questions
Python: Write a function that takes in a number as a parameter and calculates the square...
Python: Write a function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter. Please use a while or for loop.
Python: Write a recursive function that takes in a number as a parameter and calculates the...
Python: Write a recursive function that takes in a number as a parameter and calculates the square of the number up to and including the value of the parameter.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python function that takes a list of integers as a parameter and returns the...
Write a Python function that takes a list of integers as a parameter and returns the sum of the elements in the list. Thank you.
Write a Python program using that takes a number as input and then prints if the...
Write a Python program using that takes a number as input and then prints if the number is even or odd. The program should use a function isEven that takes a number as a parameter and returns a logical value true if the number is even, false otherwise.
Write a Python function that takes a filename as a parameter and returns the string 'rows'...
Write a Python function that takes a filename as a parameter and returns the string 'rows' if a file was written row by row, and 'columns' if the file was written column by column. You would call the function with a command like filetype = myfunc(filename).
Python Jupyter Notebook Write a python function called profit that takes the number of meals (n)...
Python Jupyter Notebook Write a python function called profit that takes the number of meals (n) sold by a restaurant in a month and calculates the monthly profit given the following information: The selling price of each meal is 12 USD (same for all meals) There is a fixed cost of 2500 USD per month regardless of the number of meals sold. There is a variable cost of 6 USD per each meal sold profit= total revenue- total cost =...
python a) Write a function, hailstone(), that takes an integer value n as a parameter, and...
python a) Write a function, hailstone(), that takes an integer value n as a parameter, and displays the hailstone sequence for the given integer. The hailstone sequence is determined as follows: if the value is even, divide by 2 (floor division) or if the value is odd, calculate 3 * n + 1. The function should display each value and continue updating the value until it becomes 1. b) Write a program to display the hailstone sequence of all integers...
Write a Python function evaluateFunction() that takes one floating point number x as its argument and...
Write a Python function evaluateFunction() that takes one floating point number x as its argument and returns 3√(log⁡|x| )+3(x^3) The math module has a square root function and a logarithm function
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT