In: Computer Science
Create a Java to find the GCD of two numbers. You will need to create a new variable to reflect the algorithm (numberOne, numberTwo, GCD = 0).
Requirements:
1) read in numberOne
2) read in numberTwo.
3) run the loop, If numberTwo is 0 then print out numberOne, otherwise, temp =numberOne % numberTwo, numberOne = numberTwo, numberTwo = temp.
import java.util.Scanner; public class GCD { public static void main(String args[]) { int numberOne, numberTwo, temp; Scanner in = new Scanner(System.in); // read in numberOne System.out.print("Enter value for numberOne: "); numberOne = in.nextInt(); // read in numberTwo. System.out.print("Enter value for numberTwo: "); numberTwo = in.nextInt(); // Checking numberTwo is zero while (numberTwo!=0) { // temp is the remainder of numberOne and numberTwo temp = numberOne % numberTwo; // Assigning numberTwo to numberOne numberOne = numberTwo; // Assigning temp to numberTwo numberTwo = temp; } // Printing the GCD value System.out.println("GCD = "+numberOne); } }