Given two numbers m and n, count number of m digit numbers that are divisible by n.

Examples:

Input : m = 2 n = 6 Output : 15 Two digit numbers that are divisible by 6 are 12, 18, 24, 30, 36, ....., 96. Input : m = 3 n = 5 Output :180

A **simple solution** is two try all m digit numbers. For every number, check if it is divisible by n. If yes, we increment count.

An **efficient solution** involves following steps.

The idea is based on the fact that starting from first divisible number, every n-th number is divisible by n.

- Find largest m digit number.
- Find largest m-1 digit number.
- Divide both number by n and subtract later from prior.

Below is the implementation of above steps.

## C++

`// C++ program to count m digit numbers having ` `// n as divisor. ` `#include<bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Returns count of m digit numbers having n ` `// as divisor ` `int` `findCount(` `int` `m, ` `int` `n) ` `{ ` ` ` `// generating largest number of m digit ` ` ` `int` `num1 = 0; ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `num1 = (num1 * 10) + 9; ` ` ` ` ` `// generating largest number of m-1 digit ` ` ` `int` `num2 = 0; ` ` ` `for` `(` `int` `i = 0; i < (m - 1); i++) ` ` ` `num2 = (num2 * 10) + 9; ` ` ` ` ` `// returning number of dividend ` ` ` `return` `((num1 / n) - (num2 / n)); ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `int` `m = 2, n = 6; ` ` ` `printf` `(` `"%d\n"` `, findCount(m, n)); ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java program to count m digit numbers having ` `// n as divisor. ` ` ` `class` `Main ` `{ ` ` ` `// Returns count of m digit numbers having n ` ` ` `// as divisor ` ` ` `static` `int` `findCount(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` `// generating largest number of m digit ` ` ` `int` `num1 = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < m; i++) ` ` ` `num1 = (num1 * ` `10` `) + ` `9` `; ` ` ` ` ` `// generating largest number of m-1 digit ` ` ` `int` `num2 = ` `0` `; ` ` ` `for` `(` `int` `i = ` `0` `; i < (m - ` `1` `); i++) ` ` ` `num2 = (num2 * ` `10` `) + ` `9` `; ` ` ` ` ` `// returning number of dividend ` ` ` `return` `((num1 / n) - (num2 / n)); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `main (String[] args) ` ` ` `{ ` ` ` `int` `m = ` `2` `, n = ` `6` `; ` ` ` `System.out.println(findCount(m, n)); ` ` ` `} ` `} ` ` ` `/* This code is contributed by Harsh Agarwal */` |

*chevron_right*

*filter_none*

## Python3

`# Python3 program to count m digit ` `# numbers having n as divisor. ` ` ` `# Returns count of m digit ` `# numbers having n as divisor ` `def` `findCount(m, n): ` ` ` ` ` `# Generating largest number of m digit ` ` ` `num1 ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, m): ` ` ` `num1 ` `=` `(num1 ` `*` `10` `) ` `+` `9` ` ` ` ` `# Generating largest number of m-1 digit ` ` ` `num2 ` `=` `0` ` ` ` ` `for` `i ` `in` `range` `(` `0` `, (m ` `-` `1` `)): ` ` ` `num2 ` `=` `(num2 ` `*` `10` `) ` `+` `9` ` ` ` ` `# returning number of dividend ` ` ` `return` `int` `((num1 ` `/` `n) ` `-` `(num2 ` `/` `n)) ` ` ` ` ` `# Driver code ` `m ` `=` `2` `; n ` `=` `6` `print` `(findCount(m, n)) ` ` ` `# This code is contributed by Smitha Dinesh Semwal ` |

*chevron_right*

*filter_none*

## C#

`// C# program to count m digit numbers ` `// having n as divisor. ` `using` `System; ` ` ` `class` `GfG { ` ` ` ` ` `// Returns count of m digit numbers ` ` ` `// having n as divisor ` ` ` `static` `int` `findCount(` `int` `m, ` `int` `n) ` ` ` `{ ` ` ` ` ` `// generating largest number ` ` ` `// of m digit ` ` ` `int` `num1 = 0; ` ` ` `for` `(` `int` `i = 0; i < m; i++) ` ` ` `num1 = (num1 * 10) + 9; ` ` ` ` ` `// generating largest number ` ` ` `// of m-1 digit ` ` ` `int` `num2 = 0; ` ` ` `for` `(` `int` `i = 0; i < (m - 1); i++) ` ` ` `num2 = (num2 * 10) + 9; ` ` ` ` ` `// returning number of dividend ` ` ` `return` `((num1 / n) - (num2 / n)); ` ` ` `} ` ` ` ` ` `// main function ` ` ` `public` `static` `void` `Main () ` ` ` `{ ` ` ` `int` `m = 2, n = 6; ` ` ` ` ` `Console.Write(findCount(m, n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by parashar. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP program to count m digit ` `// numbers having n as divisor. ` ` ` `// Returns count of m digit numbers ` `// having n as divisor ` `function` `findCount(` `$m` `, ` `$n` `) ` `{ ` ` ` `// generating largest number ` ` ` `// of m digit ` ` ` `$num1` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< ` `$m` `; ` `$i` `++) ` ` ` `$num1` `= (` `$num1` `* 10) + 9; ` ` ` ` ` `// generating largest number ` ` ` `// of m-1 digit ` ` ` `$num2` `= 0; ` ` ` `for` `(` `$i` `= 0; ` `$i` `< (` `$m` `- 1); ` `$i` `++) ` ` ` `$num2` `= (` `$num2` `* 10) + 9; ` ` ` ` ` `// returning number of dividend ` ` ` `return` `((` `$num1` `/ ` `$n` `) - (` `$num2` `/ ` `$n` `)); ` `} ` ` ` `// Driver code ` `$m` `= 2; ` `$n` `= 6; ` `echo` `findCount(` `$m` `, ` `$n` `), ` `"\n"` `; ` ` ` `// This code is contributed by ajit ` `?> ` |

*chevron_right*

*filter_none*

Output :

15

Time complexity : O(m)

This article is contributed by Aditya Kumar. 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.

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:

- Count 'd' digit positive integers with 0 as a digit
- Count of N-digit numbers having digit XOR as single digit
- Count numbers in a range with digit sum divisible by K having first and last digit different
- Minimum decrements to make integer A divisible by integer B
- Sum of last digit of all integers from 1 to N divisible by M
- Count positive integers with 0 as a digit and maximum 'd' digits
- Count integers in a range which are divisible by their euler totient value
- Count integers in the range [A, B] that are not divisible by C and D
- Count n digit numbers not having a particular digit
- Count of Numbers in Range where first digit is equal to last digit of the number
- Count of pairs (A, B) in range 1 to N such that last digit of A is equal to the first digit of B
- Count n digit numbers divisible by given number
- Count of numbers in range which are divisible by M and have digit D at odd places
- Count of N digit palindromic numbers divisible by 9
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Count the number of pairs (i, j) such that either arr[i] is divisible by arr[j] or arr[j] is divisible by arr[i]
- 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
- Digital Root (repeated digital sum) of square of an integer using Digital root of the given integer