-
Fibonacci sequence
What am I doing wrong here, any suggestions....
#include <iostream>
#include <cmath>
//using namespace std;
int main ()
{
int num1, num2, num3;
float ratio, Gold;
cout << "Enter the number to end the sequence: << num3 ";
num1 = 1;
num2 = 1;
if (num3 < 500)
{
num3 = num1 + num2;
ratio = num1/ num2;
num1 = num2;
num2 = num3;
}
cout<< num1;
cout<< "The ratio is: << ratio);
cout<< num2;
Gold = sqrt(5) -1 /2;
cout << "The Golden ratio is: "<< Gold ";
return 0;
}
TIA
Squirrelly
-
Try fixing the #include statements...
#include <iostream.h>
#include <cmath.h>
...
-
<cmath> and <iostream> are the now standard way of saying <math.h> (the c prefix is because it is an ANSI C library) and the old <iostream.h>. No official headers in the new standard include the .h suffix.
You didn't elaborate on the problem, though I'll venture a guess that ratio is always 0.
ratio = num1/num2
num1 and num2 are both ints, thus the result of that division is an integer. Since num2 is always greater than num1, the result will always be 0.
This can be changed by casting the terms to floats before performing the operation, like this.
ratio = (float) num1 / (float) num2