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

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 divisble 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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 n
bool 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 theoriginal 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 code
int main()
{
    long long int n = 123;
  
    if (isDivisible(n))
        cout << "Yes";
    else
        cout << "No";
  
    return 0;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// Java implementation of the approach 
class 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 theoriginal 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

chevron_right


Python

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python implementation of the approach
  
# Function that returns true if all the digits
# of n divide the sum of the digits of n
def 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 theoriginal 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 code
n = 123
if(isDivisible(n)):
    print("Yes")
else:
    print("No")

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// C# implementation of the approach
using 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 theoriginal 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.

chevron_right


Output:

Yes

Time Complexity: O(log(N))
Auxiliary Space: O(1)



My Personal Notes arrow_drop_up

Small things always make you to think big

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 : AnkitRai01, jit_t



Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.