#include <bits/stdc++.h>
using namespace std;
int isSquare(int k)
{
// if k isn't perfect square then the square root
// will be a float value but we are rounding it off to integer
int s = sqrt(k);
// only in case of perfect square there
// will not be any rounding off error
if (s * s == k)
return 1;
else
return 0;
}
int checkFibo(int k)
{
// checking whether (5n^2+4) or (5n^2-4) is perfect square
if (isSquare(5 * k * k - 4) || isSquare(5 * k * k + 4))
return 1;
else
return 0;
}
void findFibos(int *a, int n)
{
int count = 0;
for (int i = 0; i < n; i++)
{
if (checkFibo(a[i]))
{
cout << a[i] << " ";
count++;
}
}
if (count)
cout << "\nabove " << count << " fibonacci numbers are present in the array\n";
else
cout << "\nno fibonacci number is present in the array";
}
int main()
{
int n;
// enter array length
cout << "enter size of elements\n";
cin >> n;
int *a = (int *)(malloc(sizeof(int) * n));
// fill the array
cout << "enter elements \n";
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
findFibos(a, n);
return 0;
}
Output
enter size of elements
5
enter elements
2
3
10
14
17
2 3
above 2 fibonacci numbers are present in the array
0 Comments