Generate a Fibonacci Sequence - C Data Type

C examples for Data Type:int

Description

Generate a Fibonacci Sequence

Demo Code

#include <stdio.h>

int main()/*from w  ww  .java2  s .  c om*/
{
      int counter = 33;
      long int lngA, lngB, lngC, lngD;
      char ch;

      lngA = 0;
      lngB = 1;
      printf("Fibonacci Sequence:\n");

      for (int k = 1; k <= counter; k++) {
           printf("%d th term is : %ld\n", ((k * 2) - 1), lngA);
           if (((k *2) - 1) == counter)
              break;
           printf("%d th term is : %ld\n", (k * 2), lngB);

           if ((k * 2) == counter)
              break;
           lngC = lngA + lngB;
           lngD = lngB + lngC;
           lngA = lngC;
           lngB = lngD;
      }

      return(0);
}

Result


Related Tutorials