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

for (int i = 0; i <= 5; i++) { previousTime = currentTime; System.out.println((n

ID: 3683875 • Letter: F

Question

   for (int i = 0; i <= 5; i++)

       {

           previousTime = currentTime;

           System.out.println((number + i )+ " is: ");

           System.out.println(IterationFibonacci.fib(number + i));

           currentTime = System.currentTimeMillis();

          

           timeElapsed = currentTime - previousTime;

           double timeElapsedSeconds = (currentTime - previousTime)/1000;

           System.out.println("computed in:" +timeElapsed+ " miliseconds or "

                                           +timeElapsedSeconds+ " seconds.");

       }

How to I ask user to enter 1 or 2?

If user enter 1: This algorithm runs in above loop

public static long fib(long n)

   {

       if(n<= 1)

           return 1;

       else

           return fib(n-2) + fib(n-1);

   }

If user enter 2: this algrithm runs in above loop

public static long fib(long n)

   {

if ((n == 0) || (n == 1))

      return 1;

   else

      {

      long prev = 1, current = 1, next = 0;

      for (int i = 2; i <= n; i++) {

      next = prev + current;

      prev = current;

      current = next;

      }

      return next;

      }

      }

Explanation / Answer

Hi , you can modify you for loop this way to get user input choice:

Scanner sc = new Scanner(System.in); // this is to get user input
       for (int i = 0; i <= 5; i++)

   {

   previousTime = currentTime;

   System.out.println((number + i )+ " is: ");

   System.out.println("Enter 1. Recursive Fibonacci 2.Iterative Fibonacci");
   int choice = sc.nextInt();
     
   if(choice == 1){
       System.out.println(RecursionFibonacci.fib(number + i));
   }
   else if(choice == 2){
       System.out.println(IterationFibonacci.fib(number + i));
   }

   currentTime = System.currentTimeMillis();

  

   timeElapsed = currentTime - previousTime;

   double timeElapsedSeconds = (currentTime - previousTime)/1000;

   System.out.println("computed in:" +timeElapsed+ " miliseconds or "

   +timeElapsedSeconds+ " seconds.");

   }