# Fibonacci sequence output statement

• 11-18-2004
chocha7
Fibonacci sequence output statement
HI i need help writing my output statement for fibonacci sequence so far i have how to get the sequence and i must output all array elements with 5 elements per line and in a field width of 10....can someone help me?

here is what i have so far:
for (int = 0; i < 30; i++)
{
cout << setw(10) << fib[i];
}
cout << endl;
• 11-18-2004
swoopy
Code:

```for (int = 0; i < 30; i++) {   cout << setw(10) << fib[i];   if (                  )  //Look up the modulus operator       cout << endl; } cout << endl;```
Besides using the modulus operator, you could instead keep a count, and when it reaches 5, output a newline.
• 11-18-2004
chocha7
That's exactly what i was thinking....but n e suggestions on where to start?
• 11-18-2004
Zach L.
Check swoopy's post again. There are a couple methods you could use to accomplish this: mod (%) and a resetable counter.
• 11-18-2004
chocha7
ok here is what i got :
for (int i = 0; i < 30; i++)
{
cout << setw(10) << fib[i];
if ( i % 5 == 0)
cout << endl;
}
cout << endl;

i got a lil bug it outputs a 1 and then it starts again with 1 and outputs the correct info
• 11-18-2004
Zach L.
Use code tags...

Anyway, I don't quite understand the problem. Is the formatting correct?
The Fibonacci sequence itself starts with a pair of ones: { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc }
• 11-18-2004
chocha7
yes it does but the first 1 is on top n then the other 1 starts off the output correctly
• 11-18-2004
Zach L.
Code:

```if((i + 1) % 5 == 0) ... or ... if(i % 5 == 4)```
That ought to do it.
• 11-18-2004
chocha7
ahh yes how dum of me...0 % 5 is zero lol....yup the first one does it...thanx fo the help
• 11-18-2004
Siggy
Zach L. -
The Fibonacci sequence itself starts with a pair of ones: { 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, etc }

umm the Fibonacci sequence starts with a 0.............its quite essential
• 11-18-2004
Zach L.
Well, it can be convenient to define the 0th Fibonacci number F[0] = 0, and in fact, if you use the closed form function to generate Fibonacci's, then F(0)=0, and F(1)=1, so the first (1th) term is really 1, although you can put a 0 before it (obviously) without modifying the sequence.

So, F(n) = [(1+sqrt(5))^n - (1-sqrt(5))^n]/[sqrt(5) * 2^n]
Plugging in:
F(0)=0
F(1)=1
etc

Cheers