Python-Count Number of Digits in An Integer


Write a program to count the number of digits in an integer.

#To count the number of digits in a number
#E.g., Input 2387, output : 4 digits
#input 23986, output 5 digits

num = int(input('Enter a number '))

ctr = 0   # to keep a count of digits in the given number

while num > 0:
      r = num % 10   #% operator gives remainder after division by 10
      ctr = ctr + 1
      num = num // 10 #truncate the given number as last digit is used
#EOWhileLoop  This comment helps to identify the end of a loop
      
print ("There are ", ctr, " digits in that number")
#EndOfProgram
Advertisements
%d bloggers like this: