In every Fibonacci series program (that i saw) after the 46th number (1836311903) the program starts writing negative numbers (like the 47th = -132375223 instead of 2971215073*).
This is my program but the same mistake repeats with other codes from the internet:
#include <iostream>
usingnamespace std;
int main()
{
int NumerOfTheFibonacciSeriesTheUserWants;
cout << "How many numbers of the Fibonacci series do you want?" << endl ;
cin >> NumerOfTheFibonacciSeriesTheUserWants;
cout << endl << "0 , 1";
int previousNumber = 0;
int sum = 1;
int total = 1;
for (int numberOfTimesRan = 1; numberOfTimesRan <= NumerOfTheFibonacciSeriesTheUserWants - 1 ; numberOfTimesRan++){
sum = sum + previousNumber;
previousNumber = sum - previousNumber;
cout << " , " << sum ;
total += sum;
}
cout << "\n \n \n \n The sum of these numbers is " << total << "\n \n \n \n" ;
return 0;
}
Possible solution in your case would be to use [unsigned] long long. This will at least postpone your problem. If you will need values which will not fit in long long, you will need to search for some bigInt library.