/* (Extra program; we did not write this one in class!) This program reads two numbers from the user, sees whether each is prime, and reports the next prime after each. It also reports whether the two numbers are relatively prime (have no common factors). EXPECTED OUTPUT: Type two numbers: 9 16 the next prime after 9 is 11 the next prime after 16 is 17 9 and 16 are relatively prime Type two numbers: 7 21 the next prime after 7 is 7 the next prime after 21 is 23 7 and 21 are not relatively prime */ import java.util.*; public class Primes { public static void main(String[] args) { Scanner console = new Scanner(System.in); System.out.print("Type two numbers: "); int num1 = console.nextInt(); int num2 = console.nextInt(); System.out.println("the next prime after " + num1 + " is " + nextPrime(num1)); System.out.println("the next prime after " + num2 + " is " + nextPrime(num2)); if (commonFactors(num1, num2) <= 2) { System.out.println(num1 + " and " + num2 + " are relatively prime"); } else { System.out.println(num1 + " and " + num2 + " are not relatively prime"); } } public static int nextPrime(int number) { while (countFactors(number) > 2) { number++; } return number; // claim: number is now prime } public static int countFactors(int number) { // try every number between 1 and number to see if it is a factor int count = 0; for (int i = 1; i <= number; i++) { if (number % i == 0) { count++; // i is a factor } } return count; } public static int commonFactors(int n1, int n2) { // try every number between 1 and n1 or n2 int count = 0; for (int i = 1; i <= Math.min(n1, n2); i++) { if (n1 % i == 0) { if (n2 % i == 0) { count++; // i is a common factor } } } return count; } }