// This programs tests an integer input by the user for various // fun properties. // // Notice the use of the different if-else constructs. public class FunWithNumbers { public static void main(String[] args) { int number = 24; isZero(number); System.out.println(number + " has " + numFactors(number) + " factors"); isPrime(number); isEvenOrOdd(number); isPosOrNeg(number); isPosOrNeg(-3); isPosOrNeg(0); int max = max(1, 8); System.out.println("maximum is " + max); getFactors(84); // nextHailstoneNum(number); } // Determines whether or not the given integer is 0. // // int num - the number to test public static void isZero(int num) { System.out.println("Is " + num + " zero?"); if (num == 0) { System.out.println(num + " is zero!"); } System.out.println("All done!"); } // Computes and returns the number of factors of the given integer. // // int num - the integer whose factors are to be computed public static int numFactors(int num) { int count = 0; // for each integer from 1 to num for (int i = 1; i <= num; i++) { // check for divisibility if (num % i == 0) { // if divisible, add 1 to factor count count++; } } return count; } // Determines whether a given integer is prime. // // int num - the number to test public static void isPrime(int num) { if (numFactors(num) == 2) { System.out.println(num + " is prime!"); } System.out.println("Primeness determined."); } // Determines whether a given integer is even or odd. // // int num - the number to test public static void isEvenOrOdd(int num) { if (num % 2 == 0) { System.out.println(num + " is even!"); } else { System.out.println(num + " is odd!"); } } // Determines whether a given integer is positive or negative. // // int num - the number to test public static void isPosOrNeg(int num) { if (num > 0) { System.out.println(num + " is positive"); } else if (num < 0) { System.out.println(num + " is negative"); } else { System.out.println(num + " is zero"); } } // Determines whether a given integer is a multiple of each single // digit prime number. // // int num - the number to test public static void getFactors(int num) { if (num % 2 == 0) { System.out.println(num + " is a multiple of 2"); } if (num % 3 == 0) { System.out.println(num + " is a multiple of 3"); } if (num % 5 == 0) { System.out.println(num + " is a multiple of 5"); } if (num % 7 == 0) { System.out.println(num + " is a multiple of 7"); } } public static int max(int num1, int num2) { if (num1 > num2) { return num1; } else { return num2; } } // Computes the next value after the given value in the /// hailstone sequence. The hailstone sequences is defined // as follows: // - if the input is even, the next value is half the input (n / 2) // - if the input is odd, the next value is three times the input // plus one (3n + 1) // // int num - the number to test public static void nextHailstoneNum(int num) { if (num > 0) { System.out.print("The next hailstone number is "); if (num % 2 == 0) { System.out.println(num / 2); } else { // num % 2 == 1 System.out.println(3 * num + 1); } } } }