I want to print the first 100 numbers in the fibonacci sequence. My program prints until around 20 numbers than the numbers turn negative.
Can someone explain this to me please and provide a fix?
Thanks,
/*Fibonacci sequence*/
#include <iostream>
using namespace std;
int main(){
long int i, fib;
int firstNum=0, secondNum=1;
cout << firstNum << endl;
cout << secondNum << endl;
for (i=0; i < 100; i++){
fib = firstNum + secondNum;
firstNum = secondNum;
secondNum = fib;
cout << fib << endl;
}
return 0;
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…