{"id":12756,"date":"2021-09-30T17:30:33","date_gmt":"2021-09-30T12:00:33","guid":{"rendered":"https:\/\/python-programs.com\/?p=12756"},"modified":"2021-11-22T18:33:32","modified_gmt":"2021-11-22T13:03:32","slug":"python-program-to-find-number-of-digits-in-nth-fibonacci-number","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-number-of-digits-in-nth-fibonacci-number\/","title":{"rendered":"Python Program to Find Number of Digits in Nth Fibonacci Number"},"content":{"rendered":"
Don’t stop learning now. Get hold of all the important Java fundamentals with the Simple java program example<\/a> guide and practice well.<\/p>\n Given a number N the task is to calculate the number of digits in the Nth Fibonacci Number.<\/p>\n Fibonacci Numbers:<\/strong><\/p>\n 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. Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the number of digits in the Nth Fibonacci Number.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n 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 …<\/p>\n
\nThe Fibonacci Number Series is as follows: 0 1 1 2 3 5 8 13 21 and so on.<\/p>\nGiven N=14<\/pre>\n
The Number of digits present in 14 th Fibonacci number is [ 3 ]<\/pre>\n
Given N=23<\/pre>\n
The 23 th Fibonacci number is [ 17711 ]\r\nThe Number of digits present in 23 th Fibonacci number is [ 5 ]<\/pre>\n
Python Program to Find Number of Digits in Nth Fibonacci Number<\/h2>\n
\n
Method #1: Using For Loop (Static Input)<\/h3>\n
\n
# Give the number N as static input and store it in a variable.\r\nnumbr = 14\r\n# Take a variable temp1 and initialize its value to 0.\r\ntemp1 = 0\r\n# Take another variable temp2 and initialize its value to 1.\r\ntemp2 = 1\r\n# Loop till N-2 using For loop.\r\nfor z in range(numbr-2):\r\n # Increment the value of temp1 by temp1 i.e temp1=temp1+temp2.\r\n temp1 = temp1+temp2\r\n # Swap the values of temp1,temp2 using the ',' operator.\r\n temp1, temp2 = temp2, temp1\r\n# Check if the value of the given number N is 1 or not using the If statement.\r\nif(numbr == 1):\r\n # If it is true then print the value of temp1 which is the value of the nth Fibonacci number\r\n print('The', numbr, 'th Fibonacci number is [', temp1, ']')\r\n# Else print the value of temp2 which is the value of the nth Fibonacci number.\r\nelse:\r\n print('The', numbr, 'th Fibonacci number is [', temp2, ']')\r\n# Calculate the number of digits by converting it to string.\r\nstringnumbr = str(temp2)\r\n# Calculate the length of string which is the number of digits in the nth Fibonacci Number.\r\nnLen = len(stringnumbr)\r\nprint('The Number of digits present in', numbr,\r\n 'th Fibonacci number is [', nLen, ']')\r\n<\/pre>\n
The 14 th Fibonacci number is [ 233 ]\r\nThe Number of digits present in 14 th Fibonacci number is [ 3 ]<\/pre>\n
Method #2: Using For Loop (User Input)<\/h3>\n
\n
# Give the number N as user input using int(input()) and store it in a variable.\r\nnumbr = int(input('Enter some random nth number = '))\r\n# Take a variable temp1 and initialize its value to 0.\r\ntemp1 = 0\r\n# Take another variable temp2 and initialize its value to 1.\r\ntemp2 = 1\r\n# Loop till N-2 using For loop.\r\nfor z in range(numbr-2):\r\n # Increment the value of temp1 by temp1 i.e temp1=temp1+temp2.\r\n temp1 = temp1+temp2\r\n # Swap the values of temp1,temp2 using the ',' operator.\r\n temp1, temp2 = temp2, temp1\r\n# Check if the value of the given number N is 1 or not using the If statement.\r\nif(numbr == 1):\r\n # If it is true then print the value of temp1 which is the value of the nth Fibonacci number\r\n print('The', numbr, 'th Fibonacci number is [', temp1, ']')\r\n# Else print the value of temp2 which is the value of the nth Fibonacci number.\r\nelse:\r\n print('The', numbr, 'th Fibonacci number is [', temp2, ']')\r\n# Calculate the number of digits by converting it to string.\r\nstringnumbr = str(temp2)\r\n# Calculate the length of string which is the number of digits in the nth Fibonacci Number.\r\nnLen = len(stringnumbr)\r\nprint('The Number of digits present in', numbr,\r\n 'th Fibonacci number is [', nLen, ']')\r\n<\/pre>\n
Enter some random nth number = 23\r\nThe 23 th Fibonacci number is [ 17711 ]\r\nThe Number of digits present in 23 th Fibonacci number is [ 5 ]<\/pre>\n
\n