fork download
  1. import java.util.Scanner;
  2.  
  3. public class Main
  4. {
  5. static boolean isPrime(int n) {
  6. //check if n is a multiple of 2
  7. if (n%2==0) return false;
  8. //if not, then just check the odds
  9. for(int i=3;i*i<=n;i+=2) {
  10. if(n%i==0)
  11. return false;
  12. }
  13. return true;
  14. }
  15.  
  16. public static void main(String[] args) {
  17. Scanner keyboard = new Scanner(System.in);
  18. System.out.println("Enter a number:");
  19. int theNum = keyboard.nextInt();
  20.  
  21. System.out.println("\nThe prime factors of " + theNum + " are:");
  22.  
  23. for(int i=1; i <= theNum/2; i++)
  24. if (theNum % i == 0 && isPrime(i))
  25. System.out.print(i + " ");
  26. }
  27. }
Success #stdin #stdout 0.1s 380608KB
stdin
55
stdout
Enter a number:

The prime factors of 55 are:
1 5 11