PDA

View Full Version : Resolved Print output on 3 lines


onedollartotown
03-08-2009, 12:54 PM
Hi Guys,

I have this Fibonacci program. I can't get it to print on 3 lines.
I want it to be:-
1 1 2 3 5 8 13 21 34 55
89 144 233 377 610 987 1597 2584 4181 6765
10946 17711 28657 46368 75025 121393 196418 317811 514229 832040
Currently i have all this on one line.

public class Fibonacci2 {
public static void main(String[] args) {
int n0 = 1, n1 = 1, n2;
System.out.print(n0 + " " +
n1 + " ");

for (int i = 0; i < 10; i++) {
n2 = n1 + n0;
System.out.print(n2 + " ");
n0 = n1;
n1 = n2;
}

for (int i = 0; i < 8; i++) {
n2 = n1 + n0;
System.out.print(n2 + " ");
n0 = n1;
n1 = n2;
}

for (int i = 0; i < 10; i++) {
n2 = n1 + n0;
System.out.print(n2 + " ");
n0 = n1;
n1 = n2;
}
System.out.println();
}
}

Thanks for your help.

Regards,
John

Old Pedant
03-08-2009, 09:12 PM
Just add two more System.out.println(); calls at the appropriate spots. "println" is shorthand for "print LINE".

There are other ways, but that's the easiest.