i found this while attempting CP Question and i got curious about it then i thought i should share this topic to everyone so here it is...

What is Digital root? if you have done little competitive programming or practice for Data Structure or anything else you have got problem where you need to sum all digits of number and doing this process until number became single digit number. so that is Digital Root , let's talk about it.

Example:

123 = 1+2+3 => 6

456 = 4+5+6 => 15 = 1+5 => 6

There are Few solution which can solve this Problem like Brute Force, but that will slow so we'll try mathematical technique to solve it which is digital root.

Before solving this problem ,let me tell you some interesting properties of Number **9**:

when you multiply any number(>0) with 9 then sum of all digits of that number will always 9 . Example : 345*9 = 3105 => 3+1+0+5 => 9 , you can check yourself this condition (try different numbers)

When we add 9 to any number(here add means concatenating ) sum of all digits won't get effected, Example : 345 => 3+4+5 => 12 => 1+2 => 3, now add 9 , 3459 => 21 => 2+1=>3

when we divide any number by 9 , then what remainder we get is same as digital root of that number. Example : 15/9 => 6 and 15=> 1+5=>6

Any Perfect Square's digital root will be 1,4,7,9 (16=>1+6=>7, 36=>3+6=>9 etc)

First i suggest to check out this Leetcode Question

So for n==0 digital_root = 0

otherwise digital_root = 1 + ((n-1)%9)

Let's code this

C++

```
#include <iostream>
using namespace std;
int digital_root(int n){
if (n==0)return 0;
else{
return 1 + ((n-1)%9);
}
}
int main() {
int n;
cout << "Enter Number: ";
cin >> n;
cout <<endl;
cout << "Digital Root Value :" << digital_root(n);
return 0;
}
```

Python3

```
def digital_root(n):
if n==0:
return 0
else:
return 1 + ((n-1)%9)
n = int(input("Enter Number: "))
print("Digital Root :",digital_root(n))
```

If you want to Know more about it then check Digital_Root

Open for Suggestion

Thank You

## Top comments (0)