This is a C program to calculate Fibonacci numbers upto n number.

Fibonacci sequence is a set of numbers that starts with a one or a zero, followed by a one, and proceeds based on the rule that each number (called a Fibonacci number) is equal to the sum of the preceding two numbers. ... F (0) = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89
like here, sum of 0 and 1 equals to 1   also sum of 1 and 1 equals to 2 also sum of 1 and 2 equals to 3 and so on.

Source Code:

  1. #include <stdio.h>
  2.  

  3. int main()
  4. {
  5.               //Variable Declaration.
  6.   int n, first = 0, second = 1, next, c;
  7.         //Take Input.
  8.   printf("Enter the number of terms\n");
  9.   scanf("%d", &n);
  10.  
  11.   printf("First %d terms of Fibonacci series are:\n", n);
  12.      //Working Formula
  13.   for (c = 0; c < n; c++)
  14.   {
  15.     if (c <= 1)
  16.       next = c;
  17.     else
  18.     {
  19.       next = first + second;
  20.       first = second;
  21.       second = next;
  22.     }
  23.     printf("%d\t", next);
  24.   }
  25.  
  26.   return 0;
  27. }

Output:

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

Enter the number of terms
20
First 20 terms of Fibonacci series are:
0       1       1       2       3       5       8       13      21      34      55      89      144     233     377
        610     987     1597    2584    4181

0 comments:

Post a Comment