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++
#include <iostream>
using namespace std;
int isSumDivides( int N)
{
int temp = N;
int sum = 0;
while (temp) {
sum += temp % 10;
temp /= 10;
}
if (N % sum == 0)
return 1;
else
return 0;
}
int main()
{
int N = 12;
if (isSumDivides(N))
cout << "YES" ;
else
cout << "NO" ;
return 0;
}
|
Java
import java.util.*;
import java.lang.*;
class GFG
{
static int isSumDivides( int N)
{
int temp = N;
int sum = 0 ;
while (temp > 0 )
{
sum += temp % 10 ;
temp /= 10 ;
}
if (N % sum == 0 )
return 1 ;
else
return 0 ;
}
public static void main(String args[])
{
int N = 12 ;
if (isSumDivides(N) == 1 )
System.out.print( "YES" );
else
System.out.print( "NO" );
}
}
|
Python3
def isSumDivides(N):
temp = N
sum = 0
while (temp):
sum + = temp % 10
temp = int (temp / 10 )
if (N % sum = = 0 ):
return 1
else :
return 0
if __name__ = = '__main__' :
N = 12
if (isSumDivides(N)):
print ( "YES" )
else :
print ( "NO" )
|
C#
using System;
class GFG
{
public int isSumDivides( int N)
{
int temp = N, sum = 0;
while (temp > 0)
{
sum += temp % 10;
temp /= 10;
}
if (N % sum == 0)
return 1;
else
return 0;
}
public static void Main()
{
GFG g = new GFG();
int N = 12;
if (g.isSumDivides(N) > 0)
Console.WriteLine( "YES" );
else
Console.WriteLine( "NO" );
}
}
|
Javascript
<script>
function isSumDivides(N) {
var temp = N;
var sum = 0;
while (temp > 0) {
sum += temp % 10;
temp = parseInt(temp/10);
}
if (N % sum == 0)
return 1;
else
return 0;
}
var N = 12;
if (isSumDivides(N) == 1)
document.write( "YES" );
else
document.write( "NO" );
</script>
|
PHP
<?php
function isSumDivides( $N )
{
$temp = $N ;
$sum = 0;
while ( $temp )
{
$sum += $temp % 10;
$temp = (int) $temp / 10;
}
if ( $N % $sum == 0)
return 1;
else
return 0;
}
$N = 12;
if (isSumDivides( $N ))
echo "YES" ;
else
echo "NO" ;
?>
|
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 <iostream>
#include <string>
#include <vector>
#include <numeric>
using namespace std;
bool isSumDivides( int N) {
string str_N = to_string(N);
vector< int > digits;
for ( char c : str_N) {
digits.push_back(c - '0' );
}
int sum_of_digits = accumulate(digits.begin(), digits.end(), 0);
return N % sum_of_digits == 0;
}
int main() {
int N = 12;
cout << boolalpha << isSumDivides(N) << endl;
N = 15;
cout << boolalpha << isSumDivides(N) << endl;
return 0;
}
|
Java
import java.util.Arrays;
public class Main {
public static boolean isSumDivides( int N) {
int [] digits = Arrays.stream(Integer.toString(N).split( "" )).mapToInt(Integer::parseInt).toArray();
int sum_of_digits = Arrays.stream(digits).sum();
return N % sum_of_digits == 0 ;
}
public static void main(String[] args) {
int N = 12 ;
System.out.println(isSumDivides(N));
N = 15 ;
System.out.println(isSumDivides(N));
}
}
|
Python3
def isSumDivides(N):
digits = list ( map ( int , str (N)))
sum_of_digits = sum (digits)
return N % sum_of_digits = = 0
N = 12
print (isSumDivides(N))
N = 15
print (isSumDivides(N))
|
C#
using System;
using System.Linq;
public class GFG {
public static bool IsSumDivides( int N) {
int [] digits = Array.ConvertAll(N.ToString().ToCharArray(), c => Convert.ToInt32(c.ToString()));
int sum_of_digits = digits.Sum();
return N % sum_of_digits == 0;
}
public static void Main() {
int N = 12;
Console.WriteLine(IsSumDivides(N));
N = 15;
Console.WriteLine(IsSumDivides(N));
}
}
|
Javascript
function isSumDivides(N)
{
let digits = N.toString().split( "" ).map(Number);
let sum_of_digits = digits.reduce((acc, cur) => acc + cur, 0);
return N % sum_of_digits == 0;
}
let N = 12;
console.log(isSumDivides(N));
N = 15;
console.log(isSumDivides(N));
|
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
Below is the Implementation –
C++
#include <bits/stdc++.h>
using namespace std;
int sum_of_digits( int n)
{
if (n == 0)
return 0;
return (n % 10 + sum_of_digits(n / 10));
}
int main()
{
int num_1 = 12;
int num_2 = 15;
int result_1 = num_1 % sum_of_digits(num_1);
int result_2 = num_2 % sum_of_digits(num_2);
if (result_1 == 0){
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
if (result_2 == 0){
cout << "YES" ;
}
else {
cout << "NO" ;
}
return 0;
}
|
Java
import java.util.*;
class GFG {
static int sumOfDigits( int n) {
if (n == 0 )
return 0 ;
return (n % 10 + sumOfDigits(n / 10 ));
}
public static void main(String[] args) {
int num1 = 12 ;
int num2 = 15 ;
int result1 = num1 % sumOfDigits(num1);
int result2 = num2 % sumOfDigits(num2);
if (result1 == 0 ) {
System.out.println( "YES" );
} else {
System.out.println( "NO" );
}
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 )))
num_1 = 12
result_1 = num_1 % sum_of_digits(num_1)
num_2 = 15
result_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
{
static int SumOfDigits( int n)
{
if (n == 0)
return 0;
return (n % 10 + SumOfDigits(n / 10));
}
static void Main( string [] args)
{
int num_1 = 12;
int num_2 = 15;
int result_1 = num_1 % SumOfDigits(num_1);
int result_2 = num_2 % SumOfDigits(num_2);
if (result_1 == 0)
{
Console.WriteLine( "YES" );
}
else
{
Console.WriteLine( "NO" );
}
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" );
}
|
Time complexity: O(logn), where n is the number.
Auxiliary space: O(logn), due to the recursive call.