Write a program to find the greatest common divisor of two numbers.
Greatest Common Divisor in Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Create a Scanner object System.out.print("Enter a Number 1:"); int n1 = scanner.nextInt(); System.out.print("Enter a Number 2:"); int n2 = scanner.nextInt(); while(n1 != n2) { if(n1 > n2) n1 -= n2; else n2 -= n1; } System.out.println("G.C.D = " + n1); } |
Output: