Check Whether a Number is a Coprime Number or Not in Java


Two numbers are said to be Coprime numbers, if the maximum common divisors is 1 for those two numbers.

For more clarification, there might be multiple divisors for any number and in some scenarios, there are some common divisors that are also present between two number’s divisors. So, if in any case we get the maximum common divisors between two numbers are 1, then those two numbers are known as coprime numbers. Simply, it means those numbers do not have any other common factors other than 1. In other words, we can say that these two numbers are mutually prime numbers.

Some examples of Coprime numbers are − (2, 3), (3, 7), (11, 19) ... etc.

To show you some instances −

Instance-1

Input numbers are 2 and 3.
Let’s check it by using the logic of the Coprime number.
Divisors of 2 are 1 and 2.
Divisors of 3 are 1 and 3.
As you notice the maximum common divisor available here is 1.
Here 2 and 3 are coprime numbers.

Instance-2

Input numbers are 8 and 15.
Let’s check it by using the logic of the Coprime number.
Divisors of 8 are 1, 2, 4 and 8.
Divisors of 15 are 1, 3, 5 and 15.
As you notice the maximum common divisor available here is 1.
Here 8 and 15 are coprime numbers.

Instance-3

Input numbers are 9 and 18.
Let’s check it by using the logic of the Coprime number.
Divisors of 9 are 1, 3, and 9.
Divisors of 18 are 1, 2, 3, 6, 9 and 18.
As you notice the maximum common divisor available here is 9.
Here 9 and 18 are not coprime numbers.

Algorithm

Step-1 − Get the input number by static input method.

Step-2 − Find out all the divisors of the numbers and return the maximum common divisor.

Step-3 − Then check whether the maximum common divisor is 1 or not.

Step-4 − If the condition is true then print two numbers are coprime numbers otherwise not.

Multiple Approaches

We have provided the solution in different approaches.

  • By User Defined Method with Static Input Values.

  • By User Defined Method with User Input Values.

Let’s see the program along with its output one by one.

Approach-1: By Using User Defined Method with Static Input Value

In this approach, we declare two variables and initialize it with values. Then call a user defined method by passing these numbers as parameters and inside the method find out all divisors of both the numbers and return the maximum common divisor. Then call another method by passing this maximum common divisor and check if it is 1 or not. If 1 then as per logic the input numbers are coprime numbers else not.

Example

public class Main{
   public static void main (String[] args) {
      int firstNumber = 5, secondNumber = 6;
      checkCoprime(firstNumber, secondNumber);
   }
   static void checkCoprime(int F, int S) {
      if ( commonDivisor(F, S) == 1)
         System.out.println("(" + F + " & " + S + ") are Co-Prime numbers");
      else
         System.out.println("(" + F + " & " + S + ") are not Co-Prime numbers");
   }
   static int commonDivisor(int F, int S) {
      if (F == 0 || S == 0)
         return 0;
      if (F == S)
         return F;
      if (F > S)
         return commonDivisor(F-S, S);
      return commonDivisor(F, S-F);
   }
}

Output

(5 & 6) are Co-Prime numbers

Approach-2: By Using User Input Value.

In this approach, we declare two variables and take user input of values. Then call a user defined method by passing these numbers as parameters and inside the method find out all divisors of both the numbers and return the maximum common divisor. Then call another method by passing this maximum common divisor and check if it is 1 or not. If 1 then as per logic the input numbers are coprime numbers else not.

Example

import java.util.*;
public class Main{
   public static void main (String[] args) {
      Scanner sc = new Scanner(System.in);
      System.out.print(" Enter First number: ");
      int firstNumber = sc.nextInt();
      System.out.print(" Enter second number: ");
      int secondNumber = sc.nextInt();
      checkCoprime(firstNumber, secondNumber);
   }
   static void checkCoprime(int F, int S) {
      if ( commonDivisor(F, S) == 1)
         System.out.println("(" + F + " & " + S + ") are Co-Prime numbers");
      else
         System.out.println("(" + F + " & " + S + ") are not Coprime numbers");
   }
   static int commonDivisor(int F, int S) {
      if (F == 0 || S == 0)
         return 0;
      if (F == S)
         return F;
      if (F > S)
         return commonDivisor(F-S, S);
      return commonDivisor(F, S-F);
   }
}

Output

Enter First number: 5
Enter second number: 6
(5 & 6) are Co-Prime numbers

In this article, we explored how to check whether two numbers are coprime numbers or not in Java by using different approaches.

Updated on: 27-Dec-2022

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements