Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The "TestPrimeFactors" class.
- import java.awt.*;
- import hsa.Console;
- public class TestPrimeFactors
- {
- static Console c; // The output console
- public static void main (String[] args)
- {
- c = new Console ();
- c.println ("Please enter an integer to find the prime factors");
- int number = c.readInt();
- c.println("These are the prime factors of the number ");
- PrimeFactors(number);
- // Place your program here. 'c' is the output console
- } // main method
- static void PrimeFactors( int number)
- {
- boolean found = false;
- for (int i = 2; i<number; i++)
- {
- if (LCD (number, i) == i)
- {
- found = true;
- c.println (i + " is a prime factor of " + number);
- }
- }
- if (!found)
- {
- c.println ("Only 1 and " + number + " are factors.");
- }
- }
- static int LCD ( int x, int y)
- {
- int smallest;
- int largest;
- boolean found = false;
- if (x>y)
- {
- smallest = x;
- largest = y;
- }
- else
- {
- smallest = x;
- largest = y;
- }
- for (int checker = 2; checker<smallest; checker ++)
- {
- if (smallest % checker == 0)
- {
- found = true;
- return checker;
- }
- }
- return 1;
- }
- } // TestPrimeFactors class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement