Open In App

Check if the sum of digits of a number divides it

Last Updated : 07 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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 = 12
Output : Yes
Explanation: Sum of digits = 1+2 =3 and 3 divides 12.

Input : n = 15
Output : No
Explanation: Sum of digits = 1+5 =6 and 15 % 6 != 0.

Using Iterative Method - O(log n) time and O(1) space

The idea is to iterate through each digit of the number by repeatedly dividing by 10 and taking the remainder, summing these digits, and then checking if the original number is divisible by this sum. Please refer Sum of Digits of a Number for details.

C++
// C++ program to Check if the sum 
// of digits of a number N divides it
#include <iostream>
using namespace std;

bool isDivisible(int n) {
    int sum = 0;
    int temp = n;
    
    // Calculate sum of digits
    while (temp > 0) {
        sum += temp % 10;
        temp /= 10;
    }
    
    // Check if n is divisible by sum
    return (sum != 0 && n % sum == 0);
}

int main() {
    int n = 12;
    if (isDivisible(n)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
    return 0;
}
Java
// Java program to Check if the sum 
// of digits of a number N divides it

class GfG {

    static boolean isDivisible(int n) {
        int sum = 0;
        int temp = n;
        
        // Calculate sum of digits
        while (temp > 0) {
            sum += temp % 10;
            temp /= 10;
        }
        
        // Check if n is divisible by sum
        return (sum != 0 && n % sum == 0);
    }

    public static void main(String[] args) {
        int n = 12;
        if (isDivisible(n)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}
Python
# Python program to Check if the sum 
# of digits of a number N divides it

def isDivisible(n):
    sum = 0
    temp = n
    
    # Calculate sum of digits
    while temp > 0:
        sum += temp % 10
        temp //= 10
    
    # Check if n is divisible by sum
    return (sum != 0 and n % sum == 0)

if __name__ == "__main__":
    n = 12
    if isDivisible(n):
        print("Yes")
    else:
        print("No")
C#
// C# program to Check if the sum 
// of digits of a number N divides it

using System;

class GfG {

    static bool isDivisible(int n) {
        int sum = 0;
        int temp = n;
        
        // Calculate sum of digits
        while (temp > 0) {
            sum += temp % 10;
            temp /= 10;
        }
        
        // Check if n is divisible by sum
        return (sum != 0 && n % sum == 0);
    }

    static void Main(string[] args) {
        int n = 12;
        if (isDivisible(n)) {
            Console.WriteLine("Yes");
        } else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to Check if the sum 
// of digits of a number N divides it

function isDivisible(n) {
    let sum = 0;
    let temp = n;
    
    // Calculate sum of digits
    while (temp > 0) {
        sum += temp % 10;
        temp = Math.floor(temp / 10);
    }
    
    // Check if n is divisible by sum
    return (sum !== 0 && n % sum === 0);
}

let n = 12;
if (isDivisible(n)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Using Recursion - O(log n) time and O(log n) space

The idea is to calculate the sum of digits recursively by treating the problem as a smaller subproblem where we add the last digit to the sum of remaining digits, and then check if the original number is divisible by this sum.

C++
// C++ program to Check if the sum 
// of digits of a number N divides it
#include <iostream>
using namespace std;

// Recursive function to find sum of digits
int getDigitSum(int num) {
    
    // Base case
    if (num == 0)
        return 0;
    
    // Add last digit to sum of remaining digits
    return (num % 10) + getDigitSum(num / 10);
}

bool isDivisible(int n) {
    
    // Calculate sum of digits
    int sum = getDigitSum(n);
    
    // Check if n is divisible by sum
    return (sum != 0 && n % sum == 0);
}

int main() {
    int n = 12;
    if (isDivisible(n)) {
        cout << "Yes" << endl;
    }
    else {
        cout << "No" << endl;
    }
    return 0;
}
Java
// Java program to Check if the sum 
// of digits of a number N divides it

class GfG {

    // Recursive function to find sum of digits
    static int getDigitSum(int num) {
        
        // Base case
        if (num == 0)
            return 0;
        
        // Add last digit to sum of remaining digits
        return (num % 10) + getDigitSum(num / 10);
    }

    static boolean isDivisible(int n) {
        
        // Calculate sum of digits
        int sum = getDigitSum(n);
        
        // Check if n is divisible by sum
        return (sum != 0 && n % sum == 0);
    }

    public static void main(String[] args) {
        int n = 12;
        if (isDivisible(n)) {
            System.out.println("Yes");
        } else {
            System.out.println("No");
        }
    }
}
Python
# Python program to Check if the sum 
# of digits of a number N divides it

# Recursive function to find sum of digits
def getDigitSum(num):
    
    # Base case
    if num == 0:
        return 0
    
    # Add last digit to sum of remaining digits
    return (num % 10) + getDigitSum(num // 10)

def isDivisible(n):
    
    # Calculate sum of digits
    sum = getDigitSum(n)
    
    # Check if n is divisible by sum
    return (sum != 0 and n % sum == 0)

if __name__ == "__main__":
    n = 12
    if isDivisible(n):
        print("Yes")
    else:
        print("No")
C#
// C# program to Check if the sum 
// of digits of a number N divides it

using System;

class GfG {

    // Recursive function to find sum of digits
    static int getDigitSum(int num) {
        
        // Base case
        if (num == 0)
            return 0;
        
        // Add last digit to sum of remaining digits
        return (num % 10) + getDigitSum(num / 10);
    }

    static bool isDivisible(int n) {
        
        // Calculate sum of digits
        int sum = getDigitSum(n);
        
        // Check if n is divisible by sum
        return (sum != 0 && n % sum == 0);
    }

    static void Main(string[] args) {
        int n = 12;
        if (isDivisible(n)) {
            Console.WriteLine("Yes");
        } else {
            Console.WriteLine("No");
        }
    }
}
JavaScript
// JavaScript program to Check if the sum 
// of digits of a number N divides it

// Recursive function to find sum of digits
function getDigitSum(num) {
    
    // Base case
    if (num === 0)
        return 0;
    
    // Add last digit to sum of remaining digits
    return (num % 10) + getDigitSum(Math.floor(num / 10));
}

function isDivisible(n) {
    
    // Calculate sum of digits
    let sum = getDigitSum(n);
    
    // Check if n is divisible by sum
    return (sum !== 0 && n % sum === 0);
}

let n = 12;
if (isDivisible(n)) {
    console.log("Yes");
} else {
    console.log("No");
}

Output
Yes

Similar Reads