Skip to content

Create Check if a Number is Fibonacci Number #130

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions Check if a Number is Fibonacci Number
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// C++ program to check if x is a perfect square
#include <bits/stdc++.h>
using namespace std;

// A utility function that returns true if x is perfect
// square
bool isPerfectSquare(int x)
{
int s = sqrt(x);
return (s * s == x);
}

// Returns true if n is a Fibonacci Number, else false
bool isFibonacci(int n)
{
// n is Fibonacci if one of 5*n*n + 4 or 5*n*n - 4 or
// both is a perfect square
return isPerfectSquare(5 * n * n + 4)
|| isPerfectSquare(5 * n * n - 4);
}

// A utility function to test above functions
int main()
{
for (int i = 1; i <= 10; i++)
isFibonacci(i)
? cout << i << " is a Fibonacci Number \n"
: cout << i << " is a not Fibonacci Number \n";
return 0;
}

// This code is contributed by Sania Kumari Gupta (kriSania804)