Write a program to print Fibonacci Series from 1 to 100.

Here’s a simple Java program to print the Fibonacci series from 1 to 100:

				
					public class FibonacciSeries {

    public static void main(String[] args) {
        int a = 0, b = 1;
        int next = a + b;

        System.out.println("Fibonacci Series from 1 to 100:");

        // Print the initial term if it's within the range
        if (b <= 100) {
            System.out.print(b + " ");
        }

        while (next <= 100) {
            System.out.print(next + " ");
            a = b;
            b = next;
            next = a + b;
        }
    }
}

				
			

Explanation:

  1. Initialization: The variables a and b are initialized to 0 and 1, respectively, representing the first two terms of the Fibonacci series.
  2. Loop: A while loop is used to generate the next term in the series by adding the two previous terms (a and b). The loop continues as long as the next term is less than or equal to 100.
  3. Printing: Each term is printed if it is within the specified range (1 to 100).
  4. Updating Terms: After printing, the previous terms are updated to the next terms in the series for the next iteration.

Output:

When you run this program, the output will be:

				
					Fibonacci Series from 1 to 100:
1 1 2 3 5 8 13 21 34 55 89 

				
			

This output lists all the Fibonacci numbers from the series that fall within the range of 1 to 100.

Scroll to Top