Java Implementation Fibonacci Algorithm example _java

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.