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

JAVA PROGRAMMING Write a program that computes the Fibonacci number for some inp

ID: 3759951 • Letter: J

Question

JAVA PROGRAMMING

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 the index 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.Scanner;

public class FibonacciCalc {

public static void main(String args[]) {
  
//input to print Fibonacci series upto how many numbers
  
int number;
       Scanner sc = new Scanner(System.in);
      
       //System.out.println("Enter number upto which Fibonacci series to print: ");
               do {
System.out.println("Enter number upto which Fibonacci series to print: ");
while (!sc.hasNextInt()) {
System.out.println("That's not a number!");
sc.next(); // this is important!
}
           number = sc.nextInt();
       } while (number <= 0);
       //System.out.println("Thank you! Got " + number);
      
  
System.out.println("Fibonacci series upto " + number +" numbers : ");
//printing Fibonacci series upto number
for(int i=1; i<=number; i++){
System.out.print(fibonacci2(i) +" ");
}
  
  
}
  

/*
* Java program for Fibonacci number using recursion.
* This program uses tail recursion to calculate Fibonacci number for a given number
* @return Fibonacci number
*/
public static int fibonacci(int number){
if(number == 1 || number == 2){
return 1;
}
  
return fibonacci(number-1) + fibonacci(number -2); //tail recursion
}
  


/*
* Java program to calculate Fibonacci number using loop or Iteration.
* @return Fibonacci number
*/
public static int fibonacci2(int number){
if(number == 1 || number == 2){
return 1;
}
int fibo1=1, fibo2=1, fibonacci=1;
for(int i= 3; i<= number; i++){

//Fibonacci number is sum of previous two Fibonacci number
fibonacci = fibo1 + fibo2;   
fibo1 = fibo2;
fibo2 = fibonacci;
  
}
return fibonacci; //Fibonacci number
  
}   
  
}