Program to check if a number is divisible by sum of its digits - GeeksforGeeks

13 Aug, 2021
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 = 12 
Output: YES
Explanation: 
    As sum of digits of 12 = 1 + 2 = 3
    and 12 is divisible by 3 
    So the output is YES

Input: N = 123
Output: 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++
Java
Python3
C#
PHP
Javascript
// C++ implementation of above approach #include <bits/stdc++.h>using namespace std; // Function to check// if the given number is divisible// by sum of its digitsstring 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 Codeint main(){    long long int n = 123;     cout << isDivisible(n);     return 0;}
// Java implementation of above approachclass 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
# Python 3 implementation of above approach # Function to check if the given number# is divisible by sum of its digitsdef 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 Coden = 123; print(isDivisible(n)); # This code is contributed by# Akanksha Rai
// C# implementation of above approachusing 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..
<?php// PHP implementation of above approach // Function to check if the given number// is divisible by sum of its digitsfunction 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?>
<script> // Javascript implementation of above approach // Function to check if the given number// is divisible by sum of its digitsfunction isDivisible(n){    temp = n;     // Find sum of digits    sum = 0;         while (n)    {        k = n % 10;        sum += k;        n = parseInt(n / 10);    }     // Check if sum of digits divides n    if (temp % sum == 0)        return "YES";     return "NO";} // Driver Codelet n = 123; document.write(isDivisible(n));  // This code is contributed by sravan kumar </script>
NO
 
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.
If the number is divisible by sum then print Yes
Else print No
Below is the implementation of above approach:
C++
Java
Python3
C#
Javascript
// C++ implementation of above approach#include <bits/stdc++.h>using namespace std; string getResult(long long int n){         // Converting integer to String    string st = std::to_string(n);     // Initialising sum to 0    int sum = 0;     // Traversing through the String    for(char i : st)    {                 // Converting character to int        sum = sum + (int) i;    }         // Comparing number and sum    if (n % sum == 0)        return "Yes";    else        return "No";} // Driver Codeint main(){    int n = 123;         // Passing this number to get result function    cout << getResult(n);    return 0;} // This code is contributed by 29AjayKumar
// Java implementation of above approachclass GFG{     static String getResult(int n){         // Converting integer to String    String st = String.valueOf(n);     // Initialising sum to 0    int sum = 0;     // Traversing through the String    for(char i : st.toCharArray())    {                 // Converting character to int        sum = sum + (int) i;    }         // Comparing number and sum    if (n % sum == 0)        return "Yes";    else        return "No";} // Driver Codepublic static void main(String[] args){    int n = 123;         // Passing this number to get result function    System.out.println(getResult(n));}} // This code is contributed by 29AjayKumar
# 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    if (n % sum == 0):        return "Yes"    else:        return "No"  # Driver Coden = 123# passing this number to get result functionprint(getResult(n)) # this code is contributed by vikkycirus
// C# implementation of above approachusing System;public class GFG{     static String getResult(int n){         // Converting integer to String    String st = String.Join("",n);     // Initialising sum to 0    int sum = 0;     // Traversing through the String    foreach(char i in st.ToCharArray())    {                 // Converting character to int        sum = sum + (int) i;    }         // Comparing number and sum    if (n % sum == 0)        return "Yes";    else        return "No";} // Driver Codepublic static void Main(String[] args){    int n = 123;         // Passing this number to get result function    Console.WriteLine(getResult(n));}} // This code is contributed by Amit Katiyar
<script> // JavaScript implementation of above approach function getResult(n){    // Converting integer to String    let st = (n).toString();      // Initialising sum to 0    let sum = 0;      // Traversing through the String    for(let i of st.split(""))    {                  // Converting character to int        sum = sum + parseInt(i);    }          // Comparing number and sum    if (n % sum == 0)        return "Yes";    else        return "No";} // Driver Codelet n = 123;      // Passing this number to get result functiondocument.write(getResult(n)+"<br>"); // This code is contributed by unknown2108 </script>
No
ankthon
vt_m
Chandan_Kumar
Akanksha_Rai
vikkycirus
sravankumar8128
29AjayKumar
unknown2108
amit143katiyar
number-digits
Mathematical
School Programming
Mathematical
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Program to find sum of elements in a given array
Program for factorial of a number
Operators in C / C++
Euclidean algorithms (Basic and Extended)
The Knight's tour problem | Backtracking-1
Python Dictionary
Arrays in C/C++
Inheritance in C++
Reverse a string in Java
Interfaces in Java