
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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
- if n is same as 0, then
- 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
Advertisements