Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Name: Fibonacci numbers-loop method
- * @author LinChuWen
- * Date: 2014.10.14
- *
- * NCHU EE,course number:2335
- * course name: Object Oriented Language
- * Textbook: Big Java:Late Objects-Cay S. Horstmann
- * Problem: P4.16
- * Description: Enter an integer "n", then prints the nth Fibonacci number.
- */
- import java.util.*;
- public class HW3_P4_16_loop {
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- long fold1,fold2,fnew;
- while(input.hasNext()){
- int n = input.nextInt();
- fold1 = 0; fold2 = 1; fnew = 0;
- for(int cnt=1;cnt<=n;cnt++){
- fnew = fold1 + fold2;
- fold2 = fold1;
- fold1 = fnew;
- } //for end
- System.out.println(fnew);
- } //while end
- input.close();
- } //main end
- } //class end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement