Program to find the particular digit of fibonacci series

Amoeba multiplication

In this post we will be discussing to find out the particular digit of a Fibonacci series and the whole of the procedure to write the required set of code to get that particular digit of the series.

Basics of Fibonacci Series
Program to find the particular digit of fibonacci series

Problem Description : 

The Environmental Eco Club has discovered a new Amoeba that grows in the order of a Fibonacci series every month. They are exhibiting this amoeba in a national conference. They want to know the size of the amoeba at a particular time instant. If a particular month’s index is given, write a program to display the amoeba’s size.

For Example, the size of the amoeba on month 1, 2, 3, 4, 5, 6,... will be 0, 1, 1, 2, 3, 5, 8.... respectively.

INPUT & OUTPUT FORMAT:

The input is an integer which denotes the count of the month.

The output is an integer denoting the amoeba size.

Refer the sample output for formatting.

SAMPLE INPUT:

7

SAMPLE OUTPUT:

8

Logic to follow to come-up with the solution :

1. We will be creating a function named fib(int n) with an argument of int datatype.

2. Now in the function definition if inputted number is less than or equal to1 then return and print the same inputted number.

                                                if( n <= 1) , return n;

3. On the other hand if number is greater than 1 then we return the summation of last second and last third number of the series.

                                           or return fib(n-1)+fib(n-2);

4. Now in the main function input the number i.e., user input.

5. Call the declared function and print the element.

Let’s write the required code for the problem :

Code :

#include<iostream>

using namespace std;

int fib(int n)

{

if(n<=1)

  return n;

 return fib(n-1)+fib(n-2);}

int main()

{

  int n;

cin>>n;

cout<<fib(n-1);

  return 0;

}

Sample Test Cases have been passed successfully

You can run same set of code at this portal, click here to go to the online portal and just give required set of inputs and click on Execute to run the program.

If you liked this post, then share this with your friends and if anything is confusing or incorrect then let me know in the comment section.

Thanks from my side, this is Mayank, keep learning and exploring !!

Next Post Previous Post
No Comment
Add Comment
comment url

Best Buy Products