Advertisement
Guest User

l33t hax

a guest
Dec 13th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.34 KB | None | 0 0
  1. // The "TestPrimeFactors" class.
  2. import java.awt.*;
  3. import hsa.Console;
  4.  
  5. public class TestPrimeFactors
  6. {
  7.     static Console c;           // The output console
  8.    
  9.     public static void main (String[] args)
  10.     {
  11.         c = new Console ();
  12.  
  13.        
  14.         c.println ("Please enter an integer to find the prime factors");
  15.         int number = c.readInt();
  16.        
  17.         c.println("These are the prime factors of the number ");
  18.        
  19.          PrimeFactors(number);
  20.      
  21.    
  22.        
  23.        
  24.         // Place your program here.  'c' is the output console
  25.     } // main method
  26. static void PrimeFactors( int number)
  27. {
  28. boolean found = false;
  29.  
  30. for (int i = 2; i<number; i++)
  31. {
  32. if (LCD (number, i) == i)
  33. {
  34. found = true;
  35. c.println (i + " is a prime factor of " + number);
  36. }
  37. }
  38. if (!found)
  39. {
  40. c.println ("Only 1 and " + number + " are factors.");
  41. }
  42. }
  43.  
  44.  
  45.  
  46.  static int LCD ( int x, int y)
  47.  
  48.    {
  49.     int smallest;
  50.     int largest;
  51.     boolean found = false;
  52.    
  53.     if (x>y)
  54.     {
  55.     smallest = x;
  56.     largest = y;
  57.     }
  58.     else
  59.    
  60.     {
  61.     smallest = x;
  62.     largest = y;
  63.     }
  64.    
  65.     for (int checker = 2; checker<smallest; checker ++)
  66.    
  67.     {
  68.     if (smallest % checker == 0)
  69.     {
  70.     found = true;
  71.     return checker;
  72.     }
  73.     }
  74.    
  75.     return 1;
  76.    
  77. }
  78. } // TestPrimeFactors class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement