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]=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+" ");   }     } } }

Question
Asked Oct 23, 2019
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]=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+" ");  
 }
 
 
 
 
 }
 
}
 
}
check_circle

Expert Answer

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]=0;
ary[1]=1;

int sumFib =ary[0]+ary[1];
 
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

Check out a sample Q&A here.

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

Engineering

Computer Science

Java

Related Computer Science Q&A

Find answers to questions asked by student like you

Show more Q&A add
question_answer

Q: A network topology specifies how computers, printers, and other devices are connected over a network...

A: The adjacency matrix shows the neighbor it is connected to, so for ring topology is all nodes are co...

question_answer

Q: I am trying to write a java code to see if the number entered by the user is a factor of the random ...

A: The following source code will print whether a number is a factor of generated random number or not ...

question_answer

Q: Write a wholly creative quiz program that consists of three original questions that you make up acco...

A: Error in the program-Remove the points variable after each fragment of code except the point variabl...

question_answer

Q: Create a new file (in Dev C++)  In Lab 2, you created a menu for a simple calculator program. In Lab...

A: Program AlgorithmDeclare the required variables and write a welcome message before starting the menu...

question_answer

Q: Question 2: Which statement(s) is/are true about vectors? a) A vector's size is static. b) A vector'...

A: VectorsA vector is an object that has both magnitude and direction. It is a directed line segment wh...

question_answer

Q: Compare and contrast cache and virtual memory and give an example scenario where each can be applied...

A: Cache memory:-It is the memory, which is very nearest to the CPU, all recent instruction is stored i...

question_answer

Q: (select all that apply) In MS Access, what kind of data will be accepted into database using theinpu...

A: Input mask:A string of characters which are used to indicate the format of valid input values in the...

question_answer

Q: I need some help arranging these countries in a C++ program : China: 1,433,783,686 India: 1,366,417,...

A: Program AlgorithmDeclare two arrays named country and population of size 14.Using for loop enter the...

question_answer

Q: (check all that apply) The ER model for a small bank system has Customer, Account, CheckingAccount, ...

A: The answer is option C - Transaction is a weak entity of Account.