In this tutorial you will learn about the Program to check CoPrime Numbers Program in Java and its application with practical example.
In this tutorial, we will learn to create a Java Program to check CoPrime Numbers Program in Java
Before starting with this tutorial, we assume that you are best aware of the following Java programming concepts:
- Java Operators.
- Basic Input and Output function in Java.
- Class and Object in Java.
- Basic Java programming.
- If-else statements in Java.
- For loop in Java.
What is Co-prime Number.
The numbers are said to be co-prime, if their GCD is “1”
So 13,14 and 15 are co-prime numbers.
Program to check CoPrime Numbers Program in Java
In this program we would find the given two number are co-prime or not first will take the input from the user and check they arr coprime or not. let have a look at the code
//java program to find co -prime
public static void main(String args)
//declaring variables and taking vlaues from user..
Scanner in= new Scanner(System.in);
System.out.println("Enter two Numbers");
// condition for co-prime number
if(num1%i==0 && num2%i==0)
// if result is 1 they are co-prime number
System.out.println("Numbers are Co prime : "+num1+" & "+num2);
System.out.println("Numbers are not Co prime : "+num1+" & "+num2);
}// end of main method
}// end of class
Not a Co-prime
In the above program, we have first declared and initialized a set variables required in the program.
- Num1,num2 = it will hold entered numbers
- a= for iteration.
- c= for count.
After declaring variables we take two values from user.
Co-prime numbers are the numbers whose common factor is 1 lets look at the example.
Number 2 3
The factors of 2 are 1, 2. and factor of 3 is 1,3
They are Co-Prime.
Input : 4 8
The factors of 4 are 1, 2 and 4. and factor of 8 is 1,2,4 and 8
They are not Co-Prime.
after that we find factors of given number
if number has common factor 1 then they are co-prime
if not they are not co-prime numbers.