fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8.  
  9.  
  10. class FibonacciPrinter {
  11.  
  12. public static void printFibonacci(int n) {
  13.  
  14. int a = 0;
  15.  
  16. int b = 1;
  17. System.out.print(a + " ");
  18.  
  19. if(n>1){
  20. System.out.print(b + " ");
  21. }
  22.  
  23. for (int i = 2; i < n; i++) {
  24. int temp = a+b;
  25. System.out.print(temp + " ");
  26. a = b;
  27. b = temp;
  28. }
  29.  
  30. }
  31. }
  32.  
  33. class Ideone
  34. {
  35. public static void main (String[] args) throws java.lang.Exception
  36. {
  37. FibonacciPrinter p = new FibonacciPrinter();
  38. p.printFibonacci(8);
  39. }
  40. }
  41.  
  42.  
  43.  
Success #stdin #stdout 0.12s 55728KB
stdin
Standard input is empty
stdout
0 1 1 2 3 5 8 13