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

Fix Breadcrumbs Error And m=1? In Blogger

  Remove Breadcrumbs Errors And ?m=1 Breadcrumb is a small path. It points your location on your site. It also affects the SEO of your website.  Use canonical URL , so: data:blog.url.canonical  instead of data:blog.url. It should work for both desktop and mobile versions. It means the same URL works on both mobile and desktop without producing the ?m=1 or ?m=0. Before this, I am going to tell you what all the portions I will explain and fix your problem. First we will fix Breadcrumbs Error . This is the most dangerous for your website. Second we will remove ?m=1 and ?m=0, tags from our mobile view URL. Remove Breadcrumbs Error :- You should follow the below steps, so that you would fixed this error :- Go to blogger.com Login to blogger with your blogger email id Click on Template Click edit HTML, you have to edit HTML theme code. Press Ctr + F to open a search bar in your HTML theme code. Search for <div class='breadcrumbs' Paste the below code up to </b:loop> ...

API : What is an Application Programming Interface

Application Programming Interface API का पूरा नाम "Application Programming Interface" हैं| यह एक software intermediary है जो दो software applications को एक दूसरे से बात करने का कम करता हैं| जैसे कि उद्धरण के लिए Instagram पर आपको message आता है और आप अपने phone पर check कर लेते है API के कारण, क्योंकि API एक माध्यम है आपके फ़ोन में आप किसी भी application से इनफार्मेशन प्राप्त कर पते  हैं| जब आप अपने phone में  कोई  application use करते है तो application internet से जुड़ता है और data को server पर  भेजता है| तब वह server, data को लेता है और translate करता है इस data को , उसके बाद उस data पर जरुरी कार्य करता है और अंतिम में जो translated data है उसे phone पर send करता हैं| तब Application उस data को translate करता है और आपके फ़ोन पर दिखता है readable form में जैसा आप देखना चाहते हैं|  ये जितने भी कार्य है, phone से data को लेना और उसे translate कर के server को send करना और फिर उसे phone पर user के readable form में change करके उसके फ़ोन पर डिस्प्ले करना, ये सब API के मा...

Blogger : How To Create Blog Through Blogger & Its Overview

Overview Of Creation Of Blog Now days online learning and earning is very popular. Most of the people is trying to either learn or earn from their home. If you are a writer or story teller, you have interest in writing or reading new thing. So, This is very helpful for you, in this post i will tell you how to create your own blog website and earn money. Step follow to create a blog on blogger:- First you should have a email account and phone number to open or create a blogging account. You have to go to blogger website page, URL of that page is given below, simply you go and click on the URL. Click on the create new blog, after clicking, you have enter the blog name that you want to show to the user and you must choose or set the appropriate your own blog URL. After this process, you will have created your blog. You can handle everything post, pages, layout, inside the setting,you can set the user information and the account information. In the layout you can set layout from the given ...