GCD and LCM were hard nut to crack in my school days. Well, today you will be learning java program to find GCD And LCM of two numbers.

First let us understand what these terms mean in java??

## Greatest Common Divisor(GCD)

Greatest Common Divisor of two integers(or more) is the largest positive integer that divides the numbers without a remainder.

## Least Common Multiple**(LCM)**

Least Common Multiple of two integers a and b is the smallest non zero positive integer that is divisible by both a and b.

### example: Java Program To Find GCD And LCM Of Two Numbers

Also Read – Java Program To Find Even Or Odd

import java.util.Scanner; public class LCM_GCD { static int lcm(int a, int b){ int x; x = (a > b) ? a : b; while(true){ if(x % a == 0 && x % b == 0) return x; ++x; } } static int gcd(int a, int b){ int z = 0; int p, q; p = (a > b) ? a : b; q = (a < b) ? a : b; z = q; while(p % q != 0){ z = p % q; p = q; q = z; } return z; } public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Please enter two numbers: "); int a = sc.nextInt(); int b = sc.nextInt(); System.out.println("The LCM of two numbers is: " +lcm(a,b)); System.out.println("The GCD of two numbers is: " +gcd(a,b)); sc.close(); } }

**Output:**

Please enter two numbers: 15 25

The LCM of two numbers is: 75

The GCD of two numbers is: 5