(Fibonacci Series) The Fibonacci series 0, 1, 1, 2, 3, 5, 8, 13, 21, ... begins
ID: 3630247 • Letter: #
Question
(Fibonacci Series) The Fibonacci series
0, 1, 1, 2, 3, 5, 8, 13, 21, ...
begins with the terms 0 and 1 and has the property that each succeeding term is the sum of the two preceding terms.
a) Write a method fibonacci( n ) that calculates the nth Fibonacci number. Incorporate this method into an application that enables the user to enter the value of n.
b) Determine the largest Fibonacci number that can be displayed on your system.
c) Modify the application you wrote in part (a) to use double instead of int to calculate and return Fibonacci numbers, and use this modified application to repeat part (b).
Explanation / Answer
please rate - thanks
generally methods are java. message me if it's not
import java.util.*;
public class fib{
public static void main(String[] args)
{
int n;
Scanner in=new Scanner(System.in);
System.out.print("Enter a value for n: ");
n=in.nextInt();
System.out.println("The "+n+"th fibonacci number is "+fibonacci(n));
}
public static int fibonacci(int n)
{
int first=0,second=1,next=0,i;
for(i=3;i<=n;i++)
{next=first+second;
//System.out.println(i+" "+next);
first=second;
second=next;
}
return next;
}
}
on my system, the largest fibonacci number was the 47th
used the commented out print to find this
import java.util.*;
public class fib{
public static void main(String[] args)
{
int n;
Scanner in=new Scanner(System.in);
System.out.print("Enter a value for n: ");
n=in.nextInt();
System.out.println("The "+n+"th fibonacci number is "+fibonacci(n));
}
public static double fibonacci(int n)
{
double first=0,second=1,next=0;
int i;
for(i=3;i<=n;i++)
{next=first+second;
//System.out.println(i+" "+next);
first=second;
second=next;
}
return next;
}
}
with a double it's 1477th
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.