The Fibonacci sequence was the solution to a population growth problem proposed
ID: 3588121 • 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 Fibonacci to 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.
***Fibonacci.C#_Visual basic**
Explanation / Answer
using System;
namespace fibonaci{
class Program{
static void Main(string[] args){
int i, range=20, f1 = 0, f2 = 1, f3 = 0;
float sum=0;
sum = sum + f1 + f2;
Console.WriteLine("The first 20 Fibonacci numbers are:");
Console.Write(f1+" "+f2);
//generate Fibonacci series as already 2 elements added range should be 2 lesser
for (i = 0; i <= range-2; i++){
f3 = f1 + f2;
Console.Write(" "+f3);
//update fibonacci values
f1 = f2;
f2 = f3;
//add fibonacci number to sum
sum = sum + f3;
}
//calculate average and print it
Console.WriteLine(" The average is "+sum/range);
}
}
}
/* sample 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
*/
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.