This is a Python Program to count the number of digits in a number.
The program takes the number and prints the number of digits in the number.
1. Take the value of the integer and store in a variable.
2. Using a while loop, get each digit of the number and increment the count each time a digit is obtained.
3. Print the number of digits in the given integer.
Here is source code of the Python Program to count the number of digits in a number. The program output is also shown below.
n=int(input("Enter number:")) count=0 while(n>0): count=count+1 n=n//10 print("The number of digits in the number are:",count)
1. User must first enter the value of the integer and store it in a variable.
2. The while loop is used and the last digit of the number is obtained by using the modulus operator.
3. Each time a digit is obtained, the count value is incremented.
4. This loop terminates when the value of the number is 0.
5. The total count of the number of digits is printed.
Case 1: Enter number:123 The number of digits in the number are: 3 Case 2: Enter number:1892 The number of digits in the number are: 4
Sanfoundry Global Education & Learning Series – Python Programs.
To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.