Python Program to Count Number of Digits in a Number

Write a Python Program to Count Number of Digits in a Number using While Loop, Functions, and Recursion.

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 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 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 the Number and Count 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 Python Program to Count Number of Digits in a Number, Number = 98 and Count = 2

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.

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.

# 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 jump to below function:

def Counting(Number):

The last line ends with a return Count Statement.

Using Recursion

This Python 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 help 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.