In this post we will discuss hot to find GCD of n numbers i.e an Array in Java.


Greatest Common Divisor (GCD) of two or more positive numbers is the largest of them which divides all of them. For example, gcd of [16, 8, 24] is 8.

The trick to calculate the GCD of multiple numbers is to use the gcd of two numbers with the third one.

For example, first, we will calculate the GCD of the first two numbers of the array (say x), then we will calculate the gcd of the x and the third number (say y) then again we will compute gcd of y and the fourth number.

This process will repeat until the last number. The final GCD which we get will be the answer.

To implement the same we will have to loop through the array. Let’s do it.


If you have any suggestions or doubts then comment below.

Leave a Reply