Write a program that reads an integer and prints how many digits the number has, by
checking whether the number is ≥ 10, ≥ 100, and so on. (Assume that all integers are
less than ten billion.) If the number is negative, first multiply it with –1. Sample runs of the program are given below:
Enter an integer less than 10 billion: 576838
Digits: 6
Enter an integer less than 10 billion: 100000000000
Number is out of range
Enter an integer less than 10 billion: 7645362758
Digits: 10
""" Program to read an integer and print how many digits the number has.
"""
# get user input
number = int(input('Enter an integer less than 10 billion: '))
limit = 10
digits = 1
if number < 0:
number *= -1
while True:
if number < limit:
print('Digits:', digits)
break
# Any non-negative integer less than 10 has 1 digit, less than 100 has 2 digits, and so on.
limit *= 10
digits += 1
Comments
Leave a comment