>the program is not working am unable to find whats wrong.
It could be all of the awful constructs you use... :rolleyes: I won't mention them again because I described them in detail for your other post.
>am using turbo c++
Get. A. Newer. Compiler. Turbo C++ is ancient, and not only are you missing out on all of the cool stuff that C++ has learned since that silly compiler, you also drive away potential helpers for your problems because they have to perform extensive surgery just to get your pre-standard code to even compile!
Anyway, the following is how to calculate a Fibonacci number and return true if the argument matches it, false otherwise.
#include <iostream>
using namespace std;
bool check_fib ( unsigned long int val )
{
unsigned long int a = 0, b = 1, c;
if ( val == 0 || val == 1 )
return true;
for ( int i = 2; i < 45; i++ ) {
c = a + b;
if ( val == c )
return true;
a = b;
b = c;
}
return false;
}
int main()
{
unsigned long int val;
cout<<"Enter an integer: ";
cin>> val;
if ( check_fib ( val ) )
cout<<"The number is part of the Fibonacci series"<<endl;
}
Though a much better option because the length of the Fibonacci sequence is severely limited with C++ data types is to use a table driven method:
#include <cstddef>
#include <iostream>
using namespace std;
bool check_fib ( unsigned long int val )
{
static unsigned long int fib_seq[] = {
0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,
610,987,1597,2584,4181,6765,10946,17711,
28657,46368,75025,121393,196418,317811,
514229,832040,1346269,2178309,3524578,
5702887,9227465,14930352,24157817,39088169,
63245986,102334155,165580141,267914296,433494437,
701408733,1134903170
};
const size_t seq_size = sizeof fib_seq / sizeof *fib_seq;
for ( size_t i = 0; i < seq_size; i++ ) {
if ( val == fib_seq[i] )
return true;
}
return false;
}
int main()
{
unsigned long int val;
cout<<"Enter an integer: ";
cin>> val;
if ( check_fib ( val ) )
cout<<"The number is part of the Fibonacci series"<<endl;
}