1.8k views
2 votes
Write a recursive method called printNumPattern() to output the following number pattern.Given a positive integer as input (Ex: 12), subtract another positive integer (Ex: 3) continuallyuntil 0 or a negative value is reached, and then continually add the second integer until therst integer is again reached. For this lab, do not end output with a newline.

User Zarjio
by
3.5k points

1 Answer

1 vote

Answer:

Step-by-step explanation:

The following is written in Java and prints out the pattern to and from the first num parameter that is passed to the function. Meaning it goes from num1 to 0 and then from 0 to num1 again. It prints all of the values in a single line seperated by a space and a test case was used using the values provided in the question. The output can be seen in the attached picture below.

static void printNumPattern(int n1,int n2){

System.out.print(n1 + " ");

if(n1<=0) {

return;

} else {

printNumPattern(n1-n2,n2);

}

System.out.print(n1 + " ");

}

Write a recursive method called printNumPattern() to output the following number pattern-example-1
User Stefan Feuerhahn
by
4.2k points