Open In App

Check if the sum of digits of a number N divides it

Given a number N. The task is to check if the sum of digits of the given number divides the number or not. If it divides it then print YES otherwise print NO.
Examples

Input : N = 12
Output : YES
Sum of digits = 1+2 =3 and 3 divides 12.
So, print YES.
Input : N = 15
Output : NO

Extract digits of the number and calculate the sum of all of its digits and check if the sum of digits dives N or not.
Below is the implementation of the above approach:

C++

 // C++ program to check if sum of// digits of a number divides it #include using namespace std; // Function to check if sum of// digits of a number divides itint isSumDivides(int N){    int temp = N;     int sum = 0;     // Calculate sum of all of digits of N    while (temp) {        sum += temp % 10;        temp /= 10;    }     if (N % sum == 0)        return 1;    else        return 0;} // Driver Codeint main(){    int N = 12;     if (isSumDivides(N))        cout << "YES";    else        cout << "NO";     return 0;}

Java

 // Java program to check if sum of// digits of a number divides it import java.util.*;import java.lang.*; class GFG{// Function to check if sum of// digits of a number divides itstatic int isSumDivides(int N){    int temp = N;     int sum = 0;     // Calculate sum of all of digits of N    while (temp > 0)    {        sum += temp % 10;        temp /= 10;    }     if (N % sum == 0)        return 1;    else        return 0;} // Driver Codepublic static void main(String args[]){    int N = 12;     if (isSumDivides(N) == 1)        System.out.print("YES");    else        System.out.print("NO");}} // This code is contributed// by Akanksha Rai(Abby_akku)

Python3

 # Python3 program to check if sum of# digits of a number divides it # Function to check if sum of# digits of a number divides itdef isSumDivides(N):     temp = N     sum = 0     # Calculate sum of all of    # digits of N    while (temp):        sum += temp % 10        temp = int(temp / 10)     if (N % sum == 0):        return 1    else:        return 0 # Driver Codeif __name__=='__main__':    N = 12     if (isSumDivides(N)):        print("YES")    else:        print("NO")     # This code is contributed by# mits

C#

 // C# program to check if sum of// digits of a number divides itusing System; // Function to check if sum of// digits of a number divides itclass GFG{public int isSumDivides(int N){    int temp = N, sum = 0;     // Calculate sum of all of    // digits of N    while (temp > 0)    {        sum += temp % 10;        temp /= 10;    }     if (N % sum == 0)        return 1;    else        return 0;} // Driver Codepublic static void Main(){    GFG g = new GFG();    int N = 12;     if (g.isSumDivides(N) > 0)        Console.WriteLine("YES");    else        Console.WriteLine("NO");}} // This code is contributed by Soumik



PHP



Output

YES

Time Complexity : O(logn)
Auxiliary Space: O(1), since no extra space has been taken.

Method:  Using the map function, split method, and the sum function:

This approach first converts the number N to a string and then splits it into a list of individual digits using the map function and the split method. It then calculates the sum of the digits using the sum function and checks if the sum divides the number N.

C++

 #include #include #include #include  using namespace std; bool isSumDivides(int N) {    // Convert the number to a string and split it into individual digits    string str_N = to_string(N);    vector digits;    for (char c : str_N) {        digits.push_back(c - '0');    }     // Calculate the sum of the digits    int sum_of_digits = accumulate(digits.begin(), digits.end(), 0);     // Check if the sum of the digits divides the number    return N % sum_of_digits == 0;} int main() {    int N = 12;    cout << boolalpha << isSumDivides(N) << endl; // should print true     N = 15;    cout << boolalpha << isSumDivides(N) << endl; // should print false     return 0;}

Java

 import java.util.Arrays; public class Main {    public static boolean isSumDivides(int N) {        // Convert the number to a string and split it into individual digits        int[] digits = Arrays.stream(Integer.toString(N).split("")).mapToInt(Integer::parseInt).toArray();         // Calculate the sum of the digits        int sum_of_digits = Arrays.stream(digits).sum();         // Check if the sum of the digits divides the number        return N % sum_of_digits == 0;    }     public static void main(String[] args) {        int N = 12;        System.out.println(isSumDivides(N)); // should print true         N = 15;        System.out.println(isSumDivides(N)); // should print false    }}

Python3

 def isSumDivides(N):    # Convert the number to a string and split it into individual digits    digits = list(map(int, str(N)))     # Calculate the sum of the digits    sum_of_digits = sum(digits)     # Check if the sum of the digits divides the number    return N % sum_of_digits == 0 # Test the functionN = 12print(isSumDivides(N)) # should print True N = 15print(isSumDivides(N)) # should print False#This code is contributed by Edula Vinay Kumar Reddy

C#

 using System;using System.Linq; public class GFG {    public static bool IsSumDivides(int N) {        // Convert the number to a string and split it into individual digits        int[] digits = Array.ConvertAll(N.ToString().ToCharArray(), c => Convert.ToInt32(c.ToString()));         // Calculate the sum of the digits        int sum_of_digits = digits.Sum();         // Check if the sum of the digits divides the number        return N % sum_of_digits == 0;    }     public static void Main() {        int N = 12;        Console.WriteLine(IsSumDivides(N)); // should print true         N = 15;        Console.WriteLine(IsSumDivides(N)); // should print false    }}

Javascript

 function isSumDivides(N){   // Convert the number to a string and split it into individual digits  let digits = N.toString().split("").map(Number);   // Calculate the sum of the digits  let sum_of_digits = digits.reduce((acc, cur) => acc + cur, 0);   // Check if the sum of the digits divides the number  return N % sum_of_digits == 0;} let N = 12;console.log(isSumDivides(N)); // should print true N = 15;console.log(isSumDivides(N)); // should print false

Output

True
False

Time complexity: O(n), where n is the number of digits in N
Auxiliary space: O(n), since a list of size n is created to store the digits of N

Method: using Recursion –

In this approach, we will use recursion to find out the sum of all the digits in a number and then divide that number with the sum we got.

Stepwise Explanation –

Step – 1: We will use a function in which we will calculate and return the sum of the digits of a number which we will pass as argument.

Step – 2:  Outside of the function we will use the result we got from the function and divide it with the number we took first and check if the remainder returned is 0 or not.

If 0 then it is entirely divisible otherwise not.

Example –

Let the number we took be 12.

Step – 1 : 12 % 10 is 2 + send (12/10) in next step

Step – 2 – 1 % 10 is 1 + send (1/10) in next step

Step – 3 : No more digits left

so from first step we got 2

from second step we got 1

therefore the sum is – 2+1 = 3

and 12 % 3 == 0.

So our code will return True

C++

 #include using namespace std;  // Function to check sum of digit using recursionint sum_of_digits(int n){    if (n == 0)    return 0;    return (n % 10 + sum_of_digits(n / 10));}  // Driven codeint main(){    // Example 1    int num_1 = 12;         // Example 2      int num_2 = 15;       int result_1 = num_1 % sum_of_digits(num_1);      int result_2 = num_2 % sum_of_digits(num_2);         // First Result    if (result_1 == 0){      cout << "YES" << endl;    }      else{      cout << "NO" << endl;    }         // Second Result      if (result_2 == 0){      cout << "YES";    }      else{      cout << "NO";    }               return 0;}

Java

 import java.util.*; class GFG {    // Function to check sum of digits using recursion    static int sumOfDigits(int n) {        if (n == 0)            return 0;        return (n % 10 + sumOfDigits(n / 10));    }     // Driver code    public static void main(String[] args) {        // Example 1        int num1 = 12;         // Example 2        int num2 = 15;         int result1 = num1 % sumOfDigits(num1);        int result2 = num2 % sumOfDigits(num2);         // First Result        if (result1 == 0) {            System.out.println("YES");        } else {            System.out.println("NO");        }         // Second Result        if (result2 == 0) {            System.out.println("YES");        } else {            System.out.println("NO");        }    }}

Python3

 def sum_of_digits(n):    if n == 0:        return 0    return (n % 10 + sum_of_digits(int(n / 10)))    # Example - 1num_1 = 12result_1 = num_1 % sum_of_digits(num_1) # Example - 2num_2 = 15result_2 = num_2 % sum_of_digits(num_2)  print("YES" if result_1 == 0 else "NO")print("YES" if result_2 == 0 else "NO")

C#

 using System; class GFG{    // Function to check sum of digit using recursion    static int SumOfDigits(int n)    {        if (n == 0)            return 0;        return (n % 10 + SumOfDigits(n / 10));    }     static void Main(string[] args)    {        // Example 1        int num_1 = 12;         // Example 2        int num_2 = 15;         int result_1 = num_1 % SumOfDigits(num_1);        int result_2 = num_2 % SumOfDigits(num_2);         // First Result        if (result_1 == 0)        {            Console.WriteLine("YES");        }        else        {            Console.WriteLine("NO");        }         // Second Result        if (result_2 == 0)        {            Console.WriteLine("YES");        }        else        {            Console.WriteLine("NO");        }    }}

Javascript

 function sumOfDigits(n){    if (n == 0){        return 0;    }    return (n%10+sumOfDigits(parseInt(n/10)));} let num_1 = 12;let num_2 = 15; let result_1 = num_1 % sumOfDigits(num_1);let result_2 = num_2 % sumOfDigits(num_2); if (result_1 == 0){    console.log("YES \n");}else{    console.log("NO \n");} if (result_2 == 0){    console.log("YES \n");}else{    console.log("NO \n");}

Output

YES
NO

Time complexity: O(logn), where n is the number.
Auxiliary space: O(logn), due to the recursive call.