- 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

We are provided two numbers START and END to define a range of numbers.The goal is to find all the numbers in the range [START,END] that are divisible by all of its non-zero digits . We will do this by traversing numbers from START to END and for each number we will check if the number is divisible by all non-zero digits using a while loop. If yes increment count.

Let’s understand with examples.

**Input**

START=10 END=20

**Output**

Numbers that are divisible by all its non-zero digits: 14

**Explanation**

Numbers 10, 11, 12, 15, 20 are divisible by all their non-zero digits.

**Input**

START=100 END=200

**Output**

Numbers that are divisible by all its non-zero digits: 25

**Explanation**

This is list of numbers divisible by all non-zero digits : 100 101 102 104 105 110 111 112 115 120 122 124 126 128 132 135 140 144 150 155 162 168 175 184 200

We take an integer START and END as range variables.

Function divisiblebyDigits(int start, int end) takes range variables and returns the count of numbers divisible by all of their non zero digits.

Take the initial variable count as 0 for such numbers.

Take variable flag as 0

Traverse range of numbers using for loop. i=start to i=end

Now for each number num=i, using while loop check if number is >0.

calculate digit=num%10. If digit>0 and i%digit==0 set flag=1. Else flag=0 and break. Reduce num=num/10 to check next digit.

If all the non-zero digits fully divide i, flag is 1. Increment count.

At the end of all loops count will have a total number which is divisible by non-zero digits

Return the count as result.

#include <bits/stdc++.h> using namespace std; int divisiblebyDigits(int start, int end){ int count = 0; int flag=0; for (int i = start; i <= end; i++){ int num=i; while(num>0){ int digit=num%10; if(digit>0){ if(i%digit==0) { flag=1; } //set flag else{ flag=0; //un-set flag break; } } num=num/10; } if(flag==1) //divisible by all non-zero digits { count++; //cout<<i<<" "; } } return count; } int main(){ int START = 10, END = 50; cout <<"Numbers that are divisible by all its non-zero digits: "<< divisiblebyDigits(START,END); return 0; }

If we run the above code it will generate the following output −

Numbers that are divisible by all its non-zero digits: 14

- Related Questions & Answers
- Count numbers in a range that are divisible by all array elements in C++
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M in C++
- Count of all even numbers in the range [L, R] whose sum of digits is divisible by 3 in C++
- Count of Numbers in Range where the number does not contain more than K non zero digits in C++
- Count numbers which are divisible by all the numbers from 2 to 10 in C++
- Count numbers in range 1 to N which are divisible by X but not by Y in C++
- Count the numbers divisible by ‘M’ in a given range in C++
- Find the Numbers that are not divisible by any number in the range [2, 10] using C++
- Count of m digit integers that are divisible by an integer n in C++
- How to find numbers that are divisible by a certain number for a range of values in R?
- Finding all the n digit numbers that have sum of even and odd positioned digits divisible by given numbers - JavaScript
- Count of Numbers in a Range divisible by m and having digit d in even positions in C++
- Finding the count of numbers divisible by a number within a range using JavaScript
- Count numbers in range such that digits in it and it's product with q are unequal in C++
- Count all prefixes of the given binary array which are divisible by x in C++

Advertisements