In: Computer Science
Java Program
The program must prompt for an integer divisor in the range 10-20 and will print all numbers 1-1000 that are divisible by that divisor. You MUST use a while loop for this. Use printf to display the numbers in right-aligned columns, 10 to a line. For example, “%5d” would be the format String to allow a field width of 5 to display the number. If the number entered by the user is out of range just print an error message.
import java.util.Scanner;
class Main {
public static void main(String args[]) {
// taking user input
Scanner sc = new Scanner(System.in);
System.out.print("Enter a number (10-20): ");
int n = sc.nextInt();
int number = 1, count = 0;
// invalid case
if(n < 10 || n > 20)
System.out.println("Invalid number");
else
while(number <= 1000) // while loop
{
if(number % n == 0) // dividing
{
System.out.printf("%5d", number);
count++;
if(count % 10 == 0) // 10 in one line
{
count = 0;
System.out.println();
}
}
number++;
}
}
}
// Hit the thumbs up if you are fine with the answer. Happy Learning!