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.  
  18. // 0,1,1,2,3,5
  19.  
  20. for (int i = 0; i < n; i++) {
  21.  
  22. System.out.print(a + " ");
  23.  
  24. // int temp = b;
  25.  
  26. // a = b;
  27.  
  28. int temp = a +b;
  29. b = a;
  30. a=temp;
  31.  
  32.  
  33. }
  34.  
  35. }
  36. }
  37.  
  38. class Ideone
  39. {
  40. public static void main (String[] args) throws java.lang.Exception
  41. {
  42. FibonacciPrinter p = new FibonacciPrinter();
  43. p.printFibonacci(10);
  44. }
  45. }
  46.  
  47.  
  48.  
Success #stdin #stdout 0.13s 55668KB
stdin
Standard input is empty
stdout
0 1 1 2 3 5 8 13 21 34