DEV Community

Discussion on: Daily Challenge #264 - Digital Root

Collapse
 
ayanb profile image
Ayan Banerjee • Edited

Property of the digital root is that the result would be n % 9 and n % 9 == 0 it will be 9.

int digitalRoot(int n) {
    if (n == 0) return 0;
    if (n % 9 == 0) return 9;
    return n % 9;
}