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

java and recursive methode on it, not c++ Thank you Write a program that compute

ID: 3760305 • Letter: J

Question

java and recursive methode on it, not c++

Thank you

Write a program that computes the Fibonacci number for some input integer. (See segments 7.37 – 7.41 in your book for some info on the Fibonacci sequence or you can look it up on the Internet). The sequence is defined as the first two elements are each 1, after that each element in the sequence is the sum of the previous two elements. The first few numbers of the sequence are 1,1,2,3,5,8,13,21,34,55,… Your assignment is to write a program with a main method and two additional methods. The main method will request theindex into the Fibonacci sequence that is desired and verify that it is a positive integer. If it is not a positive integer, it will request that the use input a number again. Note that I will test this using letters, decimal numbers, negative integers, etc. and it must handle these problems without erroring off. It will do so until the use inputs a positive integer. It will then invoke the two methods each of which will compute the required Fibonacci number. One method will use recursion to do so and another will use iteration. Your main method will output the determined Fibonacci number from each of the methods (they had better be the same).

Explanation / Answer

import java.util.*;
class fib
{
   int f0=1,f1=1,f2=2;

   public void printFibonacci(int n)
   {


       if(n>0)
       {
            f2 = f0 + f1;
            f0 = f1;
            f1= f2;
            System.out.println(f2);
            printFibonacci(n-1);
       }

   }

   public void fibo(int n)
   {
       int i;
       f1=1;
       f0=1;
       f2=0;      
       System.out.println("1"+" "+"1");
       for(i=1;i<=n-2;i++)
       {
           f2=f0+f1;
           System.out.println(" "+f2);
           f0=f1;
           f1=f2;
       }
   }
}

class fibo_rec_iter
{
   public static void main(String args[])
   {

       int k,n;
       int i=0,j=1,f;
       fib o = new fib();

       Scanner scan = new Scanner

(System.in);
       System.out.println("Enter the range of

the Fibonacci series: ");
       n=scan.nextInt();

       System.out.println("Fibonacci Series:

");
       System.out.println("1"+" "+"1");
       o.printFibonacci(n);
       o.fibo(n);

   }
}