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
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
Computer Science,
17 hours ago
Chemistry,
17 hours ago
English,
17 hours ago
Science,
8 months ago
Physics,
8 months ago