Open in App
Not now

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

• Last Updated : 23 Mar, 2023

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 it` `int` `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 Code` `int` `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 it` `static` `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 Code` `public` `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 it` `def` `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 Code` `if` `__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 it ` `using` `System;`   `// Function to check if sum of ` `// digits of a number divides it ` `class` `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 Code ` `public` `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

 ``

## Javascript

 ``

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<``int``> 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 function` `N ``=` `12` `print``(isSumDivides(N)) ``# should print True`   `N ``=` `15` `print``(isSumDivides(N)) ``# should print False` `#This code is contributed by Edula Vinay Kumar Reddy`

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

My Personal Notes arrow_drop_up
Related Articles