CO Prime - Java
13-12-2018    2476 times

Accept two no from the user and write a program in Java to check that those numbers are co- prime or not.

If any two given number has no common factor except 1 then that pair of the numbers known as co-prime. The co-prime numbers are may or may not be a prime number themself.

Let two numbers are 14 and 15.

The factors of 14 are 1, 2, 7, 14 and the factors of 15 are 1, 3, 5, 15 the common factor is 1. Therefore, 14 and 15 are the pair of co-prime. In others words, if the HCF of two numbers is 1 then, that pair of numbers are called co-prime. Co-prime numbers are also known as a relatively prime or mutually prime.

import java.util.*;

public class CoPrime
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the first no");
int no1=sc.nextInt();
System.out.println("Enter the second no");
int no2=sc.nextInt();
int temp;
if(no2>no1)
{
temp=no2;
no2=no1;
no1=temp;
}
int r=-1;
while(r!=0)
{
r=no1%no2;
no1=no2;
no2=r;
}
if(no1==1)
{
System.out.println("CO Prime Number");
}
else
{
System.out.println("Not a CO Prime Number");
}
}
}