Created
August 7, 2017 13:34
-
-
Save JohnZavyn/02cf2300489188441bcc99ccec9daae8 to your computer and use it in GitHub Desktop.
Find all even divisors for a number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
/** Find all the even divisors of a number. */ | |
class LargestModulus | |
{ | |
/** | |
* Find all the divisors for a number (a.k.a., a dividend). The number must be a positive integer. It may be provided on the command line, or entered interactively when prompted. | |
* | |
* @param args if a command line parameter is detected, the first argument is used. | |
*/ | |
public static void main(final String[] args) | |
{ | |
int num = 0; | |
if (args.length == 0) | |
{ | |
final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); | |
System.out.print("Enter an integer: "); | |
try | |
{ | |
num = Integer.parseInt(reader.readLine()); | |
} | |
catch (final IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
} | |
else | |
{ | |
num = Integer.parseInt(args[0]); | |
} | |
findEvenDivisors(num); | |
} | |
/** | |
* Brute force method to find the even divisors for a number. | |
* | |
* @param dividend the number to find divisors for | |
*/ | |
public static void findEvenDivisors(final int dividend) | |
{ | |
if (dividend > 0) | |
{ | |
System.out.println("Even divisors for " + dividend); | |
for (int i = dividend / 2; i > 0; i--) | |
{ | |
final int modulus = dividend % i; | |
if (modulus == 0) | |
{ | |
System.out.println(i + " x " + dividend / i + " = " + dividend); | |
} | |
} | |
} | |
else | |
{ | |
System.out.println("ERROR: Number must be > 0"); | |
} | |
System.out.println("=== End ==="); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment