C++ Program for Common Divisors of Two Numbers Last Updated : 04 Dec, 2018 Comments Improve Suggest changes Like Article Like Report 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 CPP // 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; } Output: 6 Please refer complete article on Common Divisors of Two Numbers for more details! Comment More infoAdvertise with us Next Article C++ Program for Common Divisors of Two Numbers K kartik Follow Improve Article Tags : Mathematical C++ Programs DSA GCD-LCM divisors +1 More Practice Tags : Mathematical Similar Reads Count common prime factors of two numbers Given two integer A and B , the task is to find the count of common factors of two numbers where factors are prime.Examples: Input: A = 6, B = 12 Output: 2 2 and 3 are the only common prime divisors of 6 and 12Input: A = 4, B = 8 Output: 1 Naive Approach: Iterate from 1 to min(A, B) and check whethe 10 min read C++ Program To Find LCM of Two Numbers LCM (Least Common Multiple) of two numbers is the smallest number that is divisible by both numbers. For example, the LCM of 15 and 20 is 60, and the LCM of 15 and 25 is 75. In this article, we will learn to write a C++ program to find the LCM of two numbers. We can find the LCM of two numbers in C+ 4 min read Divide the two given numbers by their common divisors Given two numbers A and B, the task is to Divide the two numbers A and B by their common divisors. The numbers A and B is less than 10^8.Examples: Input: A = 10, B = 15 Output: A = 2, B = 3 The common factors are 1, 5 Input: A = 100, B = 150 Output: A = 2, B = 3 Naive Approach: Iterate from i = 1 to 10 min read C++ Program To Find All Factors of A Natural Number Given a natural number n, print all distinct divisors of it. Examples: Input : n = 10 Output: 1 2 5 10 Input: n = 100 Output: 1 2 4 5 10 20 25 50 100 Input: n = 125 Output: 1 5 25 125 Note that this problem is different from finding all prime factors. Recommended PracticeCount Numbers in RangeTry It 3 min read C++ Program for GCD of more than two (or array) numbers The GCD of three or more numbers equals the product of the prime factors common to all the numbers, but it can also be calculated by repeatedly taking the GCDs of pairs of numbers. gcd(a, b, c) = gcd(a, gcd(b, c)) = gcd(gcd(a, b), c) = gcd(gcd(a, c), b) CPP // C++ program to find GCD of two or // mo 3 min read Like