Fibonacci Numbers


SUBMITTED BY: Guest

DATE: Nov. 11, 2013, 4:51 p.m.

FORMAT: C++

SIZE: 903 Bytes

HITS: 1005

  1. // AUTHOR : If you like this, please donate to this Bitcoin address: 19GU2ZUFgwbsXM1NSP7jM55FnDiDiN5eCr
  2. // PURPOSE : Accept an integer number N from the user, and print out the first N Fibonacci numbers.
  3. #include <stdio.h>
  4. int main()
  5. {
  6. int n; // the number of Fibonacci we want to print
  7. int count; // the number of Fibonacci number printed so far. Loop Counter Variable
  8. int first;
  9. int second;
  10. int next;
  11. printf("\n Please enter a number greater than 2 : ");
  12. scanf_s("%d", &n);
  13. first = 0;
  14. second = 1;
  15. printf("%5d %5d", first, second);
  16. count = 2; // because 2 numbers have been printed so far
  17. while (count < n)
  18. {
  19. next = first + second;
  20. printf("%5d", next);
  21. count = count + 1;
  22. first = second;
  23. second = next;
  24. }
  25. getchar();
  26. getchar();
  27. return 0;
  28. }

comments powered by Disqus