# Find count of digits in a number that divide the number in C++

C++Server Side ProgrammingProgramming

#### C in Depth: The Complete C Programming Guide for Beginners

45 Lectures 4.5 hours

#### Practical C++: Learn C++ Basics Step by Step

Most Popular

50 Lectures 4.5 hours

#### Master C and Embedded C Programming- Learn as you go

Best Seller

66 Lectures 5.5 hours

Suppose a number is given. We have to count number of digits of the number which evenly divides the number. Suppose the number is 1012, the result is 3. There are three digits 1, 1, and 2 that evenly divide 1012.

To solve this, we will find each digit of the number, using modulus operation, and check whether the number is divisible by that digit or not, if divisible, then increase counter. If the digit is 0, then ignore that digit.

## Example

#include<iostream>
using namespace std;
int countDivDigit(int num) {
int count = 0;
int temp = num;
while(temp){
int div = temp%10;
if(div != 0){
if(num % div == 0)
count++;
}
temp /= 10;
}
return count;
}
int main() {
int num = 1012;
cout << "Number of digits that divides " << num << " evenly, is: " << countDivDigit(num);
}

## Output

Number of digits that divides 1012 evenly, is: 3
Updated on 01-Nov-2019 07:04:38