

- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Count of m digit integers that are divisible by an integer n in C++
We are given two integers m and n. The goal is to count m digit numbers that are divisible by n.
If m=1, then numbers are 0,1,2,3,4,5,6,7,8,9 and n=3 then numbers divisible by 3=0,3,6,9 count=4.
Let’s understand with examples.
Input − m=2, n=9
Output − Count of m digit numbers divisible by n − 10
Explanation − between 10 and 99 numbers divisible by 9 are −
18, 27, 36, 45, 54, 63, 72, 81, 90, 99
Input m=3, n=300
Output − Count of m digit numbers divisible by n: 3
Explanation − between 100 and 999 numbers divisible by 300 are −
300, 600, 900
Approach used in the below program is as follows
We take integers m and n.
Calculate largest m-1 digit number as num1
Calculate largest m digit number as num2
Function findCount(int n, int L, int R) takes n and range ( between num1 and num2 ) as input and returns all numbers in that range which are divisible by n.
Take the initial count as 0.
Starting from i=L to i=R. If i%n==0, increment count.
Return count as result.
Example
#include<bits/stdc++.h> using namespace std; // Returns count of m digit numbers having n // as divisor int findCount(int n, int L, int R){ int count=0; int i; for(int i=L;i<=R;i++){ if(i%n==0) { count++; } } return count; } int main(){ int M = 2, N = 9; int i; int num1 = 0; //largest m-1 digit no. for (i = 0; i < (M - 1); i++) num1 = (num1 * 10) + 9; int num2 = 0; //largest m digit no. for (i = 0; i < M; i++) num2 = (num2 * 10) + 9; cout<<"Count of M digit no.s divisible by N:"<<findCount(N,num1+1,num2); return 0; }
Output
If we run the above code it will generate the following output−
Count of M digit no.s divisible by N:11
- Related Questions & Answers
- Count n digit numbers divisible by given number in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Program to count n digit integers where digits are strictly increasing in Python
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Print all the combinations of N elements by changing sign such that their sum is divisible by M in C++
- Largest N digit number divisible by given three numbers in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- Find the number closest to n and divisible by m in C++
- Count of numbers satisfying m + sum(m) + sum(sum(m)) = N in C++
- Count divisors of n that have at-least one digit common with n in Java
- Find elements of an array which are divisible by N using STL in C++
- Count numbers in a range that are divisible by all array elements in C++
- Count numbers in range that are divisible by all of its non-zero digits in C++
- Is the digit divisible by the previous digit of the number in JavaScript