#include using namespace std; /* * This C++ checks to see if a number is a Fibonacci number. * * @creator gdt * @created 2008.03.27 * @see http://en.wikipedia.org/wiki/Fibonacci_number * @caveats No sanity checking performed on the inputs. * int[] minimizes maximum Fibonacci number. * @notes This program uses the array data structure. * Uses STDC Library function atoi() to convert * command-line argument strings into ints. */ int main(int argc, char* argv[]) { const int N_LEN = 32; int n[N_LEN]; int i, j, x; // generate the 1st 'limit' Fibonacci numbers n[0] = 0; n[1] = 1; int limit = N_LEN - 1; for (i = 2; i < limit; i++) n[i] = n[i - 2] + n[i - 1]; // print the 1st 'limit' Fibonacci numbers if no inputs... if (argc == 1) { for (i = 0; i < limit; i++) cout << n[i] << endl; return 0; } for (i = 1; i < argc; i++) { x = atoi(argv[i]); for (j = 0; j < limit; j++) { if (n[j] == x) { cout << x << " is a Fibonacci number" << endl; break; } } } return 0; }