In: Computer Science
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. (Java)
Thanks for the question. Below is the code you will be needing Let me know if you have any doubts or if you need anything to change. Thank You !! =========================================================================== import java.util.Scanner; public class Divisor { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer in the range (10-20): "); int divisor = scanner.nextInt(); int count = 1; if (divisor >= 10 && divisor <= 20) { int startingNumber = 1; int endingNumber = 1000; while (startingNumber <= endingNumber) { if (startingNumber % divisor == 0) { System.out.printf("%5d", startingNumber); count += 1; } if (count % 11 == 0) { System.out.println(); count=1; } startingNumber += 1; } } else { System.out.println("Value entered not in range 10 20."); } } }
===============================================================