Program to find super digit of a number in Python


Suppose we have a number n. We have to find the super digit of this number. The super digit of a single digit number is the digit itself but for multi-digit numbers super digit is the sum of all digits repeatedly until the sum is a single digit number.

So, if the input is like n = 513682, then the output will be 7 because (5+1+3+6+8+2) = 25, (2 + 5) = 7.

To solve this, we will follow these steps −

  • s := 0
  • while n > 0 or s > 9, do
    • if n is same as 0, then
      • n := s
      • s := 0
    • s := s + n mod 10
    • n := floor value of n/10
  • return s

Example

Let us see the following implementation to get better understanding −

def solve(n):
   s = 0
   while(n > 0 or s > 9):
      if n == 0:
         n = s
         s = 0

      s += n % 10
      n //= 10

   return s

n = 513682
print(solve(n))

Input

513682

Output

7

Updated on: 12-Oct-2021

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements