The Fibonacci number series looks like this 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55
ID: 3834026 • Letter: T
Question
The Fibonacci number series looks like this 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55 Write a program that allows the user to request a particular Fibonacci number, and then your program will calculate what it is. For instance, if the user requested the oth Fibonacci number, your program should print 0. If they request the 7th Fibonacci number, your program should print 13. It's strange, but there is a 0th Fibonacci number (0), so make sure your program handles this correctly. The 1st and 2nd Fibonacci numbers are 1, the 3rd Fibonacci number is 2, the 4th Fibonacci number is 3, and so on The first two numbers of the Fibonacci number series are always 0 and 1 Each successive number in the series is generated by adding the previous two Write a static method fibcalc that takes one int argument (the requested Fibonacci number) and returns a double value (the actual Fibonacci number itself). Use data type double throughout your program so you can process large Fibonacci numbers You should be able to ask for Fibonacci number 70 and get 190392490709135 as your result. Your program needs to validate that it is possible to calculate the Fibonacci number they are requesting. In other words, make sure that they enter a non-negative 0) number less than or equal to 70 before proceeding. Your program must have a mai method Goals Use your knowledge of methods to solve this program Use loops to solve the Fibonacci number series Use the double data type to process large Fibonacci numbers. Class and File Naming Name your class NetID Fibon and source file Net ID ci java, using your U NetID. For example, acci for Loki account h, they would create a class j smith FibonacciExplanation / Answer
import java.util.*;
class NetID_Fibonacci
{
public static void main (String[] args)
{
int n;
Scanner scan = new Scanner(System.in);
do
{
System.out.println(" Which fibonacci number would you like?");
n = scan.nextInt();
if(n>=0 && n<=70)
break;
}while(n<0 || n>70);
System.out.printf(" Fibonacci #%d is %.0f",n,fibcalc(n)); // call function
}
public static double fibcalc(int n)
{
double n1,n2,n3;
n3 = 0;
n1 = 0;
n2 = 1;
if(n == 0)
return 0;
else if(n == 1)
return 1;
else
{
for(int i=0;i<n-1;i++) //compute fibonacci series term
{
n3 = n1+n2;
n1 = n2;
n2 = n3;
}
}
return n3;
}
}
Output:
Which fibonacci number would you like? -1
Which fibonacci number would you like? 93
Which fibonacci number would you like? 8
Fibonacci #8 is 21
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.