Python Program to Find Number of Digits in Nth Fibonacci Number

Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example guide and practice well.

Given a number N the task is to calculate the number of digits in the Nth Fibonacci Number.

Fibonacci Numbers:

Starting with 0 and 1, the next two numbers are simply the sum of the previous two numbers. The third number in this sequence, for example, is 0+1=1, hence the third number is 1. Similarly, the fourth number in this series will be 1+1=2, resulting in the fourth number being 2.
The Fibonacci Number Series is as follows: 0 1 1 2 3 5 8 13 21 and so on.

Examples:

Example1:

Input:

Given N=14

Output:

The Number of digits present in 14 th Fibonacci number is [ 3 ]

Example2:

Input:

Given N=23

Output:

The 23 th Fibonacci number is [ 17711 ]
The Number of digits present in 23 th Fibonacci number is [ 5 ]

Python Program to Find Number of Digits in Nth Fibonacci Number

Below are the ways to find the number of digits in the Nth Fibonacci Number.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number N as static input and store it in a variable.
  • Take a variable temp1 and initialize its value to 0.
  • Take another variable temp2 and initialize its value to 1.
  • Loop till N-2 using For loop.
  • Increment the value of temp1 by temp1 i.e temp1=temp1+temp2.
  • Swap the values of temp1,temp2 using the ‘,’ operator.
  • Check if the value of the given number N is 1 or not using the If statement.
  • If it is true then print the value of temp1 which is the value of the nth Fibonacci number.
  • Else print the value of temp2 which is the value of the nth Fibonacci number.
  • Calculate the number of digits by converting it to string.
  • Calculate the length of string which is the number of digits in the nth Fibonacci Number.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as static input and store it in a variable.
numbr = 14
# Take a variable temp1 and initialize its value to 0.
temp1 = 0
# Take another variable temp2 and initialize its value to 1.
temp2 = 1
# Loop till N-2 using For loop.
for z in range(numbr-2):
    # Increment the value of temp1 by temp1 i.e temp1=temp1+temp2.
    temp1 = temp1+temp2
    # Swap the values of temp1,temp2 using the ',' operator.
    temp1, temp2 = temp2, temp1
# Check if the value of the given number N is 1 or not using the If statement.
if(numbr == 1):
  # If it is true then print the value of temp1 which is the value of the nth Fibonacci number
    print('The', numbr, 'th Fibonacci number is [', temp1, ']')
# Else print the value of temp2 which is the value of the nth Fibonacci number.
else:
    print('The', numbr, 'th Fibonacci number is [', temp2, ']')
# Calculate the number of digits by converting it to string.
stringnumbr = str(temp2)
# Calculate the length of string which is the number of digits in the nth Fibonacci Number.
nLen = len(stringnumbr)
print('The Number of digits present in', numbr,
      'th Fibonacci number is [', nLen, ']')

Output:

The 14 th Fibonacci number is [ 233 ]
The Number of digits present in 14 th Fibonacci number is [ 3 ]

Method #2: Using For Loop (User Input)

Approach:

  • Give the number N as user input using int(input()) and store it in a variable.
  • Take a variable temp1 and initialize its value to 0.
  • Take another variable temp2 and initialize its value to 1.
  • Loop till N-2 using For loop.
  • Increment the value of temp1 by temp1 i.e temp1=temp1+temp2.
  • Swap the values of temp1,temp2 using the ‘,’ operator.
  • Check if the value of the given number N is 1 or not using the If statement.
  • If it is true then print the value of temp1 which is the value of the nth Fibonacci number.
  • Else print the value of temp2 which is the value of the nth Fibonacci number.
  • Calculate the number of digits by converting it to string.
  • Calculate the length of string which is the number of digits in the nth Fibonacci Number.
  • The Exit of the Program.

Below is the implementation:

# Give the number N as user input using int(input()) and store it in a variable.
numbr = int(input('Enter some random nth number = '))
# Take a variable temp1 and initialize its value to 0.
temp1 = 0
# Take another variable temp2 and initialize its value to 1.
temp2 = 1
# Loop till N-2 using For loop.
for z in range(numbr-2):
    # Increment the value of temp1 by temp1 i.e temp1=temp1+temp2.
    temp1 = temp1+temp2
    # Swap the values of temp1,temp2 using the ',' operator.
    temp1, temp2 = temp2, temp1
# Check if the value of the given number N is 1 or not using the If statement.
if(numbr == 1):
  # If it is true then print the value of temp1 which is the value of the nth Fibonacci number
    print('The', numbr, 'th Fibonacci number is [', temp1, ']')
# Else print the value of temp2 which is the value of the nth Fibonacci number.
else:
    print('The', numbr, 'th Fibonacci number is [', temp2, ']')
# Calculate the number of digits by converting it to string.
stringnumbr = str(temp2)
# Calculate the length of string which is the number of digits in the nth Fibonacci Number.
nLen = len(stringnumbr)
print('The Number of digits present in', numbr,
      'th Fibonacci number is [', nLen, ']')

Output:

Enter some random nth number = 23
The 23 th Fibonacci number is [ 17711 ]
The Number of digits present in 23 th Fibonacci number is [ 5 ]

Related Programs:

Leave a Comment