- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to find sum of digits until it is one digit number in Python
Suppose we have a positive number n, we will add all of its digits to get a new number. Now repeat this operation until it is less than 10.
So, if the input is like 9625, then the output will be 4.
To solve this, we will follow these steps −
- Define a method solve(), this will take n
- if n < 10, then
- return n
- s := 0
- l := the floor of (log(n) base 10 + 1)
- while l > 0, do
- s := s + (n mod 10)
- n := quotient of n / 10
- l := l - 1
- return solve(s)
Let us see the following implementation to get better understanding −
Example
import math class Solution: def solve(self, n): if n < 10: return n s = 0 l = math.floor(math.log(n, 10) + 1) while l > 0: s += n % 10 n //= 10 l -= 1 return self.solve(s) ob = Solution() print(ob.solve(9625))
Input
9625
Output
4
- Related Articles
- C++ program to find sum of digits of a number until sum becomes single digit
- Sum up a number until it becomes one digit - JavaScript
- Summing up all the digits of a number until the sum is one digit in JavaScript
- Finding sum of digits of a number until sum becomes single digit in C++
- Maximum of sum and product of digits until number is reduced to a single digit in C++
- C program to find sum of digits of a five digit number
- Sum up a number until it becomes 1 digit JavaScript
- Reduce sum of digits recursively down to a one-digit number JavaScript
- Program to find the sum of all digits of given number in Python
- Program to find minimum digits sum of deleted digits in Python
- Python Program to Find the Sum of Digits in a Number without Recursion
- Program to find sum of digits that are inside one alphanumeric string in Python
- The sum of the digit of a 2 digit number is 6 . On reversing it's digits, the number is 18 less than the original number find the number
- The sum of digits of a two-digit number is 8. If 36 is added to the number then the digits reversed. Find the number.
- Sum of the digits of a two - digit number is 9 . When we interchange the digits, it is found that the resulting interchange the digits, it is found that resulting number is greater than original number by 27. What us two digit number?

Advertisements