Sovi.AI - AI Math Tutor

Scan to solve math questions

QUESTION IMAGE

digit count by codechum admin write a program that takes an integer num…

Question

digit count
by codechum admin
write a program that takes an integer number. use a while
loop to count the number of digits in the entered number and
print it.
sample output 1
enter a positive integer: 232512
number of digits: 6
sample output 2
enter a positive integer: 21258
number of digits: 5
sample output 3
enter a positive integer: 56767
number of digits: 5

Explanation:

Step1: Get user input

num = int(input("Enter a positive integer: "))

Step2: Initialize digit count

count = 0

Step3: Use while loop to count digits

while num > 0:
    num = num // 10
    count += 1

Step4: Print the result

print(f"Number of digits: {count}")

Answer:

num = int(input("Enter a positive integer: "))
count = 0
while num > 0:
    num = num // 10
    count += 1
print(f"Number of digits: {count}")