<< All Posts
How to prints the Fibonacci sequence from 0 to 100 using JAVA


I’m studying again some about JAVA. I’ve gotten some interesting codes and I’d like to share with you guys some of it. This code prints the Fibonacci sequence from 0 to 100 using Java.

package myJavaCodes;

public class example4 {

    public static void main(String[] args) {
        // Prints Fibonacci sequence from 0 until 100

        int oneNumberBefore = 1;
        int twoNumbersBefore = 1;
        int nextNumber = 1;
        int y = 1;
        
        while(true)
        {
            if (y > 2)
            {
                nextNumber = oneNumberBefore + twoNumbersBefore;
                twoNumbersBefore = oneNumberBefore;
                oneNumberBefore = nextNumber;                
            }
            
            if (nextNumber >= 100)
            {
                break;
            }
            
            System.out.println(nextNumber);
            y++;
        }
    }

}

And we got the follow output code:

1
1
2
3
5
8
13
21
34
55
89



<< All Posts

rodolfo.io

🇧🇷 🇨🇦
Runs on OpenBSD 🐡