Advertisement
Go-Ice

Sophomore Java Homework-P4.16

Oct 14th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.79 KB | None | 0 0
  1. /**
  2.  * Name: Fibonacci numbers-loop method
  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_loop {
  14.    
  15.     public static void main(String[] args) {
  16.         Scanner input = new Scanner(System.in);
  17.         long fold1,fold2,fnew;
  18.        
  19.         while(input.hasNext()){
  20.             int n = input.nextInt();
  21.             fold1 = 0; fold2 = 1; fnew = 0;
  22.            
  23.             for(int cnt=1;cnt<=n;cnt++){
  24.                 fnew = fold1 + fold2;
  25.                 fold2 = fold1;
  26.                 fold1 = fnew;                  
  27.             } //for end
  28.            
  29.             System.out.println(fnew);
  30.         } //while end
  31.        
  32.         input.close();
  33.     } //main end   
  34. } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement