JAVA PROGRAMING Please write a method that would take in an

 JAVA PROGRAMING  Please write a method that would take in an integer and return the Fibonacci number for that number in the sequence, for example if you put in 10, it will tell you the 10th Fibonacci number. Forgot what the Fibonacci sequence is? 0, 1, 1, 2, 3, 5, 8, 13, 21,34... 

Solution

FibonacciNumber.java

import java.util.Scanner;


public class FibonacciNumber {

  
   public static void main(String[] args) {
       Scanner scan = new Scanner(System.in);
       System.out.println(\"Enter an integer number n: \");
       int n = scan.nextInt();
       int nth = getFibonacci(n);
       System.out.println(n+\"th fibonaci is: \"+nth);      
      
   }
   public static int getFibonacci(int n)
   {
       int c=0;
   int a = 0;
   int b = 1;
   for (int i = 2; i <= n; i++)
   {
   c = a + b;
   a = b;
   b = c;
   }
   return c;
       }
}

Output:

Enter an integer number n:
10
10th fibonaci is: 55

 JAVA PROGRAMING Please write a method that would take in an integer and return the Fibonacci number for that number in the sequence, for example if you put in

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site