Question
I am creating a Greatest Common Divisor calculator. The main program, asks the user to enter in the total number of pairs that the user
I am creating a Greatest Common Divisor calculator.
The main program, asks the user to enter in the total number of pairs that the user would like to calculate the GCD. The value is scanned in. Then the program asks the user to enter in the values for each pair. Lastly, the main program outputs "Number 1 Number 2 GCD" for each pair.
The gCD(int a, int b) method uses Euclid's division algorithm. "Hint: The Greatest Common Divisor, GCD for short, of two positive integers can be computed with Euclid's division algorithm. Let the given numbers be a and b, a >= b. Euclid's division algorithm has the following steps: 1. Compute the remainder c of dividing a by b. 2. If the remainder c is zero, b is the greatest common divisor. 3. If c is not zero, replace a with b and b with the remainder c. Go back to step (1)."
Here is my code thus far; I know I have issues with my main program and I am unsure on how to fix it:
import java.util.Scanner;
public class GCD {
public static void main(int g) {
//Ask the user to "Enter in the total # of pairs to calculate their GCD."
System.out.println("Enter in the total number of pairs to calculate their GCD");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println("Enter each pair of whole integers to calculate their GCD");
for(int i = 0; i < n; i++){
int a = in.nextInt();
int b = in.nextInt();
System.out.format("Number 1: %i, Number 2: %i, GCD: %i. ", a, b, g);
gCD(a, b);
}
in.close();
}
public static int gCD(int a, int b){
int c = 0;
int g = 0;
if(a>=b){
c=a/b;//Euclid's division algorithm step 1.
if(c==0){
g = b;//Euclid's division algorithm step 2.
}
}
//Euclid's division algorithm step 3.
a = b;
b = c;
gCD(a, b);
return g;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started