Hello, I've been in the process of teaching myself C++, going through different tutorials and like. Anyway I was working on a fibonacci sequence program - I looked through the practice problems here and decided to do them all from beginner on upward. I thought I had it working only to find later when I went to play around with it some that I did not. Everything works up until the 47th value which comes out negative. I stepped through the loop and the numbers for 45 and 46 are correct and yet they are producing a negative...
Here's my code: I'm sure there's a better way to do this, I know I did it differently in school when we did Java but my mind is blanking.
#include <iostream>
using namespace std;
void fibonacci(int loop);
int main(){
cout << "Please enter the number of fibonacci numbers you would like displayed: ";
int loop;
cin >> loop;
fibonacci(loop);
system("PAUSE");
}
void fibonacci(int loop){
int one = 0;
int two = 1;
for(int i = 0; i < loop; i++){
if(i == 0)
{
cout << i << ": " << i << "\n";
}
if(i == 1)
{
cout << i << ": " << i << "\n";
}
if(i > 1)
{
int nextNum = one + two;
cout << i << ": " << nextNum << " " << "\n";
one = two;
two = nextNum;
}
}
}
everything is great apparently until it tries to add 1134903170 + 1836311903 and comes up with -132375223