Welcome folks today in this post we will be counting
the number of digits inside a number
in python using functions and recursion. All the full source code of the application is shown below.
Get Started
In order to get started you need to make an app.py
file and copy paste the following code
First of all we will be computing using while
loop
app.py
1 2 3 4 5 6 7 8 9 |
# 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) |
Using Recursion
app.py
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# 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) |
Using Functions
app.py
1 2 3 4 5 6 7 8 9 10 11 12 |
# 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) |
Now if you execute the python script by typing the below command
python app.py