AbdulFathaah

Check fibonacci C

Feb 9th, 2024
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.69 KB | None | 0 0
  1. /*program to check a fbonacci number with its position */
  2. #include <stdio.h>
  3.  
  4. int main() {
  5. int n, first = 0, second = 1, third = 0, count = 3;
  6.  
  7. printf("Enter a number: ");
  8. scanf("%d", &n);
  9.  
  10. if (n == 0)
  11. printf("Yes, Position 1");
  12. else if (n == 1)
  13. printf("Yes, Position 2 and 3");
  14. else {
  15. while (third < n) {
  16. third = first + second;
  17. if (third == n) {
  18. printf("Yes, Position %d", count);
  19. break;
  20. }
  21. first = second;
  22. second = third;
  23. count++;
  24. }
  25. if (third > n)
  26. printf("No");
  27. }
  28.  
  29. return 0;
  30. }
  31.  
Advertisement
Add Comment
Please, Sign In to add comment