Digital Root (repeated digital sum) of the given large integer (original) (raw)

Last Updated : 2 Jun, 2026

You are given a number n, you need to find the digital root of n.

**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 Yourselfredirect icon

Table of Content

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

The approach is focused on calculating the **digital root of a number, which is the result of summing the digits repeatedly until a single-digit value is obtained. Here's how it works conceptually:

  1. **Sum the digits: Start by adding all the digits of the given number.
  2. **Check the result: If the sum is a single-digit number (i.e., less than 10), stop and return it.
  3. **Repeat the process: If the sum is still more than a single digit, repeat the process with the sum of digits. This continues until a single-digit sum is reached.

**Example:

For a number like 1234

#include using namespace std;

// Function to find the digital root 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;

}

// Driver Code int main() { int n = 99999;

cout << digitalRoot(n);

return 0;

}

Java

import java.util.Scanner;

// Function to find the digital root 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;
}

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

    System.out.println(digitalRoot(n));
}

}

Python

""" Function to find the digital root """

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

Driver Code

if name == "main": n = 99999 print(digitalRoot(n))

C#

using System;

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;

    GfG obj = new GfG();
    Console.WriteLine(obj.digitalRoot(n));

    return 0;
}

}

JavaScript

// Function to find the digital root 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));

`

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

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

We know that every number in the decimal system can be expressed as a sum of its digits multiplied by powers of 10. For example, a number represented as **abcd can be written as follows:

abcd = a * 10 ^ 3 + b * 10 ^ 2 + c * 10 ^ 1 + d * 10 ^ 0

We can separate the digits and rewrite this as:
abcd = a + b + c + d + (a * 999 + b * 99 + c * 9)
abcd = a + b + c + d + 9 * (a * 111 + b * 11 + c)

This implies that any number can be expressed as the sum of its digits plus a multiple of 9.
So, if we take modulo with 9 on both side,
abcd % 9 = (a + b + c + d) % 9 + 0

This means that the remainder when abcd is divided by 9 is equal to the remainder where the sum of its digits (a + b + c + d) is divided by 9.

If the sum of the digits itself consists of more than one digit, we can further express this sum as the sum of its digits plus a multiple of 9. Consequently, taking modulo 9 will eliminate the multiple of 9 until the sum of digits becomes a single-digit number.

As a result, the digital root of a number can be found using modulo 9. If the result of the modulo operation is zero for a non-zero number, then the digital root is 9.

#include 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#

using System;

public class Gfg { public int digitalRoot(int n) { return 1 + (n - 1) % 9; }

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

    Gfg obj = new Gfg();
    Console.WriteLine(obj.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);

}

let n = 99999; console.log(digitalRoot(n));

`

**Time Complexity: O(1)
**Auxiliary Space: O(1)