Advertisement
brilliant_moves

FiftyFibs.java

Oct 7th, 2014
297
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 0.54 KB | None | 0 0
  1. import java.util.Stack;
  2.  
  3. public class FiftyFibs {
  4.  
  5.     /*
  6.         Display the first 50 Fibonacci numbers in descending order.
  7.         Use a stack to store these numbers.
  8.     */
  9.  
  10.     public static void main(String[] args) {
  11.         Stack<Long> stack=new Stack<Long>();
  12.         long a=1, b=1, temp;
  13.         stack.push(a);
  14.         stack.push(b);
  15.         for (int i=2; i<50; i++) {
  16.             temp = a;
  17.             a = b;
  18.             b += temp;
  19.             stack.push(b);
  20.         } // end for
  21.  
  22.         while (!stack.isEmpty()) {
  23.             System.out.print(stack.pop() + " ");
  24.         } // end while
  25.     } // end main()
  26. } // end class FiftyFibs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement