Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Stack;
- public class FiftyFibs {
- /*
- Display the first 50 Fibonacci numbers in descending order.
- Use a stack to store these numbers.
- */
- public static void main(String[] args) {
- Stack<Long> stack=new Stack<Long>();
- long a=1, b=1, temp;
- stack.push(a);
- stack.push(b);
- for (int i=2; i<50; i++) {
- temp = a;
- a = b;
- b += temp;
- stack.push(b);
- } // end for
- while (!stack.isEmpty()) {
- System.out.print(stack.pop() + " ");
- } // end while
- } // end main()
- } // end class FiftyFibs
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement