How to sum a given number into single digit using nested while loop in python?

Description

To sum a given number into single digit using python3.

  • Get the input from user.
  • Initialize nested for loop.
  • Use % and // operators for
    separating and round off the
    input value.
  • Print the result.

#get the input from user
n=int(input(“Enter the number:”))

#declare two variable to store final result
rev=0
rev1=0

#initialize first while loop
while(n>0):
r=n%10
rev=rev+r
n=n//10
#initialize second while loop
while(rev>0):
s=rev%10
rev1=rev1+s
rev=rev//10

#print the result
print(“sum of given number is:”,rev1)

Leave Comment

Your email address will not be published. Required fields are marked *

clear formSubmit