Single Digit Number in Python

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:

Single Digit Number in Python | Assignment Expert

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:

Share:

Author: Harry

Hello friends, thanks for visiting my website. I am a Python programmer. I, with some other members, write blogs on this website based on Python and Programming. We are still in the growing phase that's why the website design is not so good and there are many other things that need to be corrected in this website but I hope all these things will happen someday. But, till then we will not stop ourselves from uploading more amazing articles. If you want to join us or have any queries, you can mail me at admin@copyassignment.com Thank you