Java BigInteger gcd() method

The gcd() method of Java BigInteger class is used to get the greatest common divisor of absolute values of two BigInteger. This method returns a BigInteger whose value is the greatest common divisor of abs (this) and abs (val).

Greatest Common Divisor:

The greatest common divisor (gcd) of two or more integers which are not all zero, is the largest positive integer that divides each of the integers.

For example, the gcd of 10 and 20 is 10

Syntax:

Parameter:

val - value with which the GCD is to be computed.

Returns:

This method returns GCD( abs(this) , abs(val) )

Exception:

NA

Note:
  • This method returns 0 if this and val both are equal to 0.
  • This method returns always a positive value if either of them is negative.

Example 1

Test it Now

Output:

GCD of 12 and 10 is 2

Example 2

Test it Now

Output:

GCD of -12 and -10 is 2

Example 3

Test it Now

Output:

GCD of 12 and 0 is 12

Next TopicJava BigInteger




Latest Courses