Fibonacci series program in Java without using recursion.

Java8Java Programming Object Oriented Programming

Following is the required program.

Example

Live Demo

public class Tester {
   public static void main(String args[]) {
       int n1 = 0, n2 = 1, n3, i, max = 5;
       System.out.print(n1 + " " + n2);
       for (i = 2; i < max; ++i) {
          n3 = n1 + n2;
          System.out.print(" " + n3);
          n1 = n2;
          n2 = n3;
       }
    }
 }

Output

0 1 1 2 3
raja
Published on 22-Apr-2018 18:07:48
Advertisements