Advertisement
noor017

Fibonacci

Jun 17th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.58 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5.     int count, preTerm=0, curTerm=1, newTerm;
  6.  
  7.     scanf("%d", &count);
  8.  
  9.     if (count == 0)
  10.     {
  11.         printf("0\n");
  12.     }
  13.  
  14.     else if (count == 1)
  15.     {
  16.         printf("0 1\n");
  17.     }
  18.  
  19.     else
  20.     {
  21.         newTerm = preTerm+curTerm;
  22.         printf("%d %d", preTerm, curTerm); /* newTerming first two terms */
  23.  
  24.         while(newTerm<count)
  25.         {
  26.             printf(" %d",newTerm);
  27.             preTerm=curTerm;
  28.             curTerm=newTerm;
  29.             newTerm = preTerm+curTerm;
  30.         }
  31.  
  32.         printf("\n");
  33.     }
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement