The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root. If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
Given a number, the task is to find its digital root. The input number may be large and it may not be possible to store even if we use long long int.
Asked in ACM-ICPC
Input : num = "1234" Output : 1 Explanation : The sum of 1+2+3+4 = 10, digSum(x) == 10 Hence ans will be 1+0 = 1 Input : num = "5674" Output : 4
We have discussed a solution for numbers that can fit in long long it in below post.
Finding sum of digits of a number until sum becomes single digit
In this post, similar approaches are discussed for large numbers.
Find Digital root of 65785412
- Find out all the digits of a number
- Add all the number one by one
- If the final sum is double digit, add again to make it single digit
- The result obtained in single digit is the Digital root of number
Find Digital root: (6 + 5 + 7 + 8 + 5 + 4 + 1 + 2) = 38 => 11 => (1 + 1) = 2
The idea is based on the fact that for a non-zero number num, digital root is 9 if number is divisible by 9, else digital root is num % 9. (Please see http://www.sjsu.edu/faculty/watkins/Digitsum0.htm for details)
Find the digital root of 65785412
- Sum of digits = 6 + 5 + 7 + 8 + 5 + 4 + 1 + 2 = 38
- Since 38 is not multiple of 9, digital root is 38 % 9 = 2.
This article is contributed by Shiv Pratap Singh. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Digital Root of a given large integer using Recursion
- Smallest root of the equation x^2 + s(x)*x - n = 0, where s(x) is the sum of digits of root x.
- Square root of a number by Repeated Subtraction method
- Multiply large integers under large modulo
- Numbers in a Range with given Digital Root
- Square root of an integer
- Sudo Placement[1.7] | Greatest Digital Root
- Print a number containing K digits with digital root D
- Find Nth positive number whose digital root is X
- Count ways to obtain given sum by repeated throws of a dice
- Cost required to empty a given array by repeated removal of maximum obtained by given operations
- Minimum decrements to make integer A divisible by integer B
- Find the maximum sum (a+b) for a given input integer N satisfying the given condition
- Recursive sum of digits of a number formed by repeated appends
- Find M-th number whose repeated sum of digits of a number is N
- Repeated sum of first N natural numbers
- Remove repeated digits in a given number
- Sum of two large numbers
- Check if a large number can be divided into two or more segments of equal sum
- Recursive sum of digit in n^x, where n and x are very large