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

Fibonacci Submit Assignment Due Wednesday by 11:59pmPoints 100 Submitting a file

ID: 3585802 • Letter: F

Question

Fibonacci Submit Assignment Due Wednesday by 11:59pmPoints 100 Submitting a file upload File Types java Available after Sep 29 at 9:50am The Fibonacci numbers are defined by the sequence f1 = 1 Reformulate that as fold1 -1; fold2 1; fnew-foldl fold2; After that, discard fold2 , which is no longer needed, and set fold2 to fold1 and fold1 to fnew. Repeat an appropriate number of times. Implement a java program that prompts the user for an integer n and prints the nth Fibonacci number, using the above algorithm. Hints: work by yourself. Do not search and copy online. Think and ask if you don't know how to do it.

Explanation / Answer

import java.util.Scanner;
public class fibo
{
   public static void main(String args[])
   {
       System.out.println("Enter an integer");
       Scanner in = new Scanner(System.in);
  
       int n = in.nextInt();
       int fold1 = 1;
       int fold2 = 1;
       int fnew = 0;
      
       if(n==1 || n==2)
       {
           System.out.println(n+"th Fibonacci number is"+ fold1);
       }
       else
       {
           int temp = n-2; //temporary variable used to calculate remaining number of fibonaci numbers to calculate

           for(int i = 1; i<=temp; i++)
           {
               fnew = fold1+fold2;
              
               fold2 = fold1;
               fold1 = fnew;
              
           }

           System.out.println(n+"th Fibonacci number is"+ fnew);
       }
   }
}