Python Program to Count Number of Digits in a Number

Write a Python Program to Count the Number of Digits in a Number using the While Loop, Functions, and Recursion. Here, we have to find out the total number of individual digits in a given number. For example, if the user enters 112, the program should return 3.

Python Program to Count Number of Digits in a Number using While Loop

This Python program allows the user to enter any positive integer. Then it divides the given number into individual digits and counts those individual digits using While Loop.

# using While loop

Number = int(input("Please Enter any Number: "))
Count = 0
while(Number > 0):
    Number = Number // 10
    Count = Count + 1

print("\n Number of Digits in a Given Number = %d" %Count)
Python Program to Count Number of Digits in a Number using While Loop

This program allows the user to enter any positive integer. Then that number is assigned to the Number variable. Next, Condition in the Python While Loop make sure that the given number is greater than 0 (Means Positive integer and greater than 0)

In this Python Count Digits in a Number, the user Entered the value: Number = 9875 and Count = 0

First Iteration

Number = Number // 10 => 9875 //10
Number = 987

Count = Count + 1 => 0 + 1
Count = 1

Second Iteration: From the first Python Iteration, the values of both changed as Number = 987 and Count = 1.

Number = 987 // 10
Number = 98

Count = 1 + 1
Count = 2

Third Iteration: From the second Iteration of the Program to Count the Number of Digits in a Number, Number = 98 and Count = 2. Refer to the Python Arithmetic Operators.

Number = 98 // 10
Number = 9

Count = 2 + 1
Count = 3

Fourth Iteration: For the Fourth Iteration, Number = 9 and Count = 3

Number = 9 // 10
Number = 0

Count = 3 + 1
Count = 4

Here Number = 0. So, the condition present in a while loop fails.

The last print statement prints the number of digits present in the given number using the Count variable as the output. So, the output of the given variable 9875 is 4.

Python Program to Count Number of Digits in a Number Using Functions

This Python program allows the user to enter any positive integer. Then it divides the given number into individual digits and counts those individual digits using Functions. Refer to the Python for loop.

# using Functions

def Counting(Number):
    Count = 0
    while(Number > 0):
        Number = Number // 10
        Count = Count + 1
    print("\n Number of Digits in a Given Number = %d" %Count)

Counting(1234)

OR

def Counting(Number):
    Count = 0
    while(Number > 0):
        Number = Number // 10
        Count = Count + 1
    return Count

Number = int(input("Please Enter any Number: "))
Count = Counting(Number)
print("\n Number of Digits in a Given Number = %d" %Count)
Please Enter any Number: 123456789

 Number of Digits in a Given Number = 9

When it reaches to Counting (Number) line program, then the compiler immediately jumps to the below function:

def Counting(Number):

The last line ends with a return Count Statement.

Python Program to Count Number of Digits in a Number using Recursion

This program divides the given number into individual digits and counts those individual digits using Recursion.

Count = 0
def Counting(Number):
    global Count
    if(Number > 0):
        Count = Count + 1
        Counting(Number//10)
    return Count

Number = int(input("Please Enter any Number: "))
Count = Counting(Number)
print("\n Number of Digits in a Given Number = %d" %Count)
Please Enter any Number: 12345

 Number of Digits in a Given Number = 5

In the Counting (Number) function definition, the below statement helps to call the function Recursively with an updated value.

Counting(Number//10)

If you miss this statement, after completing the first line, it terminates. For example,

Number = 1234

Then the output is 1.

Count Digits in a Number Using len()

If we convert the given number to a string, we can use the built-in len() function to get the string length (total digits in a number). Here, str() converts the given number to a string in Python.

TIP: Please refer to the Python len function.

n = int(input('Enter any Number = '))
print(len(str(n)))
Enter any Number = 34567
5

Use log10() and floor() to count digits in a number

In the program below, we used the built-in math. log10 function to count the number of digits in a number. As the Python log10 function works with positive numbers, we must use Python abs() to convert negative numbers to positive. It also does not accept 0; we must handle zero.

import math
def digitsCount(num):
p = abs(num)
if p == 0:
return 1
else:
return math.floor(math.log10(p)) + 1

n = int(input('Enter any Number = '))
print(digitsCount(n))
Enter any Number = 9767012
7

Count total digits in a number using a for loop

A while loop is the ideal iterative approach. However, the following program shows how to use a for loop to count the total number of digits in a number. Here, the if statement with break is important to exit the control from the loop.

num = int(input("Enter Number = "))
cnt = 0
for i in range(num):
num //= 10
cnt += 1
if num == 0:
break

print('The Total Number of Digits = ', cnt)
Enter Number = 56123
The Total Number of Digits =  5

Also Read