Program to check if a number is divisible by sum of its digits
Last Updated :
16 Dec, 2022
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++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
// Function to check
// if the given number is divisible
// by sum of its digits
string isDivisible(long long int n)
{
long long int temp = n;
// Find sum of digits
int sum = 0;
while (n) {
int k = n % 10;
sum += k;
n /= 10;
}
// check if sum of digits divides n
if (temp % sum == 0)
return "YES";
return "NO";
}
// Driver Code
int main()
{
long long int n = 123;
cout << isDivisible(n);
return 0;
}
Java
// Java implementation of above approach
import java.io.*;
class GFG
{
// Function to check if the
// given number is divisible
// by sum of its digits
static String isDivisible(long n)
{
long temp = n;
// Find sum of digits
int sum = 0;
while (n != 0)
{
int k = (int) n % 10;
sum += k;
n /= 10;
}
// check if sum of digits divides n
if (temp % sum == 0)
return "YES";
return "NO";
}
// Driver Code
public static void main(String []args)
{
long n = 123;
System.out.println(isDivisible(n));
}
}
// This code is contributed by Ryuga
Python3
# Python 3 implementation of above approach
# Function to check if the given number
# is divisible by sum of its digits
def isDivisible(n):
temp = n
# Find sum of digits
sum = 0;
while (n):
k = n % 10;
sum += k;
n /= 10;
# check if sum of digits divides n
if (temp % sum == 0):
return "YES";
return "NO";
# Driver Code
n = 123;
print(isDivisible(n));
# This code is contributed by
# Akanksha Rai
C#
// C# implementation of above approach
using System;
class GFG
{
// Function to check if the
// given number is divisible
// by sum of its digits
static String isDivisible(long n)
{
long temp = n;
// Find sum of digits
int sum = 0;
while (n != 0)
{
int k = (int) n % 10;
sum += k;
n /= 10;
}
// check if sum of digits divides n
if (temp % sum == 0)
return "YES";
return "NO";
}
// Driver Code
public static void Main()
{
long n = 123;
Console.WriteLine(isDivisible(n));
}
}
// This code is contributed by anuj_67..
PHP
<?php
// PHP implementation of above approach
// Function to check if the given number
// is divisible by sum of its digits
function isDivisible($n)
{
$temp = $n;
// Find sum of digits
$sum = 0;
while ($n)
{
$k = $n % 10;
$sum += $k;
$n = (int)($n / 10);
}
// check if sum of digits divides n
if ($temp % $sum == 0)
return "YES";
return "NO";
}
// Driver Code
$n = 123;
print(isDivisible($n));
// This code is contributed
// by chandan_jnu
?>
JavaScript
<script>
// Javascript implementation of above approach
// Function to check if the given number
// is divisible by sum of its digits
function isDivisible(n)
{
temp = n;
// Find sum of digits
sum = 0;
while (n)
{
k = n % 10;
sum += k;
n = parseInt(n / 10);
}
// Check if sum of digits divides n
if (temp % sum == 0)
return "YES";
return "NO";
}
// Driver Code
let n = 123;
document.write(isDivisible(n));
// This code is contributed by sravan kumar
</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++
// C++ implementation of above approach
#include <bits/stdc++.h>
using namespace std;
string getResult(long long int n)
{
// Converting integer to String
string st = std::to_string(n);
// Initialising sum to 0
int sum = 0;
// Traversing through the String
for(char i : st)
{
// Converting character to int
sum = sum + (int) i;
}
// Comparing number and sum
if (n % sum == 0)
return "Yes";
else
return "No";
}
// Driver Code
int main()
{
int n = 123;
// Passing this number to get result function
cout << getResult(n);
return 0;
}
// This code is contributed by 29AjayKumar
Java
// Java implementation of above approach
import java.io.*;
class GFG{
static String getResult(int n)
{
// Converting integer to String
String st = String.valueOf(n);
// Initialising sum to 0
int sum = 0;
// Traversing through the String
for(char i : st.toCharArray())
{
// Converting character to int
sum = sum + (int) i;
}
// Comparing number and sum
if (n % sum == 0)
return "Yes";
else
return "No";
}
// Driver Code
public static void main(String[] args)
{
int n = 123;
// Passing this number to get result function
System.out.println(getResult(n));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python implementation of above approach
def getResult(n):
# Converting integer to string
st = str(n)
# Initialising sum to 0
sum = 0
length = len(st)
# Traversing through the string
for i in st:
# Converting character to int
sum = sum + int(i)
# Comparing number and sum
if (n % sum == 0):
return "Yes"
else:
return "No"
# Driver Code
n = 123
# passing this number to get result function
print(getResult(n))
# this code is contributed by vikkycirus
C#
// C# implementation of above approach
using System;
public class GFG{
static String getResult(int n)
{
// Converting integer to String
String st = String.Join("",n);
// Initialising sum to 0
int sum = 0;
// Traversing through the String
foreach(char i in st.ToCharArray())
{
// Converting character to int
sum = sum + (int) i;
}
// Comparing number and sum
if (n % sum == 0)
return "Yes";
else
return "No";
}
// Driver Code
public static void Main(String[] args)
{
int n = 123;
// Passing this number to get result function
Console.WriteLine(getResult(n));
}
}
// This code is contributed by Amit Katiyar
JavaScript
<script>
// JavaScript implementation of above approach
function getResult(n)
{
// Converting integer to String
let st = (n).toString();
// Initialising sum to 0
let sum = 0;
// Traversing through the String
for(let i of st.split(""))
{
// Converting character to int
sum = sum + parseInt(i);
}
// Comparing number and sum
if (n % sum == 0)
return "Yes";
else
return "No";
}
// Driver Code
let n = 123;
// Passing this number to get result function
document.write(getResult(n)+"<br>");
// This code is contributed by unknown2108
</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.
Similar Reads
Program to check if a number is divisible by any of its digits Given an integer N where 1 \leq n \leq 10^{18} . The task is to check whether the number is not divisible by any of its digit. If the given number N is divisible by any of its digits then print "YES" else print "NO". Examples: Input : N = 5115Output : YESExplanation: 5115 is divisible by both 1 and
10 min read
Check if the sum of digits of number is divisible by all of its digits Given an integer N, the task is to check whether the sum of digits of the given number is divisible by all of its digits or not. If divisible then print Yes else print No. Examples: Input: N = 12 Output: No Sum of digits = 1 + 2 = 3 3 is divisible by 1 but not 2. Input: N = 123 Output: Yes Approach:
14 min read
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. Examples: Input : n = 12Output : YesExplanation: Sum of digits = 1+2 =3 and 3 divides 12.Input : n = 15Output : NoExplanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.Using Iterative Method - O(
6 min read
Check whether sum of digits at odd places of a number is divisible by K Given two integers 'N' and 'K', the task is to find the sum of digits of 'N' at its odd places (right to left) and check whether the sum is divisible by 'K'. If it is divisible, output YES, otherwise output NO. Examples: Input: N = 4325, K = 4 Output: YES Since, 3 + 5 = 8, which is divisible by 4. I
7 min read
Check if all digits of a number divide it Given a number n, find whether all digits of n divide it or not.Examples: Input : 128 Output : Yes 128 % 1 == 0, 128 % 2 == 0, and 128 % 8 == 0. Input : 130 Output : No We want to test whether each digit is non-zero and divides the number. For example, with 128, we want to test d != 0 && 128
9 min read