Question: USING JAVA: - Create a code to calculate the greatest common divisor(GCD) for two positive integers entered by the user. The greatest common divisor of

USING JAVA:

- Create a code to calculate the greatest common divisor(GCD) for two positive integers entered by the user. The greatest common divisor of a number is the largest positive integer that divides each of the integers evenly. For example the GCD of 126 and 12 is 6. (6 x 2 = 12 & 6 x 21 = 126) or the GCD of 4 & 6 is 2. (2 x 2 = 4 & 2 x 3 = 6). These numbers need to be positive as well.

Follow these three steps while coding it:

USING JAVA: - Create a code to calculate the greatest common divisor(GCD)

Part 1 - Get positive number input: For our program to work correctly we need two positive integers. Code your program so that it asks for 2 integers. Then use sentinel while loops to make sure that the input we get is a positive number. Part 2 - Calculate the GCD: Assume our numbers are saved into x&y 1. Subtract x from y repeatedly until y=x : y=x int temp =y y=x x= temp print(y) Part 3 - Loop the program: Lastly, lets have the program loop when the user enters in " y " or "Y

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!