write a program to enter any 2 numbers and print their all common factors
Answers
Answer:
Given two integer numbers, the task is to find count of all common divisors of given numbers?
Examples :
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
Recommended: Please solve it on “PRACTICE ” first, before moving on to the solution.
It is recommended to refer all divisors of a given number as a prerequisite of this article.
A simple solution is to first find all divisors of first number and store them in an array or hash. Then find common divisors of second number and store them. Finally print common elements of two stored arrays or hash.
A better solution is to calculate the greatest common divisor (gcd) of given two
Explanation: