Given two integer numbers, the task is to find the count of all common divisors of given numbers.

Input : a = 12, b = 24 Output: 6 // all common divisors are 1, 2, 3, // 4, 6 and 12 Input : a = 3, b = 17 Output: 1 // all common divisors are 1 Input : a = 20, b = 36 Output: 3 // all common divisors are 1, 2, 4

`// C++ implementation of program ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to calculate gcd of two numbers ` `int` `gcd(` `int` `a, ` `int` `b) ` `{ ` ` ` `if` `(a == 0) ` ` ` `return` `b; ` ` ` `return` `gcd(b % a, a); ` `} ` ` ` `// Function to calculate all common divisors ` `// of two given numbers ` `// a, b --> input integer numbers ` `int` `commDiv(` `int` `a, ` `int` `b) ` `{ ` ` ` `// find gcd of a, b ` ` ` `int` `n = gcd(a, b); ` ` ` ` ` `// Count divisors of n. ` ` ` `int` `result = 0; ` ` ` `for` `(` `int` `i = 1; i <= ` `sqrt` `(n); i++) { ` ` ` `// if 'i' is factor of n ` ` ` `if` `(n % i == 0) { ` ` ` `// check if divisors are equal ` ` ` `if` `(n / i == i) ` ` ` `result += 1; ` ` ` `else` ` ` `result += 2; ` ` ` `} ` ` ` `} ` ` ` `return` `result; ` `} ` ` ` `// Driver program to run the case ` `int` `main() ` `{ ` ` ` `int` `a = 12, b = 24; ` ` ` `cout << commDiv(a, b); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

**Output:**

6

Please refer complete article on Common Divisors of Two Numbers for more details!

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Java Program for Common Divisors of Two Numbers
- Divide the two given numbers by their common divisors
- Sum of common divisors of two numbers A and B
- Maximum count of pairwise co-prime and common divisors of two given numbers
- Common Divisors of Two Numbers
- Maximum possible prime divisors that can exist in numbers having exactly N divisors
- Common divisors of N numbers
- Find sum of divisors of all the divisors of a natural number
- Divisors of n-square that are not divisors of n
- Find sum of inverse of the divisors when sum of divisors and the number is given
- Check if count of even divisors of N is equal to count of odd divisors
- Check if a number has an odd count of odd divisors and even count of even divisors
- Minimum value exceeding X whose count of divisors has different parity with count of divisors of X
- Number of triangles formed by joining vertices of n-sided polygon with two common sides and no common sides
- Count divisors of n that have at-least one digit common with n
- Maximum count of common divisors of A and B such that all are co-primes to one another
- Check if sum of divisors of two numbers are same
- Find two numbers whose divisors are given in a random order
- Program to find count of numbers having odd number of divisors in given range
- Print the kth common factor of two numbers