Count the digits in the number
num = int(input("Enter a number: ")) # Input the number
count = 0 while num != 0: # Loop until the number becomes zero num //= 10 # Divide the number by 10 to remove the rightmost digit count += 1 # Increment the count of digits print("Number of digits:", count) In this program, we first take the input from the user using the `input()` function and convert it to an integer using `int()`. Then we initialize a variable `count` to keep track of the number of digits.Next, we use a `while` loop that continues until the number becomes zero. In each iteration of the loop, we divide the number by 10 using the `//=` operator to remove the rightmost digit. We also increment the count by 1 in each iteration.
Finally, we print the value of the `count` variable, which represents the number of digits in the number.
Comments
Post a Comment