Advertisement
scottashipp

Straightforward Fibonacci

Jun 25th, 2013
359
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.69 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Iterator;
  3. import java.util.Scanner;
  4.  
  5.  
  6. public class Fibonacci {
  7.    
  8.     public static void main(String[] args) {
  9.         System.out.println("Please enter the number of Fibonacci numbers to display: ");
  10.         Scanner s = new Scanner(System.in);
  11.         Integer n = s.nextInt();
  12.        
  13.         ArrayList<Long> fib = new ArrayList<Long>();
  14.        
  15.         for(int i=0; i < n; i++) {
  16.             if(i < 2) {
  17.                 fib.add((long)i);
  18.             }          
  19.             else {
  20.                 fib.add( fib.get(i-2) + fib.get(i-1));
  21.             }
  22.         }
  23.        
  24.         Iterator<Long> iter = fib.listIterator();
  25.         if(iter.hasNext())
  26.             System.out.print(iter.next());
  27.         while(iter.hasNext()) {
  28.             System.out.print(", " + (Long)iter.next());        
  29.         }
  30.     }
  31.  
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement