1 Write a recursive method that accepts an integer n and pri

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

Solution

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);
}
}

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. 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,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site