# 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 divisible by 1 but not 2.

Input: N = 123
Output: Yes

## Recommended: Please try your approach on {IDE} first, before moving on to the solution.

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

 `// C++ implementation of the approach ` `#include ` `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 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 code ` `int` `main() ` `{ ` `    ``long` `long` `int` `n = 123; ` ` `  `    ``if` `(isDivisible(n)) ` `        ``cout << ``"Yes"``; ` `    ``else` `        ``cout << ``"No"``; ` ` `  `    ``return` `0; ` `} `

## Java

 `// 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 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

 `# 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 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 code ` `n ``=` `123` `if``(isDivisible(n)): ` `    ``print``(``"Yes"``) ` `else``: ` `    ``print``(``"No"``) `

## C#

 `// 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 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. `

Output:

```Yes
```

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

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

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.

Article Tags :
Practice Tags :

2

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