binibiningtinamoran

PrintFactors.java

Nov 19th, 2019
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.86 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class PrintFactors {
  4.  
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner scan = new Scanner(System.in);
  8.         System.out.print("Enter a limit: ");
  9.         int limit = Integer.parseInt(scan.nextLine());
  10.         while(limit < 0) {
  11.             System.out.println("Input must be positive!");
  12.             System.out.print("Enter a limit: ");
  13.             limit = Integer.parseInt(scan.nextLine());
  14.         }
  15.  
  16.         int num = 1;
  17.         while(num <= limit) {
  18.             System.out.printf("Factors of %,d: ",num);
  19.             printFactors(num);
  20.             System.out.println();
  21.             num ++;
  22.         }
  23.     }
  24.  
  25.     public static void printFactors(int num) {
  26.         for (int i = 1; i <= num; ++i) {
  27.             if (num % i == 0) {
  28.                 System.out.print(i + " ");
  29.             }
  30.         }
  31.     }
  32. }
Advertisement
Add Comment
Please, Sign In to add comment