Friday 22 March 2019

CP: Fibonacci Series [i.e 1,1,2,3,5,8,13…N terms].

Que:

Write a program to print the Fibonacci Series [i.e 1,1,2,3,5,8,13…N terms].


Fibonacci Series
Fibonacci Series generates subsequent number by adding two previous numbers. Fibonacci series starts from two numbers − F0 & F1. The initial values of F0 & F1 can be taken 0, 1 or 1, 1 respectively.
Fibonacci series satisfies the following conditions −
Fn = Fn-1 + Fn-2
So a Fibonacci series can look like this −
F8 = 0 1 1 2 3 5 8 13
Code:
#include <stdio.h>
int main()

{
  int i, n, t1 = 0, t2 = 1, nextTerm;

  printf("\nEnter the number of terms: ");
  scanf("%d", &n);

   printf("\nFibonacci Series: ");

for (i = 1; i <= n; ++i)

    {  printf("%d, ", t1);
       nextTerm = t1 + t2;

       t1 = t2;
       t2 = nextTerm;
    }

return 0;

}


Output:



No comments:

Post a Comment

LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project

  LAB 7 Arduino with Seven Segment Display || Arduino Tutorial || Code and Circuit Diagram || Project Dear All We will learn how to Connec...