Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class PrintFactors {
- public static void main(String[] args) {
- Scanner scan = new Scanner(System.in);
- System.out.print("Enter a limit: ");
- int limit = Integer.parseInt(scan.nextLine());
- while(limit < 0) {
- System.out.println("Input must be positive!");
- System.out.print("Enter a limit: ");
- limit = Integer.parseInt(scan.nextLine());
- }
- int num = 1;
- while(num <= limit) {
- System.out.printf("Factors of %,d: ",num);
- printFactors(num);
- System.out.println();
- num ++;
- }
- }
- public static void printFactors(int num) {
- for (int i = 1; i <= num; ++i) {
- if (num % i == 0) {
- System.out.print(i + " ");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment