i must write a program that reads a number between 1 and 100 then calls a function to check if the number is part of the Fibonacci series.the main should display if the number is part of thr Fibonacci series.thanks
#include<iostream.h>
#include<conio.h>

int check_fib(int a)
{char ans1,ans2,ans;
int sum,x,i;
sum=0;
x=100;
ans1='y'
ans2='n'

for(i=0;i<=x;i++)
{
sum=sum+x;

if(sum==a)
ans=ans1;
break;

else
ans=ans2;
continue
}
return(ans);
}

void main()
{
clrscr();
int x,z;

cout<<"enter an integer";
cin>>x;
z=check_fib(x);

cout<<"the number is part if the fibonacci series"<<z<<endl;

}

Recommended Answers

All 3 Replies

>thanks
What for? You didn't ask a question.

>thanks
What for? You didn't ask a question.

oops i forgot
the program is not working am unable to find whats wrong.am using turbo c++

>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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.