Two positive integers are co-prime numbers if their GCD (Greatest Common Divisor) is 1. For example, 8 and 13 are co-prime. Any two consecutive integers are always co-prime. Any integer and 1 are always co-prime. Two prime numbers are always co-prime.
Write a Java program to input two positive integers and check and display whether they are co-prime or not.
import java.util.Scanner;
class CoPrime{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
System.out.print("First number: ");
int a = Integer.parseInt(in.nextLine());
System.out.print("Second number: ");
int b = Integer.parseInt(in.nextLine());
if(a < 1 || b < 1){
System.out.println("INVALID INPUT");
return;
}
if(isCoPrime(a, b))
System.out.println("They are co-primes!");
else
System.out.println("They are not co-primes.");
}
public static boolean isCoPrime(int a, int b){
while(b != 0){
int rem = a % b;
a = b;
b = rem;
}
return a == 1;
}
}