C program to find Fibonacci series for a given number



Fibonacci Series is a sequence of numbers obtained by adding the two previous numbers.

Fibonacci series starts from two numbers f0 & f1.

The initial values of fo & f1 can be taken 0, 1 or 1, 1 Fibonacci series satisfies the following conditions −

fn = fn-1 + fn-2

Algorithm

Refer to the algorithm for the Fibonacci series.

START Step 1: Read integer variable a,b,c at run time Step 2: Initialize a=0 and b=0 Step 3: Compute c=a+b Step 4: Print c Step 5: Set a=b, b=c Step 6: Repeat 3 to 5 for n times STOP

Example

Following is the C program for the Fibonacci series using While Loop −

 Live Demo

#include <stdio.h> int main(){    int number, i = 0, Next, first = 0, second = 1;    printf("
Please Enter the Range Number: ");    scanf("%d",&number);    while(i < number){       if(i <= 1){          Next = i;       }       else{          Next = first + second;          first = second;          second = Next;       }       printf("%d \t", Next);       i++;    }    return 0; }

Output

When the above program is executed, it produces the following result −

Please Enter the Range Number: 6 0 1 1 2 3 5
Updated on: 2021-03-08T10:23:36+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements