The algorithm to find the greatest common divisor of two integers n1 and n2:
First find d to be the minimum of n1 and n2.
Check whether d, d-1, d-2, . .., 2, or 1 is a divisor for both n1 and n2 in this order.
The first such common divisor is the greatest common divisor for n1 and n2.
Write a program that prompts the user to enter two positive integers and displays the gcd.
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer number: "); int num1 = input.nextInt(); System.out.print("Enter second integer number: "); int num2 = input.nextInt(); input.close();/*from ww w . ja v a 2 s . c o m*/ //your code here } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("Enter first integer number: "); int num1 = input.nextInt(); System.out.print("Enter second integer number: "); int num2 = input.nextInt(); input.close(); int denominator = (num1 > num2) ? num2 : num1; for (; denominator > 0; denominator--) if (num1 % denominator == 0 && num2 % denominator == 0) break; System.out.println("GCD of " + num1 + " and " + num2 + " is " + denominator); } }