After sequence 47 using long type the program won't display the output properly (I am assuming because of size limit of type) so I switched type to unsigned long and now number 49 outputs as 512559680 when it should be 4807526976
Is there something wrong with my code, or is there something about using unsigned long that I am unaware of? Also is there anyway to output long double in normal notation to view the sequence further down? Any help would be greatly appreciated.
Expand|Select|Wrap|Line Numbers
- #include <iostream>
- using std::cin;
- using std::cout;
- using std::endl;
- unsigned long fib(unsigned long, unsigned long);
- int main()
- {
- unsigned long f0 = 0;
- unsigned long f1 = 1;
- int counter = 0;
- while (counter <= 46)
- {
- if (f0 == 0)
- {
- cout << "Sequence 1: " << f0 << endl;
- cout << "Sequence 2: " << f1 << endl;
- f0 = f1 + f0;
- counter = 2;
- cout << "Sequence 3: " << f0 << endl;
- }
- f0 = fib(f0, f1);
- cout << "Sequence " << counter + 2<< ": " << f0 << endl;
- counter++;
- f1 = fib(f1, f0);
- cout << "Sequence " << counter + 2<< ": " << f1 << endl;
- counter++;
- }
- cout << endl << endl;
- system("PAUSE");
- return 0;
- }
- unsigned long fib(unsigned long x, unsigned long y)
- {
- return(x+y);
- }