Advertisement
Go-Ice

Sophomore Java Homework-P4.16(bottom-up)

Oct 14th, 2014
241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.88 KB | None | 0 0
  1. /**
  2.  * Name: Fibonacci numbers-function method (bottom-up)
  3.  * @author LinChuWen
  4.  * Date: 2014.10.14
  5.  *
  6.  * NCHU EE,course number:2335
  7.  * course name: Object Oriented Language
  8.  * Textbook: Big Java:Late Objects-Cay S. Horstmann
  9.  * Problem: P4.16
  10.  * Description: Enter an integer "n", then prints the nth Fibonacci number.
  11.  */
  12. import java.util.*;
  13. public class HW3_P4_16_function {
  14.    
  15.     public static void main(String[] args) {
  16.         Scanner input = new Scanner(System.in);
  17.        
  18.         while(input.hasNext()){
  19.             int n = input.nextInt();
  20.             System.out.println(fibonacci(n));
  21.         } //while end
  22.        
  23.         input.close();
  24.     } //main end
  25.    
  26.    
  27.     static long fibonacci( long number ){
  28.         long[] Fib = new long[1000];
  29.         Fib[0]=0;Fib[1]=Fib[2]=1;
  30.        
  31.         for(int cnt=2;cnt<=number;cnt++){
  32.             Fib[cnt]=Fib[cnt-1]+Fib[cnt-2];
  33.         } //for end
  34.        
  35.         return Fib[(int) number];
  36.     } //fibonacci() end
  37.    
  38. } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement