Java program to find Greatest Common Divisor of 2 Integer numbers.

Below program will find out GCD of 2 given integer numbers.

package testjava.controlflow;

public class GreatestCommonDivisor 
{
	public void getGreatestCommonDivisor(int first, int second)
	{	
		int num1 = first;
		int num2 = second;
		
		while(num1 != num2)
		{
			if(num1 > num2)
				num1 = num1 - num2;
			else
				num2 = num2 - num1;
		}
		System.out.println("Greatest common divisor of "+first+" and "+second+" is: "+num1);	
}
	
	public static void main(String args[])
	{
		GreatestCommonDivisor gcd = new GreatestCommonDivisor();
		
		gcd.getGreatestCommonDivisor(25, 15);
		gcd.getGreatestCommonDivisor(12, 30);
		gcd.getGreatestCommonDivisor(81, 153);
		
	}
}

OUTPUT

Greatest common divisor of 25 and 15 is 5
Greatest common divisor of 12 and 30 is 6
Greatest common divisor of 81 and 153 is 9