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
#include <bits/stdc++.h> NEW_LINE using namespace std ; int gcd ( int a , int b ) { if ( a == 0 ) return b ; return gcd ( b % a , a ) ; } void printAllDivisors ( int arr [ ] , int N ) { int g = arr [ 0 ] ; set < int > divisors ; 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 . insert ( i ) ; if ( g / i != i ) divisors . insert ( g / i ) ; } } for ( auto & it : divisors ) cout << it << " ▁ " ; } int main ( ) { int arr [ ] = { 6 , 90 , 12 , 18 , 30 , 24 } ; int n = sizeof ( arr ) / sizeof ( arr [ 0 ] ) ; printAllDivisors ( arr , n ) ; return 0 ; }