
- 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 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 Articles
- Count n digit numbers divisible by given number in C++
- N digit numbers divisible by 5 formed from the M digits in C++
- If $n$ is an odd integer, then show that $n^2 - 1$ is divisible by $8$.
- Program to count n digit integers where digits are strictly increasing in Python
- For any positive integer n, prove that $n^3-n$ is divisible by 6.
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Find a non empty subset in an array of N integers such that sum of elements of subset is divisible by N in C++
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers - JavaScript
- $n^2 - 1$ is divisible by 8, if $n$ is(A) an integer(B) a natural number(C) an odd integer(D) an even integer
- Largest N digit number divisible by given three numbers in C++
- Find the sum of the integers between 100 and 200 that are divisible by 9
- How many numbers of two digit are divisible by 3?
- It is given that\[63.63=m\left(21+\frac{n}{100}\right)\]Where \( m, n \) are positive integers and \( n
- Find the sum of the integers between 100 and 200 that are not divisible by 9
- How many two digit numbers are divisible by $3?$
