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++
#include <bits/stdc++.h>
using namespace std;
string isDivisible( long long int n)
{
long long int temp = n;
int sum = 0;
while (n) {
int k = n % 10;
sum += k;
n /= 10;
}
if (temp % sum == 0)
return "YES" ;
return "NO" ;
}
int main()
{
long long int n = 123;
cout << isDivisible(n);
return 0;
}
|
Java
import java.io.*;
class GFG
{
static String isDivisible( long n)
{
long temp = n;
int sum = 0 ;
while (n != 0 )
{
int k = ( int ) n % 10 ;
sum += k;
n /= 10 ;
}
if (temp % sum == 0 )
return "YES" ;
return "NO" ;
}
public static void main(String []args)
{
long n = 123 ;
System.out.println(isDivisible(n));
}
}
|
Python3
def isDivisible(n):
temp = n
sum = 0 ;
while (n):
k = n % 10 ;
sum + = k;
n / = 10 ;
if (temp % sum = = 0 ):
return "YES" ;
return "NO" ;
n = 123 ;
print (isDivisible(n));
|
C#
using System;
class GFG
{
static String isDivisible( long n)
{
long temp = n;
int sum = 0;
while (n != 0)
{
int k = ( int ) n % 10;
sum += k;
n /= 10;
}
if (temp % sum == 0)
return "YES" ;
return "NO" ;
}
public static void Main()
{
long n = 123;
Console.WriteLine(isDivisible(n));
}
}
|
PHP
<?php
function isDivisible( $n )
{
$temp = $n ;
$sum = 0;
while ( $n )
{
$k = $n % 10;
$sum += $k ;
$n = (int)( $n / 10);
}
if ( $temp % $sum == 0)
return "YES" ;
return "NO" ;
}
$n = 123;
print (isDivisible( $n ));
?>
|
Javascript
<script>
function isDivisible(n)
{
temp = n;
sum = 0;
while (n)
{
k = n % 10;
sum += k;
n = parseInt(n / 10);
}
if (temp % sum == 0)
return "YES" ;
return "NO" ;
}
let n = 123;
document.write(isDivisible(n));
</script>
|
Time Complexity: O(log10n)
Auxiliary Space: O(1)
Method #2: Using string:
- 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++
#include <bits/stdc++.h>
using namespace std;
string getResult( long long int n)
{
string st = std::to_string(n);
int sum = 0;
for ( char i : st)
{
sum = sum + ( int ) i;
}
if (n % sum == 0)
return "Yes" ;
else
return "No" ;
}
int main()
{
int n = 123;
cout << getResult(n);
return 0;
}
|
Java
import java.io.*;
class GFG{
static String getResult( int n)
{
String st = String.valueOf(n);
int sum = 0 ;
for ( char i : st.toCharArray())
{
sum = sum + ( int ) i;
}
if (n % sum == 0 )
return "Yes" ;
else
return "No" ;
}
public static void main(String[] args)
{
int n = 123 ;
System.out.println(getResult(n));
}
}
|
Python3
def getResult(n):
st = str (n)
sum = 0
length = len (st)
for i in st:
sum = sum + int (i)
if (n % sum = = 0 ):
return "Yes"
else :
return "No"
n = 123
print (getResult(n))
|
C#
using System;
public class GFG{
static String getResult( int n)
{
String st = String.Join( "" ,n);
int sum = 0;
foreach ( char i in st.ToCharArray())
{
sum = sum + ( int ) i;
}
if (n % sum == 0)
return "Yes" ;
else
return "No" ;
}
public static void Main(String[] args)
{
int n = 123;
Console.WriteLine(getResult(n));
}
}
|
Javascript
<script>
function getResult(n)
{
let st = (n).toString();
let sum = 0;
for (let i of st.split( "" ))
{
sum = sum + parseInt(i);
}
if (n % sum == 0)
return "Yes" ;
else
return "No" ;
}
let n = 123;
document.write(getResult(n)+ "<br>" );
</script>
|
Time Complexity: O(N), Here N is the total number of digits in n.
Auxiliary Space: O(N), The extra space is used to store the number converted in string.
Feeling lost in the world of random DSA topics, wasting time without progress? It's time for a change! Join our DSA course, where we'll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 geeks!
Last Updated :
16 Dec, 2022
Like Article
Save Article