Computer Science, asked by Evas, 1 day ago

write a java program to Input two numbers and check if they are co prime or not using loop(using Input Stream method if possible)​

Answers

Answered by Anonymous
1

Answer:

import java.util.Scanner;

public class CoPrimeNumbers

{

public static void main(String[] args)

{

//Declaration

Scanner input = new Scanner(System.in);

int a, b, hcf;

//Initialization

hcf = 1;

//Prompt and accept 2 numbers from user

System.out.println("Enter the 1st number: ");

a = input.nextInt();

System.out.println("Enter the 2nd number: ");

b = input.nextInt();

//Loop to calculate HCF

for (int i = 1; i <= a && i <= b; i++)

{

   if (a % i == 0 && b % i == 0)

   {

       hcf = i;

   }

}

       

if (hcf == 1)

{

           System.out.println(a + " and " + b + " are co-prime");

}

else

{

           System.out.println(a + " and " + b + " are not co-prime");

}

}

}

Similar questions