BigInteger gcd() Method in Java with Examples Last Updated : 04 Apr, 2019 Comments Improve Suggest changes Like Article Like Report GCD (Greatest Common Divisor) or HCF (Highest Common Factor) of two numbers is the largest number that divides both of them. The java.math.BigInteger.gcd(BigInteger val) method is used to calculate gcd of two BigIntegers. This method calculates gcd upon the current BigInteger by which this method is called and BigInteger passed as parameter Syntax: public BigInteger gcd(BigInteger val) Parameters: This method accepts a parameter val which is one of the numbers out of two whose gcd is to be calculated. The number should be of BigInteger type. Return value: This method returns a BigInteger which holds the calculated gcd of two BigIntegers. Below program is used to illustrate the gcd() method of BigInteger. Example 1: java // Java program to demonstrate // gcd() method of BigInteger import java.math.BigInteger; public class GFG { public static void main(String[] args) { // BigInteger object to store the result BigInteger result; // For user input // Use Scanner or BufferedReader // Two objects of String created // Holds the values to calculate gcd String input1 = "54"; String input2 = "42"; // Creating two BigInteger objects BigInteger a = new BigInteger(input1); BigInteger b = new BigInteger(input2); // Calculate gcd result = a.gcd(b); // Print result System.out.println("The GCD of " + a + " and " + b + " is " + result); } } Output: The GCD of 54 and 42 is 6 Example 2: Java // Java program to demonstrate // gcd() method of BigInteger import java.math.BigInteger; public class GFG { public static void main(String[] args) { // BigInteger object to store result BigInteger result; // For user input // Use Scanner or BufferedReader // Two objects of String // Holds the values to calculate gcd String input1 = "4095484568135646548"; String input2 = "9014548534231345454"; // Creating two BigInteger objects BigInteger a = new BigInteger(input1); BigInteger b = new BigInteger(input2); // Calculate gcd result = a.gcd(b); // Print result System.out.println("The GCD of " + a + " and " + b + " is " + result); } } Output: The GCD of 4095484568135646548 and 9014548534231345454 is 2 Reference: https://docs.oracle.com/en/java/javase/12/docs/api/java.base/java/math/BigInteger.html#gcd(java.math.BigInteger) Comment More infoAdvertise with us Next Article BigInteger gcd() Method in Java with Examples R Rajnis09 Follow Improve Article Tags : Java Java-Functions Java-BigInteger Java-math-package Practice Tags : JavaJava-BigInteger Similar Reads BigInteger divide() Method in Java with Examples The java.math.BigInteger.divide(BigInteger val) is used to calculate the division of two BigIntegers. BigInteger class internally uses array of integers for processing, the operation on object of BigIntegers are not as fast as on primitives. This method performs an operation upon the current BigInte 3 min read BigDecimal divide() Method in Java with Examples The java.math.BigDecimal.divide(BigDecimal divisor) is used to calculate the Quotient of two BigDecimals. The Quotient is given by (this / divisor). This method performs an operation upon the current BigDecimal by which this method is called and the BigDecimal passed as the parameter. There are five 5 min read BigDecimal divideAndRemainder() Method in Java with Examples The java.math.BigDecimal.divideAndRemainder(BigDecimal divisor) is used to calculate both quotient and remainder of two BigDecimals. If both the integer quotient and remainder are needed then this method is faster than using the divideToIntegralValue() and remainder() methods separately because the 4 min read BigDecimal divideToIntegralValue() Method in Java with Examples The java.math.BigDecimal.divideToIntegralValue(BigDecimal divisor) is used to calculate the integer part of the quotient of two BigDecimals (this / divisor) which is rounded down. The preferred scale of the result is (this.scale() - divisor.scale()). This method performs an operation upon the curren 4 min read BigDecimal remainder() method in Java with Examples The java.math.BigDecimal.remainder(BigDecimal divisor) is used to calculate the remainder of two BigDecimals. The remainder is given by this.subtract(this.divideToIntegralValue(divisor).multiply(divisor)). This method performs an operation upon the current BigDecimal by which this method is called a 3 min read Like