In this tutorial, we will see **“How to calculate GCD (Greatest Common Divisor) of two numbers using Java?”**

### Calculate GCD of two numbers in Java

Java – How to Calculate GCD of two numbers? (Simplest Example) Click To Tweet

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | /** * Calculate GCD of two numbers in Java * @author Deepak Verma * */ import java.math.BigInteger; public class GCD_Two_Numbers_Java8_Example { public static void main(String[] args) { int a = 80; int b = 38; int gcd = BigInteger.valueOf(a) .gcd(BigInteger.valueOf(b)) .intValue(); System.out.println("Greatest Common Divisor(GCD) of "+a+" and "+b+" = "+gcd); } } |

**Output:**

1 | Greatest Common Divisor(GCD) of 80 and 38 = 2 |

### check if text or string present in a file using java 8

In the above example, we use the `gcd`

method of the `Math`

class to calculate the greatest common divisor (GCD) of two numbers. The method takes in two integers as parameters and returns the GCD as an integer.

### Calculate GCD of two numbers in Java

###### skip method java 8 stream

Java – How to Calculate GCD of two numbers? (Simplest Example) Click To Tweet

**Do you like this Post? – then check my other helpful posts:**

- Convert a Stream to a List in Java 8
- Stream maptoint in Java 8 with examples
- Double the numbers of specified ArrayList using Streams
- Double the even / odd numbers of a specified ArrayList using Streams
- How to check if Number is Prime or not using Streams
- Retrieve Even/Odd Numbers within the Range using Java 8 Streams
- How to add/sum up the ArrayList integers using Java8 Streams
- Generate Prime Numbers in Java 8 using Streams
- Comparator example – Collections sort with/without Lambda in Java 8
- How to pass function as a parameter in a method in Java 8?
- Remove duplicates from ArrayList in Java 8
- ForEach examples for Map/List in Java 8

**Other Useful References:**