Write a program to calculate the first 10 Fibonacci numbers and store the results in a one-dimensional array. In a second array calculate and store the average values of the adjacent numbers in the series. The first array should contain integer values and the second floating point values. Output the contents of both arrays in a neat format
public static void main(String[] args) {
//number of elements to generate in a series
int limit = 10;
long[] series = new long[limit];
//create first 2 series elements
series[0] = 1;
series[1] = 1;
//create the Fibonacci series and store it in an array
for(int i=2; i < limit; i++){
series[i] = series[i-1] + series[i-2];
}
//print the Fibonacci series numbers
System.out.println("Fibonacci Series upto " + limit);
for (int i = 0; i < limit; i++) {
System.out.print(series[i] + " ");
}
}
Okay so the first part is working fine but now to create an array to calculate the average is a bit tricky for me.So far I tried this.
int[] numbers = new int[]{1,1,2,3,5,8,13,21,34,55};
int sum=0;
for (int i = 0; i < numbers.length ; i++) {
sum = (int) (sum + numbers[i]);
double average = (double)sum/numbers.length;
System.out.println("Average value of array elements is : " + average);
}
But its not working quite well.Can someone offer me some light on this ?
averagecalculation and printing outside the loop.(series[i] + series[i+1])/2.0