Given a range l – r (inclusive), count the numbers that are divisible by all of its non-zero digits.
Examples:
Input : 1 9 Output : 9 Explanation: all the numbers are divisible by their digits in the range 1-9. Input : 10 20 Output : 5 Explanation: 10, 11, 12, 15, 20
Approach:
1. Run a loop to generate every number from l and r.
2. Check if every non-zero digit of that number divides the number or not.
3. Keep a count of all numbers that are completely divisible by its digits.
4. Print the count of numbers.
Below is the implementation of the above approach:
C++
// C++ program to // Count numbers in // range L-R that are // divisible by // all of its non-zero // digits #include <bits/stdc++.h> using namespace std; // check if the number is // divisible by the digits. bool check(int n) { int m = n; while (n) { int r = n % 10; if (r > 0) if ((m % r) != 0) return false; n /= 10; } return true; } // function to calculate the // number of numbers int count(int l, int r) { int ans = 0; for (int i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function int main() { int l = 10, r = 20; cout << count(l, r); return 0; } |
Java
// Java program to Count // numbers in range L-R // that are divisible by // all of its non-zero // digits import java.io.*; class GFG { // check if the number // is divisible by the // digits. static boolean check(int n) { int m = n; while (n != 0) { int r = n % 10; if (r > 0) if ((m % r) != 0) return false; n /= 10; } return true; } // function to calculate // the number of numbers static int count(int l, int r) { int ans = 0; for (int i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function public static void main(String args[]) { int l = 10, r = 20; System.out.println(count(10, 20)); } } // This code is contributed by Nikita Tiwari. |
Python3
# Python 3 program # to Count numbers in # range L-R that are # divisible by all of # its non-zero digits # check if the number is # divisible by the digits. def check(n) : m = n while (n != 0) : r = n % 10 if (r > 0) : if ((m % r) != 0) : return False n = n // 10 return True # function to calculate the # number of numbers def count(l, r) : ans = 0 for i in range(l, r+1) : if (check(i)) : ans = ans + 1 return ans # Driver function l = 10r = 20print(count(l, r)) # This code is contributed by Nikita Tiwari. |
C#
// Java program to Count // numbers in range L-R // that are divisible by // all of its non-zero // digits using System; class GFG { // check if the number // is divisible by the // digits. static bool check(int n) { int m = n; while (n != 0) { int r = n % 10; if (r > 0) if ((m % r) != 0) return false; n /= 10; } return true; } // function to calculate // the number of numbers static int count(int l, int r) { int ans = 0; for (int i = l; i <= r; i++) if (check(i)) ans += 1; return ans; } // Driver function public static void Main() { int l = 10, r = 20; Console.WriteLine(count(l, r)); } } // This code is contributed by Vt_m. |
PHP
<?php // PHP program to Count numbers // in range L-R that are // divisible by all of its // non-zero digits // check if the number is // divisible by the digits. function check($n) { $m = $n; while ($n) { $r = $n % 10; if ($r > 0) if (($m % $r) != 0) return false; $n /= 10; } return true; } // function to calculate the // number of numbers function countIn($l, $r) { $ans = 0; for ($i = $l; $i <= $r; $i++) if (check($i)) $ans += 1; return $ans; } // Driver function $l = 10; $r = 20; echo countIn($l, $r); // This code is contributed ajit ?> |
Output:
5
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 of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Check if the sum of digits of number is divisible by all of its digits
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Find all the possible numbers in a range that can be evenly divided by its digits
- Count numbers in given range such that sum of even digits is greater than sum of odd digits
- Count of N-digit Numbers having Sum of even and odd positioned digits divisible by given numbers
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Minimum digits to be removed to make either all digits or alternating digits same
- Count of integers in a range which have even number of odd digits and odd number of even digits
- Smallest N digit number with none of its digits as its divisor
- Count of numbers with all digits same in a given range
- Count all prime numbers in a given range whose sum of digits is also prime
- Program to check if a number is divisible by any of its digits
- Program to check if a number is divisible by sum of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Count of Array elements greater than all elements on its left and at least K elements on its right
- Count of Array elements greater than all elements on its left and next K elements on its right
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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : jit_t

