Advertisement
horselurrver

Fibonacci

Aug 2nd, 2016
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.78 KB | None | 0 0
  1. #include <stdio.h>
  2. void Fibonacci(int limit);
  3. int main(void){
  4.     int limit;
  5.     printf("Enter a limit: \n");
  6.     while(scanf("%d", &limit)==1 && limit!='q'){
  7.         if (limit<0)
  8.             printf("Try again.\n");
  9.         Fibonacci(limit);
  10.         printf("Enter a limit (q to quit): \n");
  11.  
  12.     }
  13. }
  14. void Fibonacci(int limit){
  15.     int count=0, oldnum1=1, oldnum2=1, newnum;
  16.     while (count<limit){
  17.         if(count==0){
  18.             printf("%d\n", oldnum1 );
  19.             count++;
  20.         }
  21.         else if (count==1){
  22.             printf("%d\n", oldnum2);
  23.             count++;
  24.         }
  25.         else{
  26.             newnum=oldnum1+oldnum2;
  27.             printf("%d\n", newnum);
  28.             oldnum1=oldnum2;
  29.             oldnum2=newnum;
  30.             count++;
  31.         }
  32.     }
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement