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]=0;  ary[1]=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+" ");    }          }   }   }       This is for java. Please include explanation with the steps so I can understand. I also didn't learn about about the variable of long yet. If you can use a different variable that would be great. Thanks!

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter5: Repetition Statements
Section: Chapter Questions
Problem 9PP
icon
Related questions
Question

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]=0;
 ary[1]=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+" ");  
 }
 
 
 
 
 }
 
}
 
}
 
 
 
This is for java. Please include explanation with the steps so I can understand. I also didn't learn about about the variable of long yet. If you can use a different variable that would be great. Thanks!
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Basics of loop
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
Microsoft Visual C#
Microsoft Visual C#
Computer Science
ISBN:
9781337102100
Author:
Joyce, Farrell.
Publisher:
Cengage Learning,
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning