- Related Questions & Answers
- 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
- Count of m digit integers that are divisible by an integer n in C++
- Largest K digit number divisible by X in C++
- Count n digit numbers not having a particular digit in C++
- Count all possible N digit numbers that satisfy the given condition in C++
- Numbers At Most N Given Digit Set in C++
- Divide number into two parts divisible by given numbers in C++ Program
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Smallest number that is divisible by first n numbers in JavaScript
- C++ Program for Largest K digit number divisible by X?
- C++ Program for Smallest K digit number divisible by X?
- C++ Programming for Smallest K digit number divisible by X?
- Count of Binary Digit numbers smaller than N in C++

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

We are given the two elements let’s say, d and num, the task is to find the d digit numbers which are divisible by num.

In simple words let us suppose we have given an input 2 in d, so we will first find all the 2-digit numbers i.e. from 10-99 and then find all the numbers which are divisible by num.

Let us understand more of this with the help of examples −

**Input** − digit = 2, num= 12

**Output** − Count of n digit numbers divisible by given number: 8

**Explanation** − The 2-digit numbers divisible by 12 are 12, 24, 36, 48, 60, 72, 84 and 96, so there are 8 2 digit numbers divisible by 12.

**Input** − digit = 2, num= 9

**Output** − Count of n digit numbers divisible by given number − 10

**Explanation** − The 2 digit numbers divisible by 9 are 18, 27, 36, 45, 54, 63, 72, 81, 90 and 99 so there are 10 two digit numbers divisible by 9.

Take element digit and num as inputs.

Assign a variable count as 0 to count the number of digits divisible by num.

Declare and set digi_first as pow(10, digit - 1)

Declare and set digi_last to pow(10, digit)

Now declare and set d_first as digi_first % num and d_last as digi_last % num

After finding d_first and d_last, set digi_first as (digi_first - d_first) + num and digi_last as digi_last - d_last

Now set count to ((digi_last - digi_first) / num + 1).

Return and print count.

#include <cmath> #include <iostream> using namespace std; int main(){ int digit = 2 , num = 9; //store the count int count= 0 ; int digi_first = pow(10, digit - 1); int digi_last = pow(10, digit); int d_first = digi_first % num; int d_last = digi_last % num; digi_first = (digi_first - d_first) + num; digi_last = digi_last - d_last; count = ((digi_last - digi_first) / num + 1); cout<<"Count of n digit numbers divisible by given number: "<<count<<"\n"; return 0; }

If we run the above code, we will get the following output −

Count of n digit numbers divisible by given number: 10

Advertisements