Java Program Fibonacci Sequence

前端 未结 15 1852
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 03:17

I am writing a \"simple\" program to determine the Nth number in the Fibonacci sequence. Ex: the 7th number in the sequence is: 13. I have finished writing the program, it

15条回答
  •  执念已碎
    2020-12-17 03:33

    import java.util.*;
    public class FibonacciNumber
    {
    
      public static void main(String[] args)
      {
        int high = 1, low = 1;
        int num;
        Scanner in = new Scanner(System.in);
        try
        {
          System.out.print("Enter Number : " );
          num = in.nextInt(); 
          System.out.println( low);
          while(high < num && num < 2000000000)
          {
            System.out.println(high);
            high = low + high;
            low = high - low;
          }
         } catch (InputMismatchException e) {
           System.out.print("Limit Exceeded");
         }
       }
    }
    
    /* Ouput : 
    Enter Number : 1999999999
    1
    1
    2
    3
    5
    8
    13
    21
    34
    55
    89
    144
    233
    377
    610
    987
    1597
    2584
    4181
    6765
    10946
    17711
    28657
    46368
    75025
    121393
    196418
    317811
    514229
    832040
    1346269
    2178309
    3524578
    5702887
    9227465
    14930352
    24157817
    39088169
    63245986
    102334155
    165580141
    267914296
    433494437
    701408733
    1134903170
    1836311903
    -1323752223
    512559680
    -811192543
    -298632863
    -1109825406
    -1408458269
     1776683621
     368225352   */
    

提交回复
热议问题