I'm trying to generate a code that will display the Fibonacci sequence using an array I did generate a code that has the right solution but I think it's too long since the instructor told us it's gonna be a maximum of 5 lines of code So here is the method :
public static void fibonacci_array_calc(int[] array) {
int result;
System.out.println("Fibonacci Series of " + array.length + " numbers: ");
if (array.length == 0 || array.length == 1) {
for(int i = 0;i < array.length; i++)
System.out.print(array[i] + " ");
} else {
for (int i = 0; i <= 1; i++) {
result = array[i] = i;
System.out.print(result + " ");
if (array[i] >= 1) {
for (int j = 2; j < array.length; i++, j++) {
result = (array[j] = (array[i] + array[i - 1]));
System.out.print(result + " ");
}
}
}
}
}
the output is
Fibonacci Series of 10 numbers:
0 1 1 2 3 5 8 13 21 34
it's not allowed to use the recursion technique is there any way to shorten this code?