1
1
2
3
5
8
13
Write a function which takes as input an integer x an returns an array of integers filled with fibonacci numbers in order.
First declare the array then fill it with the appropriate numbers by iterating from the smallest to the biggest.
public class Fibonacci {
public static int[] getSeries(int x){
int[] result = new int[x];
result[0]= 1;
result[1]= 1;
for (int i=2; i < x; i++){
result[i] = result[i-1] + result[i-2];
}
return result;
}
public static void main (String [] args){
int[] fib = Fibonacci.getSeries(20);
for (int i : fib){
System.out.println(i);
}
}
}

0 comments:
Post a Comment