Problem Statement:
In this Single Digit Number in Python problem, we are given a number, we need to add its digits, and we need to repeat this until the addition results in a single digit. At last, we need to print the result. For example, 78=> 7+8=>15=>1+5=>6.
Code for Single Digit Number in Python:
import math
n = 78
while True:
if 1<=n<=9:
print(n)
break
else:
one = n%10
two = math.floor(n/10)
n = one + two
Output:
Explanation:
We imported the math module, declared n, and assigned 78 to it. Then inside it, we first check if n is between 1 and 9, we will print it else we will add it’s both digits. Our output results in 6 because 78=> 7+8=>15=>1+5=>6.
Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- Team Points in Python
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Split the sentence in Python
- String Slicing in JavaScript
- First and Last Digits in Python | Assignment Expert
- List Indexing in Python
- Date Format in Python | Assignment Expert
- New Year Countdown in Python
- Add Two Polynomials in Python
- Sum of even numbers in Python | Assignment Expert
- Evens and Odds in Python
- A Game of Letters in Python
- Sum of non-primes in Python
- Smallest Missing Number in Python
- String Rotation in Python
- Secret Message in Python
- Word Mix in Python
- Single Digit Number in Python
- Shift Numbers in Python | Assignment Expert
- Weekend in Python
- Shift Numbers in Python | Assignment Expert
- Temperature Conversion in Python
- Special Characters in Python
- Sum of Prime Numbers in the Input in Python