+

Check if the sum of the digits of the number N divides this

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 the digits of a number and calculate the sum of all its digits and check if the sum of the digits is N or not.

Below is the implementation of the above approach:

C ++

// C ++ program for checking if the sum
// the digits of the number divide it

 
# include & lt; iostream & gt;

using namespace std;

 
// Function to check if the amount
// digits of the number divide it

int isSumDivides ( int N)

{

int temp = N;

 

int sum = 0;

 

// Calculate the sum of all N digits

  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 & lt; & lt; "YES" ;

else

  cout & lt; & lt; "NO" ;

 

return 0;

}

Java

// Java program for checking if the amount
// digits of the number share it

 

import java.util. *;

import java.lang. *;

 

class GFG

{
// Function for checking if the sum is
// digits of a number divide it

static int isSumDivides ( int N)

{

int temp = N;

 

int sum = 0 ;

 

// Calculate the sum of all N digits

  while (temp & gt; 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 was added
// Akanksha Rai (Abby_akku )

python3

# Python3 program to check if the sum
# the digits of the number divide it

 
# Function to check if the sum
# the digits of the number divide it

def isSumDivides (N):

  

temp = N

  

sum = 0

  

# Calculate the sum of all

# of digits 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 provided
# mit

C #

PHP

// C # program for checking if the amount
// digits of a number divide it

using System;

 
// Function to check if the amount
// digits of a number divide it

class GFG

{

public int isSumDivides ( int N) 

int temp = N, sum = 0; 

 

// Calculate the sum of all

  // N numbers

while (temp & gt; 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) & gt; 0) 

  Console.WriteLine ( "YES" ); 

else

Console.WriteLine ( " NO " ); 


}

 
// This code is provided by Soumik

& lt;? Php
/ / PHP program to check if the sum
// the digits of the number divide it

 
// Function to check if the sum
// the digits of the number divide it

function isSumDivides ( $ N )

{

  $ temp = $ N ;

 

$ sum = 0;

 

// Calculate the sum of all

  // N numbers

while ( $ temp

{

$ sum + = $ temp % 10;

$ temp = (int) $ temp / 10;

}

 

  if ( $ N % $ sum == 0)

  return 1;

else

return 0;

}

 
// Driver code

$ N = 12;

 

if (isSumDivides ( $ N ))

  echo " YES " ;

else

echo " NO " ;

 
// This code is provided by ajit
? & gt;

Exit:

 YES 
Get Solution for free from DataCamp guru