Using Netbeans create a program that reads in two integer nu

Using Netbeans create a program that reads in two integer numbers and identifies if they are co-prime. Consider identifying divisors of one number in a loop and test if each of them also divides the other number.

Solution

import java.util.Scanner;

/**
* @author
*
*/
public class CoPrime {

   /**
   * determines if the values are coprime. This accepts the two positive
   * integers to be tested as parameters and returns a Boolean result. The
   * result is generated by comparing the GCD of the two values
   *
   * @param value1
   * @param value2
   * @return
   */
   public static boolean Coprime(int value1, int value2) {
       return GetGCDByModulus(value1, value2) == 1;
   }

   /**
   * determines if two values are coprime. First we need an implementation of
   * Euclid\'s Algorithm to obtain the GCD. We will use the modulus-based code
   * from the article, \"Euclid\'s Algorithm\"
   *
   * @param value1
   * @param value2
   * @return
   */
   public static int GetGCDByModulus(int value1, int value2) {
       while (value1 != 0 && value2 != 0) {
           if (value1 > value2)
               value1 %= value2;
           else
               value2 %= value1;
       }
       return Math.max(value1, value2);
   }

   /**
   * @param args
   */
   public static void main(String[] args) {

       Scanner scanner = null;
       try {

           // Scanner to read data
           scanner = new Scanner(System.in);

           // prompt to read first number
           System.out.print(\"Enter the first number:\");
           int a = scanner.nextInt();

           // prompt to read second number
           System.out.print(\"Enter the second number:\");
           int b = scanner.nextInt();

           // if there is no divisible numbers
           if (Coprime(a, b)) {
               System.out.println(\"Co-Prime\");
           } else {
               System.out.println(\"Not Co-Prime\");
           }

       } catch (Exception e) {
           // TODO: handle exception
       } finally {
           if (scanner != null)
               scanner.close();

       }
   }
}

OUTPUT:
Enter the first number:8
Enter the second number:10
Not Co-Prime

Enter the first number:9
Enter the second number:10
Co-Prime

Note:Two values are said to be coprime if they have no common prime factors.
For example, the values nine (3 x 3) and ten (2 x 5) are coprime. The values eight (2 x 2 x 2)
and ten (2 x 5) are not coprime as they share the common divisor of two.

Using Netbeans create a program that reads in two integer numbers and identifies if they are co-prime. Consider identifying divisors of one number in a loop and
Using Netbeans create a program that reads in two integer numbers and identifies if they are co-prime. Consider identifying divisors of one number in a loop and

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site