C program to find fibonacci series-Fibonacci sequence, fibonacci in c

 C Program To Find Fibonacci Series

Here we will learn c program to display the fibanacci series. Fibonacci series is series where the first number is zero and the next number is one and then the following numbers are the sum of previous two numbers.

C program to find fibonacci series
C program to find fibonacci series



First we will input the number the terms in fibonacci sequence to be printed then we will print the first two numbers of fbanacci series that is 0 and 1 and than we will run a for loop to print the following numbers in fibanacci sequence in which we will add previous two numbers in the nextterm variable which is 'nt' in the below program and we will print it and reaplace the value of f1 and f2 with the previous two numbers in the fibanacci series.

What is Fibonacci sequence

In mathematics, the Fibonacci sequence, commonly denoted Fn, form a sequence called the Fibonacci sequence, such that each number is the sum of the two preceding ones, starting from 0 and 1.

f0 = 0 and f1 = 1 and 
 
fn = fn-1 + fn-2

The sequence starts: 
          0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . .

As you can see any number in the series is the sum of previous two numbers 

For Example:-

F5 = F4 + F3 = 3 + 2 = 5.

Algorithm:-

  1. Start
  2. Assign f1 to 0 and f2 to 1
  3. Input n
  4. Print f1 and f2
  5. Run for loop from  i=2 to i<n in steps of one
  6. add nextterm nt and print it
  7. Assign f1 to f2 and f2 to nt
  8. Stop.

Program:-

C program to find the fibonacci sequence.


#include <stdio.h>

int main()
{
    int n, i, f1=0, f2=1, nt;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    
    printf("Fibonacci Series:\n %d %d ", f1, f2);
    
    for (i = 2; i < n; i++) {
        nt = f1 + f2;
        printf("%d ", nt);
        f1 = f2;
        f2 = nt;
    }

    return 0;
}


Output:-

Enter the number of terms: 10                                                                                         
Fibonacci Series:                                                                                                     
 0 1 1 2 3 5 8 13 21 34  



Enter the number of terms: 5                                                                                          
Fibonacci Series:                                                                                                     
 0 1 1 2 3 


Explanation:-

In the above program to find the fibonacci series, first we are asking the user to enter the number of terms and that we are storing that in the integer variable 'n'. Since we know the first two numbers of the fibonacci series which is zero and one, we store these numbers zero and one in two integer variables f1 and f2 respectively. After taking the user input into the variable n we are running the for-loop from 2 to n and then we are finding the next value of the series by adding the previous two value. First time we add zero and one to get one and then add one and one to get two and we repeat this until the for-loop terminates. Since the for loop is running till n we have to change the value of f1 and f2 in every iteration so we assign f1 with the previous value of f2 and f2 with the sum of previous two values. During each time through the loop we print the sum of previous two values. 

Post a Comment

0 Comments