This article describes the Java implementation of the Fibonacci algorithm method. Share to everyone for your reference. as follows:
Package com.yenange.test2;
Import Java.util.Scanner;
public class Fibonacci {private static Scanner input = new Scanner (system.in);
public static void Main (string[] args) {System.out.println ("-----------1th algorithm------------");
int NUM1 = 1;
int num2 = 1;
int temp, count;
System.out.println ("Please enter the number (>=2) to be queried:");
Count = Input.nextint ();
System.out.println ("The 1th number is: 1");
System.out.println ("The 2nd number is: 1");
for (int i = 3; I <= count; i++) {temp = num2;
Num2 + = NUM1;
System.out.println ("First" + i + "number is:" + num2);
NUM1 = temp;
} System.out.println ("-----------2nd algorithm------------");
System.out.println ("First" + Count + "number is:" + cal (count));
System.out.println ("-----------3rd Algorithm------------");
int[] arr = new Int[count];
Arr[0] = 1;
ARR[1] = 1;
for (int i = 2; i < arr.length i++) {arr[i] = Arr[i-1] + arr[i-2]; System.out.println ("First" + (i + 1) + "number is:" + arr[i]);
} static int cal (int count) {if (Count <= 2) {return 1;
Return cal (Count-1) + cal (count-2);
}
}
I hope this article will help you with your Java programming.