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

help with fibonacci

P: 40
hello everyone;

I have made this code

# include < iostream >
# include < iomanip>

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 )
{ if ( number <= 1)

return 1;
else
return number * factorial ( number - 1 ) ;

}


my question is

1- how can i rewrite this program with a nonrecursive function
2- how can i detemine the largest fibonacci number that can be printed on my system?

3- how can i modify the part in # 1 to use double instead of int to calculate and return fibonacci numbers and use this modified program to repeat part #2



thanks in advance
Sep 25 '06 #1
Share this Question
Share on Google+
3 Replies


P: 40
anybody can help me please with this project...

thanks
Sep 25 '06 #2

100+
P: 180
anybody can help me please with this project...

thanks
This homework is for you and you should give an attempt first.
Anyway I have made your work quite easy.....

Please try out the third question and any problem we'll help....
The code might not be too efficient

Expand|Select|Wrap|Line Numbers
  1. # include < iostream >
  2. # include < iomanip>
  3.  
  4. unsigned long factorial ( unsigned long number)
  5. {
  6.     if ( number <= 1)
  7.     return 1;
  8.     else
  9.     return number * factorial ( number - 1 ) ;
  10. }
  11.  
  12. unsigned long fact(unsigned long num)
  13. {    
  14.     unsigned long product = 1;
  15.     for(unsigned long i=1; i<=num; ++i)
  16.     {
  17.         product = product*i;
  18.     }
  19.     return product;
  20. }
  21.  
  22. unsigned long largest(const vector<unsigned long>& vect)
  23. {
  24.     unsigned long large = vect[0];
  25.     for(unsigned long i=0; i<vect.size(); ++i)
  26.     {
  27.         if(vect[i] > large)
  28.             large = vect[i];
  29.     }
  30.     return large;
  31. }
  32.  
  33. int main()
  34. {
  35.     vector<unsigned long> vec;
  36.     cout<<"Non Recursive:"<<endl;
  37.     for ( int i = 0 ; i <= 10; i ++ )
  38.     {
  39.         unsigned long val = fact(i);
  40.         vec.push_back(val);
  41.         cout << setw ( 2) << i << " ! = " << val << endl;
  42.     }
  43.  
  44.     cout<<"=============================="<<endl;
  45.  
  46.     cout<<"Recursive:"<<endl;
  47.     for ( int i = 0 ; i <= 10; i ++ )
  48.     cout << setw ( 2) << i << " ! = " << factorial(i) << endl;
  49.  
  50.     cout<<"Largest is:"<<largest(vec)<<endl;
  51.  
  52.     return 0;
  53. }
Sep 25 '06 #3

P: 40
Hi;

Can You Please Explain Your Code For Me.

Thanks
Ayman
Sep 25 '06 #4

Post your reply

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