Python Program to Count the Number of Digits Present In a Number Ad Sponsored

ⓘ Sponsored by

Counting the number of digits present in a number involves determining the total number of digits in the given number. For example, if the input number is 12345, the output would be 5, since the number has five digits.

Python Code :

The Below Python program counts the number of digits present in a number:

number = 12345
count = 0

while number != 0:
    number //= 10
    count += 1

print(f'Number of digits in the number: {count}')

In this program, we first define a number (in this example, 12345). We then define a variable called count and initialize it to zero. This variable will hold the number of digits in the number.

We use a while loop to count the number of digits. Inside the loop, we divide the number by 10 using integer division (//) to remove the last digit. We then increment the count variable by 1.

We continue this process until the number becomes zero, at which point we have counted all the digits.

Finally, we print the number of digits using the print() function.

Note that this program assumes that the input number is a positive integer. If you want to count the digits in a negative integer or a floating-point number, you will need to modify the program accordingly. Also, the program will not work if the input number contains leading zeros. Ad Sponsored

ⓘ Sponsored by