Python 3 Script to Count the Number of Digits in the Number Using Loops and Functions and Recursion Full Tutorial For Beginners

Python 3 Script to Count the Number of Digits in the Number Using Loops and Functions and Recursion Full Tutorial For Beginners

 

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

 

# 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)

 

See also  Python 3 Script to Build a Sitemap Generator and Download Sitemap XML File for any Website or Domain Using sitemap-generator Library Full Tutorial For Beginners

 

Using Recursion

 

app.py

 

# 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

 

 

# 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)

 

See also  Python Tkinter Random Number Generator Using Random Library GUI Desktop App Full Project For Beginners

 

Now if you execute the python script by typing the below command

 

python app.py

 

 

 

Leave a Reply