DEV Community

Danny
Danny

Posted on

2 1 1

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 :)

Billboard image

Create up to 10 Postgres Databases on Neon's free plan.

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Try Neon for Free →

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay