In Java, we have been given numbers ‘x’ and ‘y’. Print all the numbers between 1 and x which follow the below criteria
1. Should be co-prime with x
2. Should be divisible by ‘y’
Answers
Program:
public class MyClass {
public static void main(String args[]) {
int x = 35;
int y = 5300;
for ( int i = x; i <= y; i++){
// System.out.println("Current number: " + i + "\n");
if(checkIfCoPrime(i, x)){
// System.out.println(i + " is co-prime with "+ x);
if(y % i == 0){
// System.out.println(y + " % " + i + " = " + y%i);
System.out.println(i);
}
}
}
}
public static boolean checkIfCoPrime(int a, int b){
int min, max, gcd = 1;
min = a;
if (min > b)
{
min = b;
max = a;
}
else
{
min = a;
max = b;
}
while (max > min)
{
int r = max % min;
if (r == 0)
{
gcd = min;
break;
}
else
{
max = min;
min = r;
}
}
if (gcd == 1)
{
return true;
}
else
{
return false;
}
}
}
Output:
Provided in the attachments... ( from 35 to 5300 )
Note: I used a online compiler to compile this program, you may need to make some changes.
Hope this helps you!