+ 1

Please friends Write a program that input a number and find the number is Fibonacci or not.

Thanks in advance. hint! Fibonacci numbers are 1,1,2,3,5,8,....

18th Feb 2017, 10:30 AM
Nomi Numan
Nomi Numan - avatar
1 Odpowiedź
+ 3
#include <iostream> #include <math.h> using namespace std; bool isPerfectSquare(int x) { int s = sqrt(x); return (s*s == x); } bool isFibonacci(int n) { // n is Fibinacci if one of 5*n*n + 4 or 5*n*n - 4 or both return isPerfectSquare(5*n*n + 4) || isPerfectSquare(5*n*n - 4); } 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; }
18th Feb 2017, 1:14 PM
Megatron
Megatron - avatar