Answer:
import java.util.*;
// for Scanner
public class Lab4Q2{
public static void main(String[] args){
Scanner console = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int num1 = console.nextInt();
System.out.print("\nEnter a second positive integer: ");
int num2 = console.nextInt();
System.out.println();
printRange(num1, num2);
}
public static void printRange(int a, int b){
if(a == b){
System.out.print(a);
} else if (a < b){
for(int i = a; i <= b; i++){
System.out.print(i + " ");
}
}
else if (a > b){
for(int i = a; i >= b; i--){
System.out.print(i + " ");
}
}
}
}
Explanation:
In the printRange method that is called from the main method; we pass the two parameters of numbers entered as 'a' and 'b'. First, we check if 'a' and 'b' are the same, then we output a single instance of the input.
Next, we check if the first input is less than the second input then we output in ascending order.
Lastly, we check if the first input is greater than the second input then we output in descending order.