DEV Community

Danny
Danny

Posted on

Algorithms Challenge (1) How to print out Fibonacci series in two lines code

Print out Fibonacci numbers in range 0 - 100

Concept : print out two numbers every time

Code in java:

public class fibonacci {
   public static void main(String[] args) {
         for(int a = 0, b=1; a < 100 ; a+=b, b+=a)
             System.out.println(a + "," + (b<100 ? b + ",": ""));
    }
}
Enter fullscreen mode Exit fullscreen mode

Result :
0,1,
1,2,
3,5,
8,13,
21,34,
55,89,

Top comments (1)

Collapse
 
wozaisuzhou profile image
Danny

Welcome to add your comments :)