Co-Prime Number in JAVA
Co-Prime Number in JAVA
Co-Prime Number : A
Co-prime number is a set of numbers or integers which have only 1 as their
common factor i.e. their highest common factor (HCF) will be 1
Co-prime numbers are also known as relatively
prime or mutually prime numbers.
Example :-
For 21 and 22
- The factors of 21 are 1, 3, 7, and 21.
- The factors of 22 are 1, 2, 11, and 22.
For 21 and 27:
- The factors of 21 are 1, 3, 7, and 21.
- The factors of 27 are 1, 3, 9, and 27.
Program to check & Print whether two numbers are Co-prime or not :-
Program :-
import java.io.*;
import java.util.*;
class Coprime
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int a,b,p,i,x=0;
System.out.println("Enter two Numbers");
a= in.nextInt();
b= in.nextInt();
p=a*b;
for(i=1;i<p;i++)
{
if(a%i==0 && b%i==0)
{
x=i;
}
}
if(x==1)
{
System.out.println("Numbers are Co prime : "+a+" & "+b);
}
else
{
System.out.println("Numbers are not Co prime : "+a+" & "+b);
}
}// end of main method
}// end of class
import java.util.*;
class Coprime
{
public static void main(String args[])
{
Scanner in= new Scanner(System.in);
int a,b,p,i,x=0;
System.out.println("Enter two Numbers");
a= in.nextInt();
b= in.nextInt();
p=a*b;
for(i=1;i<p;i++)
{
if(a%i==0 && b%i==0)
{
x=i;
}
}
if(x==1)
{
System.out.println("Numbers are Co prime : "+a+" & "+b);
}
else
{
System.out.println("Numbers are not Co prime : "+a+" & "+b);
}
}// end of main method
}// end of class
For Proper Understanding Watch the Video :-
Watch this video : Co-Prime Number in JAVA
Follow me on Instagram
All the best :)
Keep Learning :)
Comments
Post a Comment