Java Programming

GCD

Java example for find the greatest common divisor of two number

9/1/2021
0 views
gcd.javaJava
import java.io.*;

class divisor {

	/* Recursive method */
	void common_divisor(int x, int y) {
		if(y == 0)
			System.out.println("GCD value: "+x);
		else

			/* Call the common_divisor method inside in it */
			common_divisor(y, x % y);
	}
}

class gcd {
	public static void main(String args[ ]) {
		divisor d = new divisor();
		d.common_divisor(Integer.parseInt(args[0]),Integer.parseInt(args[1]));
	}
}



/* Output */
java gcd 10 5
GCD value: 5
Greatest Common DivisorGCDJava Tutorial

Related Examples