Digital Root

Last Updated : 27 Jun, 2026

Given a number n, find the digital root of n. Digital Root of a number is the recursive sum of its digits until we get a single digit number.

Examples :

Input: n = 1
Output:  1
Explanation: Digital root of 1 is 1.

Input: n = 99999
Output: 9
Explanation: The sum of digits of 99999 is 45 which is not a single digit number, hence the sum of digits of 45 is 9 which is a single digit number.

Try It Yourself
redirect icon

[Naive Approach] Repetitively Adding Digits -  O(d) Time and O(1) Space

The approach computes the digital root of a number by repeatedly summing its digits until a single digit value is obtained. It starts by adding all the digits of the given number; if the result is a single digit, it is returned. Otherwise, the same process is repeated on the new sum until a single digit result is reached.

C++
#include <iostream>
using namespace std;

int digitalRoot(int n)
{
    int res = 0;

    // Repetitively calculate sum until
    // it becomes single digit
    while (n > 0 || res > 9)
    {

        // If n becomes 0, reset it to res
        // and start a new iteration.
        if (n == 0)
        {
            n = res;
            res = 0;
        }

        res += n % 10;
        n /= 10;
    }

    return res;
}

int main()
{
    int n = 99999;

    cout << digitalRoot(n);

    return 0;
}
Java
import java.util.Scanner;

public class GFG {
    public static int digitalRoot(int n)
    {
        int res = 0;

        // Repetitively calculate sum until
        // it becomes single digit
        while (n > 0 || res > 9) {

            // If n becomes 0, reset it to res
            // and start a new iteration.
            if (n == 0) {
                n = res;
                res = 0;
            }

            res += n % 10;
            n /= 10;
        }

        return res;
    }

    public static void main(String[] args)
    {
        int n = 99999;

        System.out.println(digitalRoot(n));
    }
}
Python
def digitalRoot(n):
    res = 0

    # Repetitively calculate sum until
    # it becomes single digit
    while n > 0 or res > 9:

        # If n becomes 0, reset it to res
        # and start a new iteration.
        if n == 0:
            n = res
            res = 0

        res += n % 10
        n //= 10

    return res

if __name__ == "__main__":
    n = 99999
    print(digitalRoot(n))
C#
public class GFG {
    public int digitalRoot(int n)
    {
        int res = 0;

        while (n > 0 || res > 9) {
            if (n == 0) {
                n = res;
                res = 0;
            }

            res += n % 10;
            n /= 10;
        }

        return res;
    }

    public static int Main()
    {
        int n = 99999;

        Console.WriteLine(digitalRoot(n));

        return 0;
    }
}
JavaScript
function digitalRoot(n)
{
    let res = 0;

    // Repetitively calculate sum until
    // it becomes single digit
    while (n > 0 || res > 9) {

        // If n becomes 0, reset it to res
        // and start a new iteration.
        if (n === 0) {
            n = res;
            res = 0;
        }

        res += n % 10;
        n = Math.floor(n / 10);
    }

    return res;
}

// Driver Code
let n = 99999;

console.log(digitalRoot(n));

Output
9

Time Complexity: O(d), where d is the number of digits in n.
Auxiliary Space: O(1)

[Expected Approach] Using Mathematical Formula - O(1) Time O(1) Space

This approach computes the digital root using a mathematical property instead of repeatedly summing the digits. If the number is 0, the digital root is 0. Otherwise, the digital root is equal to the remainder when the number is divided by 9; however, if the remainder is 0, the digital root is 9 because every non-zero multiple of 9 has a digital root of 9.

  • A number and the sum of its digits always leave the same remainder when divided by 9. For example, 5725 + 7 + 2 = 141 + 4 = 5, and both 572 and 5 have the same remainder modulo 9.
  • Repeatedly summing the digits (finding the digital root) does not change this remainder.
  • Therefore, for any non-zero number, the digital root is simply n % 9.
  • The only exception is when n is a non-zero multiple of 9. In this case, n % 9 is 0, but the digital root is 9, so the function returns 9.
  • If n is 0, its digital root is also 0, which is handled separately.
C++
#include <iostream>
using namespace std;

int digitalRoot(int n)
{

    // If given number is zero its
    // digit sum will be zero only
    if (n == 0)
        return 0;

    // If result of modulo operation is
    // zero then, the digit sum is 9
    if (n % 9 == 0)
        return 9;

    return (n % 9);
}

int main()
{
    int n = 99999;
    cout << digitalRoot(n);
    return 0;
}
C
#include <stdio.h>

int digitalRoot(int n)
{
    // If given number is zero its
    // digit sum will be zero only
    if (n == 0)
        return 0;

    // If result of modulo operation is
    // zero then, the digit sum is 9
    if (n % 9 == 0)
        return 9;

    return (n % 9);
}

int main()
{
    int n = 99999;
    printf("%d", digitalRoot(n));
    return 0;
}
Java
public class GFG {
    public static int digitalRoot(int n)
    {
        // If given number is zero its
        // digit sum will be zero only
        if (n == 0)
            return 0;

        // If result of modulo operation is
        // zero then, the digit sum is 9
        if (n % 9 == 0)
            return 9;

        return (n % 9);
    }

    public static void main(String[] args)
    {
        int n = 99999;
        System.out.println(digitalRoot(n));
    }
}
Python
def digitalRoot(n):
    # If given number is zero its
    # digit sum will be zero only
    if n == 0:
        return 0

    # If result of modulo operation is
    # zero then, the digit sum is 9
    if n % 9 == 0:
        return 9

    return n % 9


if __name__ == "__main__":
    n = 99999
    print(digitalRoot(n))
C#
public class GFG {
    public int digitalRoot(int n)
    {
        return 1 + (n - 1) % 9;
    }

    public static void Main()
    {
        int n = 99999;
        Console.WriteLine(digitalRoot(n));
    }
}
JavaScript
function digitalRoot(n)
{
    // If given number is zero its
    // digit sum will be zero only
    if (n === 0)
        return 0;

    // If result of modulo operation is
    // zero then, the digit sum is 9
    if (n % 9 === 0)
        return 9;

    return (n % 9);
}
// Driver code
let n = 99999;
console.log(digitalRoot(n));

Output
9
Comment