26.6k views
1 vote
Implement a recursive program that takes in a number and finds the square of that number through addition. For example if the number 3 is entered, you would add 3+3+3=9. If 4 is entered you would add 4+4+4+4=16. This program must be implemented using recursion to add the numbers together. mips

User Carletta
by
8.3k points

1 Answer

3 votes

Answer:

def recurSquare(n):

if n == 0:

return 0

return recurSquare(n-1) + n + n-1

print(recurSquare(2))

Step-by-step explanation:

Programming language used is Python.

The recursive function is defined as recurSquare(n)

The IF statement checks the argument that is passed to the function to ensure it is not equal to zero.

The function is a recursive function, which means it calls itself.

In the return statement the function calls itself and the argument is reduced by 1.

The function keeps calling itself until n is equal to zero (n == 0) it returns zero and the function stops.

Implement a recursive program that takes in a number and finds the square of that-example-1
User Ruslan
by
8.7k points