C Program to Display Fibonacci Sequence with Algorithm and Flowchart

 Fibonacci Sequence



Source: Wikipedia




Fibonacci sequence is a series of numbers arranged such that the number in nth place is the sum of (n-1)th and (n-2)th element in the sequence, starting with 0,1. In simple words, current place number is the sum of 2 numbers behind it in the series given the first two numbers be 0 and 1.

Fibonacci Sequence: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, . . . .

Mathematical Rule for Fibonacci Sequence: 

    Xn = Xn-1 + Xn-2


Source: Wikipedia

Concepts in C to know to make the program to compute Fibonacci Sequence:

  1. Loops
  2. Operators
  3. Loop break and continue statements

Algorithm for Fibonacci Sequence

  1. Start
  2. Declare necessary variables
  3. Set first number's value to 0 and second to 1
  4. Take input for nth term
  5. Start loop from 1 to n(inclusive)
    1. Print first number
    2. Sum up first and second number and store it in a variable
    3. Set first number's value to current second number's
    4. Set current second number's value to the stored sum
  6. End

Flowchart for Fibonacci Sequence

Flowchart for fibonacci series


C program for Fibonacci Sequence:

#include <stdio.h>
int main() {
    int n, nTerm, fTerm = 0, sTerm = 1;
    printf("Enter the number of terms: ");
    scanf("%d", &n);
    printf("Required Fibonacci Series is: ");
    for (i = 1; i <= n; ++i) {
        printf("%d, ", fTerm);
        nextTerm = fTerm + sTerm;
        fTerm = sTerm;
        sTerm = nTerm;
    }
}
Output:
Enter the number of terms: 7
Required Fibonacci Series is: 0,1,1,2,3,5,8 

If you got any problems or queries, then please comment below. We will try to clear your doubts as soon as possible.

Post a Comment

0 Comments