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

In the previous article, we have discussed Python Program to Find Sum of Odd Numbers Using Recursion in a List/Array

Given a number and the task is to count the number of digits present in a given number using recursion in python.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given Number = 567812

Output:

The Number of digits present in the above given Number { 567812 } =  6

Example2:

Input:

Given Number = 602

Output:

The Number of digits present in the above given Number { 602 } =  3

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

Below are the ways to count the number of digits present in a given number using recursion in python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the number as static input and store it in a variable.
  • Take a variable say cnt_digits and initialize its value to 0.
  • Pass the given number as an argument to the countnumbr_digits function.
  • Create a recursive function to say countnumbr_digits which takes the given number as an argument and returns the count of the number of digits present in a given number.
  • Make the cnt_digits a global declaration.
  • Check if the given number is not equal to 0 using the if conditional statement.
  • If the statement is true, then increment the value of cnt_digits by 1 and store it in the same variable.
  • Pass the given number divided by 10 as an argument to the countnumbr_digits function(which removes the last digit of the number) {Recursive Logic}.
  • Return cnt_digits.
  • Print the count of the number of digits present in the above-given number.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say countnumbr_digits which takes the given number as
# an argument and returns the count of the number of digits present in a given number.


def countnumbr_digits(gvn_numb):
  # Make the cnt_digits a global declaration.
    global cnt_digits
    # Check if the given number is not equal to 0 using the if conditional statement.
    if (gvn_numb != 0):
      # If the statement is true, then increment the value of cnt_digits by 1 and store it
      # inthe same variable.
        cnt_digits += 1
      # Pass the given number divided by 10 as an argument to the countnumbr_digits function
      # (which removes the last digit of the number) {Recursive Logic}.
        countnumbr_digits(gvn_numb // 10)
    # Return cnt_digits.
    return cnt_digits


# Give the number as static input and store it in a variable.
gvn_numb = 567812
# Take a variable say cnt_digits and initialize its value to 0.
# Pass the given number as an argument to the countnumbr_digits function.
cnt_digits = 0
# Print the count of the number of digits present in the above-given number.
print("The Number of digits present in the above given Number {", gvn_numb, "} = ", countnumbr_digits(
    gvn_numb))

Output:

The Number of digits present in the above given Number { 567812 } =  6

Method #2: Using Recursion (User Input)

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Take a variable say cnt_digits and initialize its value to 0.
  • Pass the given number as an argument to the countnumbr_digits function.
  • Create a recursive function to say countnumbr_digits which takes the given number as an argument and returns the count of the number of digits present in a given number.
  • Make the cnt_digits a global declaration.
  • Check if the given number is not equal to 0 using the if conditional statement.
  • If the statement is true, then increment the value of cnt_digits by 1 and store it in the same variable.
  • Pass the given number divided by 10 as an argument to the countnumbr_digits function(which removes the last digit of the number) {Recursive Logic}.
  • Return cnt_digits.
  • Print the count of the number of digits present in the above-given number.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say countnumbr_digits which takes the given number as
# an argument and returns the count of the number of digits present in a given number.


def countnumbr_digits(gvn_numb):
  # Make the cnt_digits a global declaration.
    global cnt_digits
    # Check if the given number is not equal to 0 using the if conditional statement.
    if (gvn_numb != 0):
      # If the statement is true, then increment the value of cnt_digits by 1 and store it
      # inthe same variable.
        cnt_digits += 1
      # Pass the given number divided by 10 as an argument to the countnumbr_digits function
      # (which removes the last digit of the number) {Recursive Logic}.
        countnumbr_digits(gvn_numb // 10)
    # Return cnt_digits.
    return cnt_digits


# Give the number as user input using the int(input()) function and 
# store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Take a variable say cnt_digits and initialize its value to 0.
# Pass the given number as an argument to the countnumbr_digits function.
cnt_digits = 0
# Print the count of the number of digits present in the above-given number.
print("The Number of digits present in the above given Number {", gvn_numb, "} = ", countnumbr_digits(
    gvn_numb))

Output:

Enter some random number = 5341
The Number of digits present in the above given Number { 5341 } = 4

Find a comprehensive collection of Examples of Python Programs ranging from simple ones to complex ones to guide you throughout your coding journey.