In: Computer Science
CS 206 Visual Programming, Netbeans
Understand and modify the attached GCDMethod.java so it can find
the greatest common divisor of three numbers input
by the user.  Hints: modify the gcd() method by
adding the third parameter.
import java.util.Scanner;
public class GCDMethod {
  /** Main method */
  public static void main(String[] args) {
    // Create a Scanner
    Scanner input = new Scanner(System.in);
    // Prompt the user to enter two integers
    System.out.print("Enter first integer: ");
    int n1 = input.nextInt();
    System.out.print("Enter second integer: ");
    int n2 = input.nextInt();
    System.out.println("The greatest common divisor for " + n1 +
      " and " + n2 + " is " + gcd(n1, n2));
  }
  /** Return the gcd of two integers */
  public static int gcd(int n1, int n2) {
    int gcd = 1; // Initial gcd is 1
    int k = 1; // Possible gcd
    
    while (k <= n1 && k <= n2) {
      if (n1 % k == 0 && n2 % k == 0)
        gcd = k; // Update gcd
      k++;
    }
    return gcd; // Return gcd
  }
}
This is the GCDMethod.java attachment
import java.util.Scanner;
public class GCDMethod {
    /** Main method */
    public static void main(String[] args) {
        // Create a Scanner
        Scanner input = new Scanner(System.in);
        // Prompt the user to enter two integers
        System.out.print("Enter first integer: ");
        int n1 = input.nextInt();
        System.out.print("Enter second integer: ");
        int n2 = input.nextInt();
        System.out.print("Enter second integer: ");
        int n3 = input.nextInt();
        System.out.println("The greatest common divisor for " + n1 +
                " and " + n2 + " and "+n3+" is " + gcd(n1, n2,n3));
    }
    /** Return the gcd of two integers */
    public static int gcd(int n1, int n2) {
        int gcd = 1; // Initial gcd is 1
        int k = 1; // Possible gcd
        while (k <= n1 && k <= n2) {
            if (n1 % k == 0 && n2 % k == 0)
                gcd = k; // Update gcd
            k++;
        }
        return gcd; // Return gcd
    }
    public static int gcd(int n1, int n2, int n3) {
        return gcd(n1,gcd(n2,n3));
    }
}

