In: Computer Science
Complete java program below.
Complete non-recursive version nthFibonacciWithLoop() method.
Complete recursive version nthFibonacciWithRecursion() method.
public class Fibonacci {
// Fib(N): N N = 0 or N = 1
// Fib(N-1) + Fib(N-2) N > 1
// For example,
// Fib(0) = 0
// Fib(1) = 1
// Fib(2) = Fib(1) + Fib(0) = 1 + 0 = 1
// Fib(3) = Fib(2) + Fib(1) = Fib(2) + 1 = (Fib(1) + Fib(0)) + 1 = 1 + 0 + 1 =
2
// ...
// Fib(10) = (0), 1, 1, 2, 3, , 5, 8, 13, 21, 34, 55, ...
// Return the nth Fibonacci number.
public static int nthFibonacciWithLoop(int nth) {
}
public static int nthFibonacciWithRecursion(int nth) {
}
// Check first 30 Fibonicci numbers.
private static void check() {
int [] check = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181, 6765};
boolean correctLoop = true, correctRecursion = true;
for (int i = 0; i < 20; i++) {
if (nthFibonacciWithLoop(i) != check[i])
correctLoop = false;
if (nthFibonacciWithRecursion(i) != check[i])
correctRecursion = false;
}
if (correctLoop && correctRecursion)
System.out.println("\nYou got them right!");
else {
if (!correctLoop)
System.out.println("\nCheck nthFibonacciWithLoop()");
if (!correctRecursion)
System.out.println("\nCheck nthFibonacciWithRecursion()");
}
}
public static void main(String [] args) {
System.out.print("Enter N: ");
int n = new java.util.Scanner(System.in).nextInt();
for (int i = 0; i <= n; i++) {
System.out.print(nthFibonacciWithLoop(i) + " ");
}
System.out.println();
for (int i = 0; i <= n; i++) {
System.out.print(nthFibonacciWithRecursion(i) + " ");
}
check();
}
}
import java.util.*;
public class Main
{
public static int nthFibonacciWithLoop(int nth)
{
int i,a,b,s=0;
a=0;//initializing a to 0
b=1;// and b to 1
if(nth==0)//the first value is 0 so it returns 0 for n=0
return a;
for(i=1;i<nth;i++)//loops for other Numbers
{
s=a+b;//adding previous value to present
a=b;//exchanging values
b=s;
}
return b;//returning the present value of b
}
public static int nthFibonacciWithRecursion(int nth)
{
if(nth<=1)//end condition
{
return nth;//returns n value 0 or 1
}
return
nthFibonacciWithRecursion(nth-1)+nthFibonacciWithRecursion(nth-2);//fib(3)=fib(2)+fib(1)
}
private static void check() {
//initializing first 30 fibonacci series values to array
int [] check = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233,
377,
610, 987, 1597, 2584, 4181,
6765,10946,17711,28657,46368,75025,121393,196418,317811,514229,832040};
boolean correctLoop = true, correctRecursion = true;//boolean
values
for (int i = 0; i < 30; i++) //checking for first 30 values
through loop
{
if (nthFibonacciWithLoop(i) != check[i])
correctLoop = false;
if (nthFibonacciWithRecursion(i) != check[i])
correctRecursion = false;
}
if (correctLoop && correctRecursion)//if both of them are
true then w got them right
System.out.println("\nYou got them right!");
else//else it will give us which is giving wrong output loop or
recursion
{
if (!correctLoop)
System.out.println("\nCheck nthFibonacciWithLoop()");
if (!correctRecursion)
System.out.println("\nCheck nthFibonacciWithRecursion()");
}
}
public static void main(String[] args) {
int n,i;
Scanner s=new Scanner(System.in);
System.out.print("Enter N: ");
n=s.nextInt();
for(i=0;i<n;i++)
{
System.out.print(nthFibonacciWithLoop(i) + " ");
}
System.out.println();
for ( i = 0; i <= n; i++)
{
System.out.print(nthFibonacciWithRecursion(i) + " ");
}
check();
}
}