Common Divisors of Two Numbers | Java implementation of program ; method to calculate gcd of two numbers ; method to calculate all common divisors of two given numbers a , b -- > input integer numbers ; find gcd of a , b ; Count divisors of n . ; if ' i ' is factor of n ; check if divisors are equal ; Driver method
class Test { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static int commDiv ( int a , int b ) { int n = gcd ( a , b ) ; int result = 0 ; for ( int i = 1 ; i <= Math . sqrt ( n ) ; i ++ ) { if ( n % i == 0 ) { if ( n / i == i ) result += 1 ; else result += 2 ; } } return result ; } public static void main ( String args [ ] ) { int a = 12 , b = 24 ; System . out . println ( commDiv ( a , b ) ) ; } }