# Count the numbers divisible by ‘M’ in a given range

A and B are two numbers which define a range, where A <= B. Find the total numbers in the given range [A … B] divisible by 'M'

Examples:

Input : A = 25, B = 100, M = 30 Output : 3 Explanation : In the given range [25 - 100], 30, 60 and 90 are divisible by 30 Input : A = 6, B = 15, M = 3 Output : 4 Explanation : In the given range [6 - 15], 6, 9, 12 and 15 are divisible by 3

**Method 1 : [Brute-force]**

Run a loop from A to B. If a number divisible by ‘M’ is found, increment counter.

Below is the implementation of above method:

## C/C++

`// Program to count the numbers divisible by ` `// M in a given range ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `int` `countDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Variable to store the counter ` ` ` `int` `counter = 0; ` ` ` ` ` `// Running a loop from A to B and check ` ` ` `// if a number is divisible by M. ` ` ` `for` `(` `int` `i = A; i <= B; i++) ` ` ` `if` `(i % M == 0) ` ` ` `counter++; ` ` ` ` ` `return` `counter; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `// A and B define the range, M is the dividend ` ` ` `int` `A = 30, B = 100, M = 30; ` ` ` ` ` `// Printing the result ` ` ` `cout << countDivisibles(A, B, M) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the numbers divisible by ` `// M in a given range ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to count the numbers divisible by ` ` ` `// M in a given range ` ` ` `static` `int` `countDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` ` ` `{ ` ` ` `// Variable to store the counter ` ` ` `int` `counter = ` `0` `; ` ` ` ` ` `// Running a loop from A to B and check ` ` ` `// if a number is divisible by M. ` ` ` `for` `(` `int` `i = A; i <= B; i++) ` ` ` `if` `(i % M == ` `0` `) ` ` ` `counter++; ` ` ` ` ` `return` `counter; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// A and B define the range, M is the dividend ` ` ` `int` `A = ` `30` `, B = ` `100` `, M = ` `30` `; ` ` ` ` ` `// Printing the result ` ` ` `System.out.println(countDivisibles(A, B, M)); ` ` ` `} ` `} ` ` ` `// Contributed by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Program to count the numbers ` `# divisible by M in a given range ` ` ` `def` `countDivisibles(A, B, M): ` ` ` ` ` `# Variable to store the counter ` ` ` `counter ` `=` `0` `; ` ` ` ` ` `# Running a loop from A to B ` ` ` `# and check if a number is ` ` ` `# divisible by M. ` ` ` `for` `i ` `in` `range` `(A, B): ` ` ` `if` `(i ` `%` `M ` `=` `=` `0` `): ` ` ` `counter ` `=` `counter ` `+` `1` ` ` ` ` `return` `counter ` ` ` `# Driver code ` `# A and B define the range, ` `# M is the dividend ` `A ` `=` `30` `B ` `=` `100` `M ` `=` `30` ` ` `# Printing the result ` `print` `(countDivisibles(A, B, M)) ` ` ` `# This code is contributed by Sam007. ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the numbers ` `// divisible by M in a given range ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to count the numbers divisible by ` ` ` `// M in a given range ` ` ` `static` `int` `countDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` ` ` `{ ` ` ` `// Variable to store the counter ` ` ` `int` `counter = 0; ` ` ` ` ` `// Running a loop from A to B and check ` ` ` `// if a number is divisible by M. ` ` ` `for` `(` `int` `i = A; i <= B; i++) ` ` ` `if` `(i % M == 0) ` ` ` `counter++; ` ` ` ` ` `return` `counter; ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// A and B define the range, M is the dividend ` ` ` `int` `A = 30, B = 100, M = 30; ` ` ` ` ` `// Printing the result ` ` ` `Console.WriteLine(countDivisibles(A, B, M)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to count the ` `// numbers divisible by ` `// M in a given range ` ` ` `function` `countDivisibles(` `$A` `, ` `$B` `, ` `$M` `) ` `{ ` ` ` ` ` `// Variable to store the counter ` ` ` `$counter` `= 0; ` ` ` ` ` `// Running a loop from ` ` ` `// A to B and check ` ` ` `// if a number is ` ` ` `// divisible by M. ` ` ` `for` `(` `$i` `= ` `$A` `; ` `$i` `<= ` `$B` `; ` `$i` `++) ` ` ` `if` `(` `$i` `% ` `$M` `== 0) ` ` ` `$counter` `++; ` ` ` ` ` `return` `$counter` `; ` `} ` ` ` ` ` `// Driver Code ` ` ` `// A and B define the range, ` ` ` `// M is the dividend ` ` ` `$A` `= 30; ` ` ` `$B` `= 100; ` ` ` `$M` `= 30; ` ` ` ` ` `// Printing the result ` ` ` `echo` `countDivisibles(` `$A` `, ` `$B` `, ` `$M` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

Output:

3

**Method 2 : [Better]**

The loop can be modified by incrementing the iterator ‘M’ times after the first divisible is found. Also, if ‘A’ is less than ‘M’, it can be changed to ‘M’, because a number less than ‘M’ can not be divided by it.

**Method 3 : [Efficient]**

Let B = b * M and A = a * M The count of numbers divisible by 'M' between A and B will be equal to b - a. Example: A = 25, B = 70, M = 10. Now, a = 2, b = 7. Count = 7 - 2 = 5.

It can be observed that, if A is divisible by M, ‘b – a’ will exclude the count for A, so the count will be less by 1. Thus, in this case we add 1 explicitly.

**Example when A is divisible by M:**

A = 30, B = 70, M = 10. Now, a = 3, b = 7. Count = 7 - 3 = 4. But, Count should be 5. Thus, we will add 1 explicitly.

Below is the implementation of the above method :

## C/C++

`// Program to count the numbers divisible ` `// by M in a given range ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns count of numbers in [A B] that ` `// are divisible by M. ` `int` `countDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` `{ ` ` ` `// Add 1 explicitly as A is divisible by M ` ` ` `if` `(A % M == 0) ` ` ` `return` `(B / M) - (A / M) + 1; ` ` ` ` ` `// A is not divisible by M ` ` ` `return` `(B / M) - (A / M); ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `// A and B define the range, M is the divident ` ` ` `int` `A = 30, B = 70, M = 10; ` ` ` ` ` `// Printing the result ` ` ` `cout << countDivisibles(A, B, M) << endl; ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count the numbers divisible by ` `// M in a given range ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` `// Function to count the numbers divisible by ` ` ` `// M in a given range ` ` ` `static` `int` `countDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` ` ` `{ ` ` ` `// Add 1 explicitly as A is divisible by M ` ` ` `if` `(A % M == ` `0` `) ` ` ` `return` `(B / M) - (A / M) + ` `1` `; ` ` ` ` ` `// A is not divisible by M ` ` ` `return` `(B / M) - (A / M); ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `// A and B define the range, M is the dividend ` ` ` `int` `A = ` `30` `, B = ` `100` `, M = ` `30` `; ` ` ` ` ` `// Printing the result ` ` ` `System.out.println(countDivisibles(A, B, M)); ` ` ` `} ` `} ` ` ` `// Contirbuted by Pramod Kumar ` |

*chevron_right*

*filter_none*

## Python3

`# Program to count the numbers divisible ` `# by M in a given range ` ` ` `# Returns count of numbers in [A B] that ` `# are divisible by M. ` `def` `countDivisibles(A, B, M): ` ` ` ` ` `# Add 1 explicitly as A is divisible by M ` ` ` `if` `(A ` `%` `M ` `=` `=` `0` `): ` ` ` `return` `((B ` `/` `M) ` `-` `(A ` `/` `M)) ` `+` `1` ` ` ` ` `# A is not divisible by M ` ` ` `return` `((B ` `/` `M) ` `-` `(A ` `/` `M)) ` ` ` `# Driver Code ` `# A and B define the range, M ` `# is the divident ` `A ` `=` `30` `B ` `=` `70` `M ` `=` `10` ` ` `# Printing the result ` `print` `(countDivisibles(A, B, M)) ` ` ` `# This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count the numbers ` `// divisible by M in a given range ` `using` `System; ` ` ` `public` `class` `GFG { ` ` ` ` ` `// Function to count the numbers divisible by ` ` ` `// M in a given range ` ` ` `static` `int` `countDivisibles(` `int` `A, ` `int` `B, ` `int` `M) ` ` ` `{ ` ` ` `// Add 1 explicitly as A is divisible by M ` ` ` `if` `(A % M == 0) ` ` ` `return` `(B / M) - (A / M) + 1; ` ` ` ` ` `// A is not divisible by M ` ` ` `return` `(B / M) - (A / M); ` ` ` `} ` ` ` ` ` `// driver program ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `// A and B define the range, M is the dividend ` ` ` `int` `A = 30, B = 100, M = 30; ` ` ` ` ` `// Printing the result ` ` ` `Console.WriteLine(countDivisibles(A, B, M)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Sam007 ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP Program to count the numbers ` `// divisible by M in a given range ` ` ` `// Returns count of numbers in ` `// [A B] that are divisible by M. ` `function` `countDivisibles(` `$A` `, ` `$B` `, ` `$M` `) ` `{ ` ` ` ` ` `// Add 1 explicitly as A ` ` ` `// is divisible by M ` ` ` `if` `(` `$A` `% ` `$M` `== 0) ` ` ` `return` `(` `$B` `/ ` `$M` `) - ` ` ` `(` `$A` `/ ` `$M` `) + 1; ` ` ` ` ` `// A is not divisible by M ` ` ` `return` `(` `$B` `/ ` `$M` `) - ` ` ` `(` `$A` `/ ` `$M` `); ` `} ` ` ` ` ` `// Driver Code ` ` ` `// A and B define the range, ` ` ` `// M is the divident ` ` ` `$A` `= 30; ` ` ` `$B` `= 70; ` ` ` `$M` `= 10; ` ` ` ` ` `// Printing the result ` ` ` `echo` `countDivisibles(` `$A` `, ` `$B` `, ` `$M` `) ; ` ` ` `return` `0; ` ` ` `// This code is contributed by nitin mittal. ` `?> ` |

*chevron_right*

*filter_none*

Output:

5

This article is contributed by **Rohit Thapliyal**. 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 write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

## Recommended Posts:

- Count numbers in range 1 to N which are divisible by X but not by Y
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Sum of all numbers divisible by 6 in a given range
- Numbers that are not divisible by any number in the range [2, 10]
- Count integers in a range which are divisible by their euler totient value
- Count natural numbers whose factorials are divisible by x but not y
- Count pairs of numbers from 1 to N with Product divisible by their Sum
- Count n digit numbers divisible by given number
- Count numbers which are divisible by all the numbers from 2 to 10
- Count Odd and Even numbers in a range from L to R
- Count of numbers having only 1 set bit in the range [0, n]
- Count of numbers from range [L, R] whose sum of digits is Y
- Count factorial numbers in a given range