You are given a range of numbers, from lower number to higher number (both inclusive). Consider all the factors of these numbers except 1, the task is to find the factor which appears the maximum number of times. If more than one number have maximum frequency than print anyone of them.

**Examples:**

Input :lower=10 higher=14Output :The factor with maximum frequency is 2.Input :lower=11 higher=11Output :The factor with maximum frequency is 11.

**Explanation: **

If we have a range of numbers, say 10 to 14. Then the factors other than 1 will be:

In the above case, frequency(2)= 3, which is maximum on comparing with other factors.

**Approach:**

1. If lower = higher, then all factors will have frequency 1. So we can print any number which is factor, since every number is a factor of itself so we print the same number.

2. Otherwise, we print ‘2’ as it will always be true for a range of numbers.

## C++

`// CPP program to find most common factor ` `// in a range. ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `mostCommon(` `int` `lower, ` `int` `higher) ` `{ ` ` ` `// Check whether lower number ` ` ` `// and higher number are same ` ` ` `if` `(lower == higher) ` ` ` `return` `lower; ` ` ` `else` ` ` `return` `2; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `int` `lower = 10; ` `// Lower number ` ` ` `int` `higher = 20; ` `// Higher number ` ` ` ` ` `printf` `(` `"The most frequent factor %d\n"` `, ` ` ` `mostCommon(lower, higher)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to find most common factor ` `// in a range. ` ` ` `public` `class` `GfG { ` ` ` ` ` `public` `static` `int` `mostCommon(` `int` `lower, ` `int` `higher) ` ` ` `{ ` ` ` `// Check whether lower number ` ` ` `// and higher number are same ` ` ` `if` `(lower == higher) ` ` ` `return` `lower; ` ` ` `else` ` ` `return` `2` `; ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `main(String []args) { ` ` ` ` ` `int` `lower = ` `10` `; ` `// Lower number ` ` ` `int` `higher = ` `20` `; ` `// Higher number ` ` ` `System.out.println(` `"The most frequent factor "` `+ ` ` ` `mostCommon(lower, higher)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Python 3

`# Python 3 program to find most ` `# common factor in a range. ` `def` `mostCommon( lower, higher): ` ` ` ` ` `# Check whether lower number ` ` ` `# and higher number are same ` ` ` `if` `(lower ` `=` `=` `higher): ` ` ` `return` `lower ` ` ` `else` `: ` ` ` `return` `2` ` ` `# Driver Code ` `lower ` `=` `10` `# Lower number ` `higher ` `=` `20` `# Higher number ` ` ` `print` `(` `"The most frequent factor"` `, ` ` ` `mostCommon(lower, higher)) ` ` ` `# This code is contributed by ash264 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to find most common factor ` `// in a range. ` `using` `System; ` ` ` `class` `GFG ` `{ ` `static` `int` `mostCommon(` `int` `lower, ` `int` `higher) ` `{ ` ` ` `// Check whether lower number ` ` ` `// and higher number are same ` ` ` `if` `(lower == higher) ` ` ` `return` `lower; ` ` ` `else` ` ` `return` `2; ` `} ` ` ` `// Driver Code ` `public` `static` `void` `Main() ` `{ ` ` ` `int` `lower = 10; ` `// Lower number ` ` ` `int` `higher = 20; ` `// Higher number ` ` ` ` ` `Console.WriteLine(` `"The most frequent factor "` `+ ` ` ` `mostCommon(lower, higher)); ` `} ` `} ` ` ` `// This code is contributed ` `// by Akanksha Rai ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to find most common ` `// factor in a range. ` ` ` `function` `mostCommon(` `$lower` `, ` `$higher` `) ` `{ ` ` ` `// Check whether lower number ` ` ` `// and higher number are same ` ` ` `if` `(` `$lower` `== ` `$higher` `) ` ` ` `return` `$lower` `; ` ` ` `else` ` ` `return` `2; ` `} ` ` ` `// Driver Code ` `$lower` `= 10; ` `// Lower number ` `$higher` `= 20; ` `// Higher number ` ` ` `echo` `"The most frequent factor "` `. ` ` ` `mostCommon(` `$lower` `, ` `$higher` `) . ` `"\n"` `; ` ` ` `// This code is contributed ` `// by Akanksha Rai ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

The most frequent factor 2

**Time Complexity: **O(1)

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:

- Find the most frequent digit without using array/string
- Most frequent element in a 2D array
- Queries on the sum of prime factor counts in a range
- Count all the numbers in a range with smallest factor as K
- Generate a pair of integers from a range [L, R] whose LCM also lies within the range
- Minimum possible value T such that at most D Partitions of the Array having at most sum T is possible
- Median in a stream of integers (running integers)
- Mode in a stream of integers (running integers)
- Lexicographically smallest permutation of size A having B integers exceeding all preceeding integers
- Exactly n distinct prime factor numbers from a to b
- Print the kth common factor of two numbers
- k-th prime factor of a given number
- Nearest element with at-least one common prime factor
- Smallest number S such that N is a factor of S factorial or S!
- Find largest prime factor of a number
- Sum of largest prime factor of each number less than equal to n
- N-th prime factor of a given number
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Count all the numbers less than 10^6 whose minimum prime factor is N
- Largest factor of a given number which is a perfect square

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.