In this article we will show you, how to 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 and then it will divide the given number into individual digits and count those individual digits using Python While Loop.

**PYTHON CODE**

# Python Program to Count Number of Digits in a Number 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)

**OUTPUT**

**ANALYSIS**

This program allows the user to enter any positive integer and then, that number is assigned to the Number variable.

Next, Condition in the While loop will make sure that, the given number is greater than 0 (Means Positive integer and greater than 0)

User Entered value: Number = 9875 and Count = 0

**First Iteration**

Number = Number // 10

Number = 9875 //10

Number = 987

Count = Count + 1

Count = 0 + 1

Count = 1

**Second Iteration**

From the first Iteration the values of both Number and Count has been changed as: Number = 987 and Count = 1

Number = Number // 10

Number = 987 // 10

Number = 98

Count = Count + 1

Count = 1 + 1

Count = 2

**Third Iteration**

From the Third Iteration the values of both Number and Count has been changed as: Number = 98 and Count = 2

Number = Number // 10

Number = 98 // 10

Number = 9

Count = Count + 1

Count = 2 + 1

Count = 3

**Fourth Iteration**

From the Fourth Iteration the values of both Number and Count has been changed as: Number = 9 and Count = 3

Number = Number // 10

Number = 9 // 10

Number = 0

Count = Count + 1

Count = 3 + 1

Count = 4

**Here** Number = 0 so, the condition present in the while loop will fail

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

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

This program allows the user to enter any positive integer and then it will divide the given number into individual digits and counting those individual digits using Functions.

**PYTHON CODE**

# Python Program to Count Number of Digits in a Number 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

# Python Program to Count Number of Digits in a Number using Functions 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)

**OUTPUT**

**ANALYSIS**

When the compiler reaches to Counting (Number) line python program then the compiler will immediately jump to below function:

def Counting(Number):

We already explained LOGIC in above example. Please refer Python Program to Find Count of the Digits of a Given Number using While Loop Analysis section.

Last line ends with return Count Statement.

## Python Program to Count Number of Digits in a Number Using Recursion

This program allows the user to enter any positive integer and then it will divide the given number into individual digits and count those individual digits using Recursion.

**PYTHON CODE**

# Python Program to Count Number of Digits in a Number 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)

**OUTPUT**

**ANALYSIS**

In the Counting (Number) function definition,

Below statement will help to call the function Recursively with updated value.

Counting(Number//10)

If you miss this statement then, after completing the first line it will terminate. For example,

Number = 1234

Then the output will be 1

Thank you for Visiting Our Blog