Practice Question 6

Home / Java Practice Question / Practice Question 6

Practice Question 6


Question 6. Write a Program to print fibonacci series to n.

Program


class Main {

  public static void main(String[] args) {

    int n = 10, firstTerm = 0, secondTerm = 1;

    System.out.println("Fibonacci Series till " + n + " terms:");

    for (int i = 1; i <= n; ++i) {

      System.out.print(firstTerm + ", ");

      int nextTerm = firstTerm + secondTerm;

      firstTerm = secondTerm;

      secondTerm = nextTerm;

    }

  }

}