Answer to Question #178674 in Python for Woyengimiesindo Malati

Question #178674

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


1
Expert's answer
2021-04-06T15:57:13-0400
""" 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

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS