Self Dividing Numbers

Difficulty Level Easy
Frequently asked in Adobe Epic Systems Tesla
MathViews 3983

A number is known as a self dividing numbers if –

1. Mod of every digit of number with number is zero.

2. The number should contain all non zero digits.

For instance – 128

128 % 1 = 0, 128 % 2 = 0, 128 % 8 = 0

Therefore it is a self dividing number.

Self Dividing Numbers

Example

Input: 128

Output: Yes

Input: 10

Output: No

Explanation

Let the given number = 128

Create a temporary variable to hold the value of the given number and another variable digit to store the current digit.

Steps to check if the given number is a self dividing number or not –

  • Initially n = 128, temp = 128, digit = 0.
  • Step 1 : digit = 8 and n%digit=0 i.e. 128%8=0, therefore, temp = 12.
  • Step 2 : digit = 2 and n%digit=0 i.e. 128%2=0, therefore, temp = 1.
  • Step 3 : digit = 1 and n%digit=0 i.e. 128%1=0, therefore, temp = 0.

As all the digits of the given number 128 divides the given number completely, we can say that the given number is a self dividing number.

Algorithm

  1. Initialise an integer variable/number n.
  2. Create a function to check if the given number is self dividing which accepts an integer value as it’s a parameter.
  3. Create a temporary variable of integer type and store the value of n in it.
  4. Traverse through the temporary variable and while the temporary variable is greater than 0, create another integer variable to store the digits and store the last digit of the given number in it.
  5. After that, check if the digit variable is not equal to 0 and the mod of given number, and the digit variable is 0, update the temporary variable as the temporary variable divided by 10 else return false.
  6. Return true.
  7. If the returned value is equal to true, print “Yes”. Else print “No”.

C++ Program to check self dividing numbers

#include <bits/stdc++.h> 
using namespace std; 
  
bool isDivisible(int n, int digit){ 
    
    return ((digit != 0) && (n % digit == 0)); 

} 
  
bool selfDivide(int n){ 
    int temp = n; 
    while(temp > 0){ 
  
        int digit = n % 10; 
        if(!(isDivisible(n, digit))) 
            return false; 
  
        temp /= 10; 
    } 
    return true; 
} 
  
int main(){ 
    int n = 128; 
    if(selfDivide(n)) 
        cout<<"Yes"; 
    else
        cout<<"No"; 
    return 0; 
}
Yes

Java Program to check self dividing numbers

import java.io.*; 
  
class Divide{ 
  
    static boolean isDivisible(int n, int digit){ 
        
        return((digit != 0) && (n % digit == 0)); 
    
    } 
  
    static boolean selfDivide(int n){ 
        int temp = n; 
        while(temp > 0) { 
  
            int digit = n % 10; 
  
            if((isDivisible(n, digit)) == false) 
                return false; 
  
            temp /= 10; 
        } 
        return true; 
    } 
  
    public static void main(String args[]){ 
        int n = 128; 
  
        if(selfDivide(n)) 
            System.out.println("Yes"); 
        else
            System.out.println("No"); 
    } 
}
Yes

Complexity Analysis

Time Complexity: O(L) (L is the number of digits in given number)

Space Complexity: O(1) because we used constant space.

References

Translate ยป