Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Write a recursive method that accepts an integer n and prints the first n pos

ID: 3788964 • Letter: 1

Question

1. Write a recursive method that accepts an integer n and prints the first n positive integers to the Java console. For example:

n, (n – 1), (n – 2), ..., 3, 2, 1

For example, if n = 10, then the following would be printed to the Java console:

10 9 8 7 6 5 4 3 2 1

2. Write a recursive method that accepts an integer n and prints all multiples of 3 from n down to 3.

For example, if I was given n = 20 then I would send the following output to the Java console:

18 15 12 9 6 3

Note: do not assume that n begins as a multiple of 3

Explanation / Answer

1)

import java.util.Scanner;

public class Numbers {

public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scan.nextInt();
printPositive(number);

}

public static void printPositive(int num) {

if(num>=1)
{
System.out.print(num+ " ");
printPositive(num-1);
}
}
}

2)

import java.util.Scanner;

public class Numbers {

public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = scan.nextInt();
multiply(number);

}

public static void multiply(int num) {
if(num%3==0 && num>=3)
{System.out.println(num);
}
multiply(num-1);
}
}