// Miya Natsuhara // 07-12-2019 // CSE142 // TA: Grace Hopper // Given a number from the user, prints out different mathematical properties and facts about that // number. import java.util.*; public class FunWithNumbers { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Please type an integer: "); int num = console.nextInt(); isZero(num); isEvenOrOdd(num); isPosOrNeg(num); getSmallPrimeFactors(num); System.out.println("sum of numbers from 1 to " + num + " is " + gaussSum(num)); } // Prints out whether the given number is 0. // int num: number to test public static void isZero(int num) { if (num == 0) { System.out.println("0 is zero!"); } } // Prints out whether the given number is even or odd. // int num: 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!"); } } // Prints out whether the given number is positive or negative. // int num: 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!"); } } // Prints out whether the small prime number (2,3,5,7) are factors of the given number. // int num: number whose factos will be looked at public static void getSmallPrimeFactors(int num) { if (num % 2 == 0) { System.out.println("2 is a factor of " + num); } if (num % 3 == 0) { System.out.println("3 is a factor of " + num); } if (num % 5 == 0) { System.out.println("5 is a factor of " + num); } if (num % 7 == 0) { System.out.println("7 is a factor of " + num); } } // Computes and returns the sum of all numbers up to and including the given number // int num: number whose value will be used to sum up to public static int gaussSum(int num) { // NOTE: An example of a *cumulative algorithm* where we set up our result variable // outside of the loop, and then accumulate pieces of the result inside of the loop. // keep a running total // for every number 1 to the number // add in that number to the running total int runningTotal = 0; for (int i = 1; i <= num; i++) { runningTotal = runningTotal + i; } return runningTotal; } }