Connecting Tech Pros Worldwide Forums | Help | Site Map

Can anyone help me please with a fibonacci program?

Member
 
Join Date: Sep 2006
Posts: 52
#1: Sep 26 '06
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 ?
Member
 
Join Date: Sep 2006
Posts: 52
#2: Sep 26 '06

re: Can anyone help me please with a fibonacci program?


anyone please.
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#3: Sep 26 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by ayman723

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
Lives Here
 
Join Date: Sep 2006
Posts: 12,070
#4: Sep 26 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by ayman723

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
Member
 
Join Date: Sep 2006
Posts: 52
#5: Sep 26 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by r035198x

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
Member
 
Join Date: Sep 2006
Posts: 52
#6: Sep 26 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by ayman723

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 ?
Member
 
Join Date: Sep 2006
Posts: 52
#7: Sep 26 '06

re: Can anyone help me please with a fibonacci program?


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
Banfa's Avatar
AdministratorVoR
 
Join Date: Feb 2006
Location: South West UK
Posts: 6,166
#8: Sep 27 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by ayman723

---------


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.
D_C D_C is offline
Needs Regular Fix
 
Join Date: Jun 2006
Posts: 294
#9: Sep 27 '06

re: Can anyone help me please with a fibonacci program?


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.
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#10: Sep 27 '06

re: Can anyone help me please with a fibonacci program?


Quote:
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. }
Member
 
Join Date: Sep 2006
Posts: 52
#11: Sep 27 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by Banfa

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
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#12: Sep 28 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by ayman723

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

dear Banfa

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

Ayman723

Hi man!! did that help u????????
Familiar Sight
 
Join Date: Aug 2006
Posts: 180
#13: Sep 28 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by ayman723

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

Ayman723

Hi man!! did that help u????????
Member
 
Join Date: Sep 2006
Posts: 52
#14: Sep 29 '06

re: Can anyone help me please with a fibonacci program?


Quote:

Originally Posted by vermarajeev

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
Reply