468,512 Members | 1,448 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

Can anyone help me please with a fibonacci program?

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
13 4919
anyone please.
Sep 26 '06 #2
vermarajeev
180 100+
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
r035198x
13,262 8TB
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
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
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
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
9,056 Expert Mod 8TB
---------


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
D_C
293 100+
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
vermarajeev
180 100+
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
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
vermarajeev
180 100+
-------------

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
vermarajeev
180 100+
-------------

Ayman723
Hi man!! did that help u????????
Sep 28 '06 #13
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.

Similar topics

28 posts views Thread by dleecurt | last post: by
reply views Thread by Alex Vinokur | last post: by
14 posts views Thread by felixnielsen | last post: by
3 posts views Thread by ayman723 | last post: by
6 posts views Thread by Andrew Tatum | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.