# Hi, I have to display the first 20 Fibonacci numbers to the console.The first two numbers are given, 0 and  1. The sequence continues adding the previous two numbers.0, 1, 1, 2, 3, 5, ...I have to generate this sequence using a for loop starting with the 3rd number. The first two numbers are given.   The numbers shall be formatted using the printf statement such that 5 numbers are displayed in a row.Once all of the numbers are derived and displayed to the console, I have to display the sum and the average of the 20 numbers.  Then I have to generate another sequence of numbers 1, 2, 4, 8, 16, 32, ...This also requires using a for loop.Then I have to calculate the sum and the average, and display it to the console.This is an absolutely imperfect coding. The other half is just me trying something that may work. I'll keep working on it while I wait on a response to see if I'm on the right track. Here's what I got so far:import java.util.Scannerpublic class PrSpecial { public static final int N = 20; public static void main(String[] args) {  int[] ary = new int[N];  ary=0; ary=1;  for (int i = 2; i < N; i++) {  ary[i] = ary[i-1]+ary[i-2];   }  for (int i = 0; i < N; i++) {   System.out.printf("ary[%d] = %3d\n", i, ary[i]);  } {   Scanner number = new Scanner(System.in);  int sum = 0;  System.out.print("Enter sequence") for(int i = 1; i<N; i+=2) {  ary[i]= number.nextInt();  su  System.out.print(i+" ");   }     } } }

Question
29 views

Hi, I have to display the first 20 Fibonacci numbers to the console.

The first two numbers are given, 0 and  1. The sequence continues adding the previous two numbers.

0, 1, 1, 2, 3, 5, ...

I have to generate this sequence using a for loop starting with the 3rd number. The first two numbers are given.

The numbers shall be formatted using the printf statement such that 5 numbers are displayed in a row.

Once all of the numbers are derived and displayed to the console, I have to display the sum and the average of the 20 numbers.

Then I have to generate another sequence of numbers 1, 2, 4, 8, 16, 32, ...

This also requires using a for loop.

Then I have to calculate the sum and the average, and display it to the console.

This is an absolutely imperfect coding. The other half is just me trying something that may work. I'll keep working on it while I wait on a response to see if I'm on the right track.

Here's what I got so far:

import java.util.Scanner
public class PrSpecial {

public static final int N = 20;

public static void main(String[] args) {

int[] ary = new int[N];

ary=0;
ary=1;

for (int i = 2; i < N; i++) {
ary[i] = ary[i-1]+ary[i-2];

}

for (int i = 0; i < N; i++) {

System.out.printf("ary[%d] = %3d\n", i, ary[i]);

}
{

Scanner number = new Scanner(System.in);

int sum = 0;

System.out.print("Enter sequence")
for(int i = 1; i<N; i+=2)
{
ary[i]= number.nextInt();
su
System.out.print(i+" ");
}

}

}

}
check_circle

Step 1

Program:

class PrSpecial
{

public static final int N = 20;

public static void main(String[] args)
{

int count=0;
int sumSec=0;
float secAvg;
float avg;
int[] ary = newint[N];

ary=0;
ary=1;

int sumFib =ary+ary;

for (int i = 2; i < N; i++)
{
ary[i] = ary[i-1]+ary[i-2];

sumFib+= ary[i];
}

System.out.println(" Displaying 5 numbers in a row: ");

for (int i = 0; i < N; i++)
{
System.out.printf(" "+ ary[i]);

count++;
if (count==5)
{
System.out.println();
count=0;

}

}

avg= (float)sumFib/N;

System.out.println(" The sum of fibonacci: "+sumFib);
System.out.println(" The average of fibonacci: "+avg);

System.out.println(" Printing the second sequence:");

for(int i = 1; i <= N; i...

### Want to see the full answer?

See Solution

#### Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

### Java 