Common divisors of N numbers | C # program to find all common divisors of N numbers ; Function to calculate gcd of two numbers ; Function to print all the common divisors ; Variable to find the gcd of N numbers ; Set to store all the common divisors ; Finding GCD of the given N numbers ; Finding divisors of the HCF of n numbers ; Print all the divisors ; Driver 's Code ; Function to print all the common divisors
using System ; using System . Collections . Generic ; class GFG { static int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } static void printAllDivisors ( int [ ] arr , int N ) { int g = arr [ 0 ] ; HashSet < int > divisors = new HashSet < int > ( ) ; for ( int i = 1 ; i < N ; i ++ ) { g = gcd ( arr [ i ] , g ) ; } for ( int i = 1 ; i * i <= g ; i ++ ) { if ( g % i == 0 ) { divisors . Add ( i ) ; if ( g / i != i ) divisors . Add ( g / i ) ; } } foreach ( int it in divisors ) Console . Write ( it + " ▁ " ) ; } public static void Main ( String [ ] args ) { int [ ] arr = { 6 , 90 , 12 , 18 , 30 , 24 } ; int n = arr . Length ; printAllDivisors ( arr , n ) ; } }