Q:

Write a python program to count the total number of digits in a number using a while loop

belongs to collection: Python Loop Exercises

0

Count the total number of digits in a number

Write a program to count the total number of digits in a number using a while loop.

For example, the number is 75869, so the output should be 5.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Hint:

  • Set counter = 0
  • Run while loop till number != 0
  • In each iteration of loop
    • Reduce the last digit from the number using floor division ( number = number // 10)
    • Increment counter by 1
  • print counter

Solution:

num = 75869
count = 0
while num != 0:
    # floor division
    # to reduce the last digit from number
    num = num // 10

    # increment counter by 1
    count = count + 1
print("Total digits are:", count)

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a python program to use for loop to print th... >>
<< Display numbers from a list using loop using pytho...