Question
*Note: I honestly just need the algothrim that computes the greatest commom divisor, so nothing fancy. By that I mean keep the code simple. Methods,
*Note: I honestly just need the algothrim that computes the greatest commom divisor, so nothing fancy. By that I mean keep the code simple. Methods, arrays, constructors, and loops are good enough. It's an early assignment in the class so I can't show up with complex code I haven't learned. However, the output has to be exactly like the ones I provided. That woud be very helpful. Thank you.
In this assignment, you will write a Java program that prompts the user for two positive integers, then prints out the greatest common divisor of the two numbers. Your program will check its input, and will repeatedly prompt the user until appropriate values are entered. The main control structure used in this program will be the loop (either while, do-while, or for). The greatest common divisor (GCD) of two positive integers is, as the name implies, the largest integer that evenly divides both numbers. For instance, the GCD of 531 and 300 is 3. One way to see this is to list all divisors of both numbers, determine the set of common divisors, then determine the largest element in that set.
Recall that if a and b are positive integers, then the quotient q and remainder r of a upon division by bare uniquelydeterminedbythetwoconditions: ??=?????+ ?? and 0 ? ?? a the dividend, b thedivisor, q the quotient and r the remainder. If ?? = 0, we say that b divides evenly into a, or simply that b divides a. (Note that the word divisor is used differently here than in the preceding paragraph, in which it meant a number which divides evenly into another. The remainder need not be zero in general.) For instance, dividing 531 by 300 yields a quotient of 1 and remainder of 231, since 531 = 300 ? 1 + 231.
The process halts when we reach a remainder of 0. The GCD is then the last non-zero remainder, which is in this case is 3. Observe that on each iteration, we discard the dividend and quotient, and save only the divisor and remainder for the next step, on which they become dividend and divisor respectively. Note also that the process must eventually terminate, since on each iteration the remainder is less than the divisor (0 ? ??
In this project you are to write a Java program that implements the Euclidean Algorithm described above. It should be clear at this point that your program must use one of Javas iterative control structures (while, forordo-while.) Recallthatifaandbareintvariablesstoringpositivevalues,thentheJavaexpression a%b evaluates to the integer remainder of a upon division by b. Observe also that the quotient is never used, so one need only declare int variables for the dividend, divisor, and remainder. On each iteration you should compute the remainder from the dividend and divisor, update the values of dividend and divisor, then go to the next iteration. The loop repetition condition should simply be that the remainder is greater than zero.
In addition, your program will carry out a robust interaction with the user to check for correct input. In particular, if the user enters anything other a positive integer, your program will continue to prompt for more input. You must design appropriate loops to carry out these checks. Format your program output to coincide with the sample runs below.
Observe that the user may enter the numbers in any order, i.e. smallerlarger, or largersmaller. Note also that the program prompts separately for the two inputs, and that non-positive integers, double values and non-numeric strings are rejected until a positive integer is entered.
Divisors of 531: {1,3, 9, 59, 177, 531) Divisors of 300: {1,2,3,4, 5, 6, 10, 12, 15, 20, 25, 30, 50, 60, 75, 100, 150, 300]) Common Divisors of 531 and 300: {1, 3}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