Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The Fibonacci sequence was the solution to a population growth problem proposed

ID: 3587326 • Letter: T

Question

The Fibonacci sequence was the solution to a population growth problem proposed and solved in the 12th - 13th century by the Italian mathematician Fibonacci. Write a program called Fibonaccito display the first 20 Fibonacci numbers F(n), where F(n)=F(n–1)+F(n–2) and F(1)=F(2)=1. In other words, the Fibonacci sequence starts with 1 (some people say 0) and each following number is the sum of the previous two numbers. 0 + 1 = 1, 1 + 1 = 2, 1 + 2 = 3, 2 + 3 = 5 and so on. In addition to calculating the 20 numbers, also compute their average. The output shall look like:

Dont forget to add comments to explain what you are doing.

Explanation / Answer

Fibonacci.java

public class Fibonacci {

public static void main(String[] args) {

int n1 = 0, n2 = 1, n3, i, count;

count = 20;

System.out.println("The first "+count+" Fibonacci numbers are:");

System.out.print(n1 + ", " + n2);

int sum = n2;

  

for(i = 2; i <= count; ++i)

  

{

n3 = n1 + n2;

sum += n3;

System.out.print(", " + n3);

n1=n2;

n2=n3;

  

}

System.out.println();

System.out.println("The average is "+(sum/(double)20));

}

}

Output:

The first 20 Fibonacci numbers are:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765
The average is 885.5

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote