
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
Count n digit numbers divisible by given number in C++
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.
Approach used in the below program as follows
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.
Example
#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; }
Output
If we run the above code, we will get the following output −
Count of n digit numbers divisible by given number: 10
- Related Articles
- 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
- N digit numbers divisible by 5 formed from the M digits in C++
- Count of m digit integers that are divisible by an integer n in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Count all possible N digit numbers that satisfy the given condition in C++
- Count n digit numbers not having a particular digit in C++
- Smallest number that is divisible by first n numbers in JavaScript
- Count numbers (smaller than or equal to N) with given digit sum in C++
- Largest K digit number divisible by X in C++
- How many two digit numbers are divisible by $3?$
- How many three digit numbers are divisible by 7?
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Find the number of all three digit natural numbers which are divisible by 9.
