By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,190 Members | 833 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,190 IT Pros & Developers. It's quick & easy.

Can anyone help me please with a fibonacci program?

P: 40
I wrote this program code :

# include < iostream >

# include < iomanip >

using namespace std;

unsigned long factorial ( unsigned long );

int main ()

{

for ( int i = 0 ; i <= 10; i ++ )

cout << setw ( 2) << i << " ! = " << factorial (i) << endl;

return 0;

}

unsigned long factorial ( unsigned long number )

{

unsigned long product = 1;

for ( unsigned long i=1; i<=number; ++i )

{

product = product * i ;

}

return product;

}

How can I determine the largest fibonacci number that can be printed on my system ?
Sep 26 '06 #1
Share this Question
Share on Google+
13 Replies


P: 40
anyone please.
Sep 26 '06 #2

100+
P: 180
anyone please.
Hi..
This is like spoon feeding...Please try something rather than depending on others. This is a simple program and please try to give an attempt... Also please dont send the same query again and again. It irritates a lot.

This query I have seen for almost 3 times....

Try out yourself....I have already given a solution for your question...Try understanding yourself. Also please dont ask any stupid question again.

Thanx
Sep 26 '06 #3

10K+
P: 13,264
anyone please.
First find the largest number that can be printed on your system.
Then the problem becomes that of finding the largest fibbo number less or equal to a number
Sep 26 '06 #4

P: 40
First find the largest number that can be printed on your system.
Then the problem becomes that of finding the largest fibbo number less or equal to a number

THANKS FOR YOUR KIND ANSWER , I'LL TRY IT AND LET YOU KNOW

THANKS AGAIN
AYMAN
Sep 26 '06 #5

P: 40
THANKS FOR YOUR KIND ANSWER , I'LL TRY IT AND LET YOU KNOW

THANKS AGAIN
AYMAN

---------


I GOT 13! WAS THE LAST CORRECT NUMBER, 14! LOOKED FUNNY. DOES THAT MEANS THAT 13! WAS THE HIGHEST NUMBER ?
Sep 26 '06 #6

P: 40
i also want to add that eventhough 14! is giving me funny numbers, but I was gitting results ( funny results ) up to 33!, so which one I should considir highest 14! or 33! ?

thanks
ayman
Sep 26 '06 #7

Banfa
Expert Mod 5K+
P: 8,916
---------


I GOT 13! WAS THE LAST CORRECT NUMBER, 14! LOOKED FUNNY. DOES THAT MEANS THAT 13! WAS THE HIGHEST NUMBER ?
I am not sure if you are aware of this but in normal net etiquet using all capitals is considered to be shouting and some what aggresive or rude.
Sep 27 '06 #8

100+
P: 293
D_C
Fibonacci or Factorial?

Fib(n) = Fib(n-1) + Fib(n-2)
Fact(n) = n*Fact(n-1)

When the number gets too large, it will overflow. That means the sign changes. This is always true for addition. In general, it's not always the case for multiplying two numbers. However, for factorials it is.

Write a loop that calculates the next factorial or Fibonacci number. After calculating it, compare it to the previous one. If the previous one is now larger, then it overflowed. The previous of the two is the biggest your system can handle.
Sep 27 '06 #9

100+
P: 180
How can I determine the largest fibonacci number that can be printed on my system ?
Hi check this...Is this what you want???
I' ve not checked the output...Please test this program extensively and let me know if it works for all machines.....

Expand|Select|Wrap|Line Numbers
  1. long fact(long num)
  2. {        
  3.   long product = 1;    
  4.   for(long i=1; i<=num; ++i)
  5.   {
  6.      try
  7.      {
  8.         product *= i;
  9.         if((INT_MAX - product) < product*(i+1))
  10.         {
  11.     throw "Reached Max limit";                 
  12.         }                
  13.      }
  14.      catch (char* msg)
  15.      {    
  16.          cout<<msg<<endl;                 
  17.         cout<<"The max factorial value your system can       handle :"<<product<<endl;
  18.         return -1;
  19.       }
  20.    }
  21.     return product;
  22. }
  23.  
  24. int main(int argc, char** argv[])
  25. {
  26.      for ( long i = 0 ; i <= 15; i++ )
  27.       {
  28.          int val = fact(i);
  29.          if( val == -1 )
  30.     break;
  31.          else
  32.     cout << setw ( 2) << i << " ! = " << val << endl;
  33.        }                        
  34.       return 0;
  35. }
Sep 27 '06 #10

P: 40
I am not sure if you are aware of this but in normal net etiquet using all capitals is considered to be shouting and some what aggresive or rude.

-------------

dear Banfa

I didn't know that, but thank you for letting me know.

Ayman723
Sep 27 '06 #11

100+
P: 180
-------------

dear Banfa

I didn't know that, but thank you for letting me know.

Ayman723
Hi man!! did that help u????????
Sep 28 '06 #12

100+
P: 180
-------------

Ayman723
Hi man!! did that help u????????
Sep 28 '06 #13

P: 40
Hi man!! did that help u????????
thanks man. that helped me finish my project. I couldn't have done without your help guys.

thanks a million
ayman723
Sep 28 '06 #14

Post your reply

Sign in to post your reply or Sign up for a free account.