Skip to main content

Fibonacci Series : C Program To Find The Index Of an Input Number

What is Fibonacci Series

The problem is to find the index of a number that occurs in the Fibonacci series. Fibonacci series starts from zero. Sequence such as 0 1 1 2 3 5 8 13 21 34 55.......etc. The sequence is the sum of two previous values in the series.
                                                                               

Algorithm :-

  1. Enter the number, you want to find the index in the Fibonacci series, read as n.
  2. Declare three integer variable, read as a, b, c, res, these variables are used to find the Fibonacci series.
  3. Initialize the variable a=0, series start from zero, b=1, c=1 and res=1,here res is the index of the Fibonacci series.
  4. We have to check, if n is less than or equal to one then return 1; else run the while loop in the next.
  5. For while loop variables a, b, c are used to find the Fibonacci series and res is used to calculate the index of n.
  6. whenever c is equal to or greater than the n, while loop while will be terminated, else while loop executes.
  7. First a+b is stored in c and res will be incremented, b value initialized to a and c value initialized to b, after firs loop completed, it will check the value of c and compare with n, if value of c is greater than the n then while loop terminates and return will return res which is the index to the main function.
  8. Inside the main function, index of the program is printed.

Program:-

#include<stdio.h>
#include<conio.h>

int fibo(int n)
{
  int a, b, c, res;
  a=0;
  b=1;
  c=1;
  
  res=1;

  if(n<=1)
   return n;

  while(c<n)
  {
     c=a+b;
     res++;
     a=b;
     b=c;
   }
   return res;
}

int main()
{
  int n,p;
  printf("Enter a number:-);
  scanf("%d",&n);
  
  p=fibo(n);
  printf("Index of %d is %d",n,res);
}

Output:-

Enter a number:-
21
Index of 21 is 8

Explanation:-
In this program, Finding the index of a number in The Fibonacci series, required number is entered as input by the user. A function used inside the main function fibo(), to pass the value that is fibo(n). It passes the enter input to the prototype function.

Some header files are used to execute the build in function in the c programming such as #include<stdio.h> and #include<conio.h>. In both, #include is including both the library files in the program. stdio.h and conio.h are two headers files, stio.h stands for standard input output in the c program.

Int fibo(int n), it takes the integer value n and passes inside the function to find the Fibonacci series and the index of number n. Variables a, b, c are used to calculate the Fibonacci series and res is used to find the index value of the entered input n.
Example:-
Enter n=21
Fibonacci series : 0 1 1 2 3 5 8 13 21 ......, so 21 comes at 8th position in the series,since 1 comes two times, but it is counted as 1, starts from zero as 1st position and position of 1 is first position of 1 is 2nd, position of 2 is 3rd, similarly position of 21, you will get 8th.

Comments

Popular posts from this blog

Merge Arrays : Merge Two Arrays Of Different Size In C++

Merge Two Arrays C++ Program C++ program to merge two arrays of different size. First array size is m and second array size is n. The size of the first array is greater than the first array i.e. m>n.                               Algorithm :- Enter the size of first array m. Enter the size of 2nd array n. Enter the elements of 1st and 2nd array and number of elements of first array should be greater than the number of elements of second array. After m index of first array, insert the element of second array into the first array up to m+n-1. Sort the merge array of size m+n. Display the first array, which will display all the elements of the merge array of size m+n. Program: - #include<iostream> using namespace std; int main() { int i,n,m,a[15],b[5],j;//array a[] and b[] are declared cout<<"Enter the size 1st array:-"<<endl; cin>>m; cout<<"Enter the size of 2nd array(n<m...

C++ Vector : Basics Of Vector in C++ STL

In C++, Vector is unlike array, it stores different data types such as int, double, string, float, etc. It works like the dynamic array with the ability to resize automatically itself. Vector stores data in the contiguous memory location. Two functions are needed to traverse from starting to end that is begin() and end() functions.                               Syntax of the vector declaration:-             vector<data_type> variable_name (number_of_elements);  Here number_of_elements is optional, we can also declare a vector with empty vector that contains zero elements. The data_type in the angle-brackets indicates any type of data type which is valid in c++. Vector declaration examples:- vector<int> numbers (10); //In this example, we declared a vector name number of 10 integers. vector<string> names;  //In this, a vector name declared of string. In eve...

Data Structure And Algorithm Important Topics Overview

  Overview Of Data Structures And Algorithms This post will give you the clear idea about learning data structures and algorithms . So, Data structure? => In computer science a data structure is a data organization , management and storage format that enables efficient access and modification. Or in the other words it is a way in which data is stored on a computer. Types of data structures:- Array  String Stack Queue Linked List  Binary Tree Binary Search Tree Heap Hash Table Graph Above topics, array is a linear data structure which stores the data in the sequence order, dynamic array, every data is stored in the next contiguous memory location.   String is a collection of characters. If it contains the alphabets and it also uses the digit as character. If a string is composed of numbers and characters then numbers are also treated as characters. Stack is also a linear data structure. It works either on Last In First Out or First In Last Out . It has only one ...