204k views
5 votes
Write a recursive function called print_num_pattern() to output the following number pattern.Given a positive integer as input (Ex: 12), subtract another positive integer (Ex: 3) continually until a negative value is reached, and then continually add the second integer until the first integer is again reached. For this lab, do not end output with a newline.THIS IS THE PROMPT I HAVE# TODO: Write recursive print_num_pattern() functionif __name__ == "__main__":num1 = int(input())num2 = int(input())print_num_pattern(num1, num2)Do not modify the given main program.Ex. If the input is:12 3the output is:12 9 6 3 0 -3 0 3 6 9 12

2 Answers

3 votes

Final Answer:

The recursive function print_num_pattern() will generate the desired number pattern by subtracting the second integer from the first until a negative value is reached and then adding the second integer until the first integer is reached again. The output will display these numbers in sequence, as specified.

Step-by-step explanation:

The function print_num_pattern() uses recursion to generate the number pattern. When given inputs (num1 = 12, num2 = 3), it starts by printing num1 and then recursively calls itself with num1 - num2 until the value becomes negative. Upon reaching a negative value, it prints 0 and then recursively calls itself with num1 + num2 until num1 is reached again.

For instance, starting with num1 = 12 and num2 = 3:

1. print_num_pattern(12, 3) prints 12 and calls print_num_pattern(9, 3).

2. print_num_pattern(9, 3) prints 9 and calls print_num_pattern(6, 3).

3. This sequence continues until print_num_pattern(0, 3) is reached, printing 0 and triggering the recursion to start adding num2 (3) until num1 (12) is reached again.

The pattern will display: 12 9 6 3 0 -3 0 3 6 9 12 as specified in the prompt. This recursive approach repeats the subtraction and addition cycles until it meets the condition for termination (reaching negative value and returning back to num1).

User Michael Daum
by
8.1k points
2 votes

Final answer:

The student's question has been answered by providing a recursive function print_num_pattern() in Python that correctly outputs the number pattern by subtracting and then adding back a second integer to the first.

Step-by-step explanation:

The task is to write a recursive function print_num_pattern() that takes two positive integers and prints a pattern of numbers. The function should subtract the second number from the first until a negative number is reached, and then reverse the operation by adding the second number until the first number is again reached.

Here is the Python function that accomplishes this:

def print_num_pattern(num1, num2):
print(num1, end=' ')
if num1 <= 0: # Base case: if the number is less than or equal to zero
return
else: # Recursive case
print_num_pattern(num1 - num2, num2) # recursive call with num1 - num2
print(num1, end=' ') # will execute during the unwinding of the recursion

This function will output the desired number pattern when called with the appropriate inputs.

User Mrkiffie
by
8.7k points