Given an integer N, the task is to check whether the number is divisible by the sum of its digits or not. If divisible, then print “YES” else print “NO”.

**Examples:**

Input:N = 12Output:YESExplanation:As sum of digits of 12 = 1 + 2 = 3 and 12 is divisible by 3 So the output is YESInput:N = 123Output:NO

**Approach:** The idea to solve the problem is to extract the digits of the number and add them. Then check if the number is divisible by the sum of its digit. If it is divisible then print YES otherwise print NO.

Below is the implementation of above approach:

**Implementation:**

## C++

`// CPP implementation of above approach ` ` ` `#include <bits/stdc++.h> ` `using` `namespace` `std; ` ` ` `// Function to check ` `// if the given number is divisible ` `// by sum of its digits ` `string isDivisible(` `long` `long` `int` `n) ` `{ ` ` ` `long` `long` `int` `temp = n; ` ` ` ` ` `// Find sum of digits ` ` ` `int` `sum = 0; ` ` ` `while` `(n) { ` ` ` `int` `k = n % 10; ` ` ` `sum += k; ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `// check if sum of digits divides n ` ` ` `if` `(temp % sum == 0) ` ` ` `return` `"YES"` `; ` ` ` ` ` `return` `"NO"` `; ` `} ` ` ` `// Driver Code ` `int` `main() ` `{ ` ` ` `long` `long` `int` `n = 123; ` ` ` ` ` `cout << isDivisible(n); ` ` ` ` ` `return` `0; ` `} ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check if the ` ` ` `// given number is divisible ` ` ` `// by sum of its digits ` ` ` `static` `String isDivisible(` `long` `n) ` ` ` `{ ` ` ` `long` `temp = n; ` ` ` ` ` `// Find sum of digits ` ` ` `int` `sum = ` `0` `; ` ` ` `while` `(n != ` `0` `) ` ` ` `{ ` ` ` `int` `k = (` `int` `) n % ` `10` `; ` ` ` `sum += k; ` ` ` `n /= ` `10` `; ` ` ` `} ` ` ` ` ` `// check if sum of digits divides n ` ` ` `if` `(temp % sum == ` `0` `) ` ` ` `return` `"YES"` `; ` ` ` ` ` `return` `"NO"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String []args) ` ` ` `{ ` ` ` `long` `n = ` `123` `; ` ` ` `System.out.println(isDivisible(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by Ryuga ` |

*chevron_right*

*filter_none*

## Python3

`# Python 3 implementation of above approach ` ` ` `# Function to check if the given number ` `# is divisible by sum of its digits ` `def` `isDivisible(n): ` ` ` ` ` `temp ` `=` `n ` ` ` ` ` `# Find sum of digits ` ` ` `sum` `=` `0` `; ` ` ` `while` `(n): ` ` ` ` ` `k ` `=` `n ` `%` `10` `; ` ` ` `sum` `+` `=` `k; ` ` ` `n ` `/` `=` `10` `; ` ` ` ` ` `# check if sum of digits divides n ` ` ` `if` `(temp ` `%` `sum` `=` `=` `0` `): ` ` ` `return` `"YES"` `; ` ` ` ` ` `return` `"NO"` `; ` ` ` `# Driver Code ` `n ` `=` `123` `; ` ` ` `print` `(isDivisible(n)); ` ` ` `# This code is contributed by ` `# Akanksha Rai ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to check if the ` ` ` `// given number is divisible ` ` ` `// by sum of its digits ` ` ` `static` `String isDivisible(` `long` `n) ` ` ` `{ ` ` ` `long` `temp = n; ` ` ` ` ` `// Find sum of digits ` ` ` `int` `sum = 0; ` ` ` `while` `(n != 0) ` ` ` `{ ` ` ` `int` `k = (` `int` `) n % 10; ` ` ` `sum += k; ` ` ` `n /= 10; ` ` ` `} ` ` ` ` ` `// check if sum of digits divides n ` ` ` `if` `(temp % sum == 0) ` ` ` `return` `"YES"` `; ` ` ` ` ` `return` `"NO"` `; ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `Main() ` ` ` `{ ` ` ` `long` `n = 123; ` ` ` `Console.WriteLine(isDivisible(n)); ` ` ` `} ` `} ` ` ` `// This code is contributed by anuj_67.. ` |

*chevron_right*

*filter_none*

## PHP

`<?php ` `// PHP implementation of above approach ` ` ` `// Function to check if the given number ` `// is divisible by sum of its digits ` `function` `isDivisible(` `$n` `) ` `{ ` ` ` `$temp` `= ` `$n` `; ` ` ` ` ` `// Find sum of digits ` ` ` `$sum` `= 0; ` ` ` `while` `(` `$n` `) ` ` ` `{ ` ` ` `$k` `= ` `$n` `% 10; ` ` ` `$sum` `+= ` `$k` `; ` ` ` `$n` `= (int)(` `$n` `/ 10); ` ` ` `} ` ` ` ` ` `// check if sum of digits divides n ` ` ` `if` `(` `$temp` `% ` `$sum` `== 0) ` ` ` `return` `"YES"` `; ` ` ` ` ` `return` `"NO"` `; ` `} ` ` ` `// Driver Code ` `$n` `= 123; ` `print` `(isDivisible(` `$n` `)); ` ` ` `// This code is contributed ` `// by chandan_jnu ` `?> ` |

*chevron_right*

*filter_none*

**Output:**

NO

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.

## Recommended Posts:

- Check if the sum of digits of number is divisible by all of its digits
- Find N numbers such that a number and its reverse are divisible by sum of its digits
- Program to check if a number is divisible by any of its digits
- Check whether product of digits at even places is divisible by sum of digits at odd place of a number
- Kth number exceeding N whose sum of its digits divisible by M
- Numbers with sum of digits equal to the sum of digits of its all prime factor
- Even digits Sum and Odd digits sum divisible by 4 and 3 respectively
- Sum of the digits of square of the given number which has only 1's as its digits
- Count of numbers between range having only non-zero digits whose sum of digits is N and number is divisible by M
- Smallest N digit number with none of its digits as its divisor
- Find a N-digit number such that it is not divisible by any of its digits
- Find a N-digit number such that it is not divisible by any of its digits
- Numbers of Length N having digits A and B and whose sum of digits contain only digits A and B
- Check if a given number divides the sum of the factorials of its digits
- Check whether sum of digits at odd places of a number is divisible by K
- Count numbers in range L-R that are divisible by all of its non-zero digits
- Number of times a number can be replaced by the sum of its digits until it only contains one digit
- Smallest number with given sum of digits and sum of square of digits
- Minimum digits to be removed to make either all digits or alternating digits same
- Find smallest number with given number of digits and sum of digits

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.