Check if the sum of digits of number is divisible by all of its digits - GeeksforGeeks

15 Mar, 2022
Given an integer N, the task is to check whether the sum of digits of the given number is divisible by all of its digits or not. If divisible then print Yes else print No.
Examples: 
Input: N = 12 Output: No Sum of digits = 1 + 2 = 3 3 is divisible by 1 but not 2.
Input: N = 123 Output: Yes 
Approach: First find the sum of the digits of the number then one by one check, whether the calculated sum is divisible by all the digits of the number. If for some digit it is not divisible then print No else print Yes.
Below is the implementation of the above approach: 
C++
Java
Python
C#
Javascript
// C++ implementation of the approach#include <bits/stdc++.h>using namespace std; // Function that returns true if all the digits// of n divide the sum of the digits of nbool isDivisible(long long int n){     // Store a copy of the original number    long long int temp = n;     // Find the sum of the digits of n    int sum = 0;    while (n) {        int digit = n % 10;        sum += digit;        n /= 10;    }     // Restore the original value    n = temp;     // Check if all the digits divide    // the calculated sum    while (n) {        int digit = n % 10;         // If current digit doesn't        // divide the sum        if (sum % digit != 0)            return false;         n /= 10;    }     return true;} // Driver codeint main(){    long long int n = 123;     if (isDivisible(n))        cout << "Yes";    else        cout << "No";     return 0;}
// Java implementation of the approachclass GFG{         // Function that returns true if all the digits    // of n divide the sum of the digits of n    static boolean isDivisible(long n)    {             // Store a copy of the original number        long temp = n;             // Find the sum of the digits of n        int sum = 0;        while (n != 0)        {            int digit = (int) n % 10;            sum += digit;            n /= 10;        }             // Restore the original value        n = temp;             // Check if all the digits divide        // the calculated sum        while (n != 0)        {            int digit = (int)n % 10;                 // If current digit doesn't            // divide the sum            if (sum % digit != 0)                return false;                 n /= 10;        }        return true;    }         // Driver code    public static void main (String[] args)    {        long n = 123;             if (isDivisible(n))            System.out.println("Yes");        else            System.out.println("No");    }} // This code is contributed by AnkitRai01
# Python implementation of the approach # Function that returns true if all the digits# of n divide the sum of the digits of ndef isDivisible(n):     # Store a copy of the original number    temp = n     # Find the sum of the digits of n    sum = 0    while (n):        digit = n % 10        sum += digit        n //= 10     # Restore the original value    n = temp     # Check if all the digits divide    # the calculated sum    while(n):        digit = n % 10         # If current digit doesn't        # divide the sum        if(sum % digit != 0):            return False         n //= 10;     return True # Driver coden = 123if(isDivisible(n)):    print("Yes")else:    print("No")
// C# implementation of the approachusing System; class GFG{         // Function that returns true if all the digits    // of n divide the sum of the digits of n    static bool isDivisible(long n)    {             // Store a copy of the original number        long temp = n;             // Find the sum of the digits of n        int sum = 0;        while (n != 0)        {            int digit = (int) n % 10;            sum += digit;            n /= 10;        }             // Restore the original value        n = temp;             // Check if all the digits divide        // the calculated sum        while (n != 0)        {            int digit = (int)n % 10;                 // If current digit doesn't            // divide the sum            if (sum % digit != 0)                return false;                 n /= 10;        }        return true;    }         // Driver code    static public void Main ()    {        long n = 123;             if (isDivisible(n))            Console.Write("Yes");        else            Console.Write("No");    }} // This code is contributed by @tushil.
<script> // Javascript implementation of the approach     // Function that returns true if all the// digits of n divide the sum of the digits// of nfunction isDivisible(n){         // Store a copy of the original number    var temp = n;     // Find the sum of the digits of n    var sum = 0;         while (n != 0)    {        var digit = parseInt(n % 10);        sum += digit;        n = parseInt(n / 10);    }     // Restore the original value    n = temp;     // Check if all the digits divide    // the calculated sum    while (n != 0)    {        var digit = parseInt(n % 10);         // If current digit doesn't        // divide the sum        if (sum % digit != 0)            return false;         n = parseInt(n/10);    }    return true;} // Driver codevar n = 123; if (isDivisible(n))    document.write("Yes");else    document.write("No"); // This code is contributed by todaysgaurav </script>
Yes
Time Complexity: O(log(N)) Auxiliary Space: O(1)
We have to convert the given number to a string by taking a new variable.
Traverse the string ,Convert each element to integer and add this to sum.
Traverse the string again
Check if the sum is not divisible by any one of the digits
If it is true then return False
Else return True
Below is the implementation of above approach:
C++
Python3
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; string getResult(int n){     // Converting integer to string    string st = to_string(n);     // Initialising sum to 0    int sum = 0;    int length = st.size();     // Traversing through the string    for (int i = 0; i < st.size(); i++) {         // Converting character to int        sum = sum + (int(st[i]) - 48);    }     // Comparing number and sum    // Traversing again    for (int i = 0; i < st.size(); i++) {         // Check if any digit is        // not dividing the sum        if (sum % (int(st[i]) - 48) != 0) {             // Return false            return "No";        }         // If any value is not returned        // then all the digits are dividing the sum        // SO return true        else {            return "Yes";        }    }} // Driver Codeint main(){    int n = 123;     // passing this number to get result function    cout << getResult(n);    return 0;} // This code is contributed by subhamkumarm348.
# Python implementation of above approachdef getResult(n):       # Converting integer to string    st = str(n)         # Initialising sum to 0    sum = 0    length = len(st)     # Traversing through the string    for i in st:         # Converting character to int        sum = sum + int(i)             # Comparing number and sum    # Traversing again    for i in st:               # Check if any digit is        # not dividing the sum        if(sum % int(i) != 0):                         # Return false            return 'No'               # If any value is not returned    # then all the digits are dividing the sum    # SO return true    return 'Yes'  # Driver Coden = 123 # passing this number to get result functionprint(getResult(n)) # this code is contributed by vikkycirus
<script> // JavaScript implementation of above approach function getResult(n){        // Converting integer to string    var st = n.toString();          // Initialising sum to 0    var sum = 0    var length = st.length;      // Traversing through the string    for (var i= 0 ;i<st.length ; i++){          // Converting character to int        sum = sum + Number(st[i])    }              // Comparing number and sum    // Traversing again    for( var i = 0 ; i < st.length ; i++){                // Check if any digit is        // not dividing the sum        if(sum % Number(st[i]) != 0){                          // Return false            return 'No'        }                // If any value is not returned    // then all the digits are dividing the sum    // SO return true        else{            return 'Yes';          }    }       }    // Driver Codevar n = 123;  // passing this number to get result functiondocument.write(getResult(n));  </script>
Yes
ankthon
jit_t
ManasChhabra2
vikkycirus
todaysgaurav
bunnyram19
subhamkumarm348
number-digits
Mathematical
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Print all possible combinations of r elements in a given array of size n
Program for factorial of a number
The Knight's tour problem | Backtracking-1
Operators in C / C++
Find minimum number of coins that make a given value
Program to find sum of elements in a given array
Program to print prime numbers from 1 to N.
Euclidean algorithms (Basic and Extended)
Segment Tree | Set 1 (Sum of given range)
Write a program to calculate pow(x,n)