Java Program to Display Fibonacci Series

In this example, we are giving a program to display fibonacci series.


To display fibonacci series:

  1. # To display fibonacci series
  2. public class Fibonacci {
  3. public static void main(String[] args) {
  4. int n = 10, t1 = 0, t2 = 1;
  5. System.out.print("First " + n + " terms: ");
  6. for(int i = 1; i <= n; ++i) {
  7. System.out.print(t1 + ", ");
  8. int sum = t1 + t2;
  9. t1 = t2;
  10. t2 = sum;
  11. }
  12. }
  13. }

Output:

0, 1, 1, 2, 3, 5, 8, 13, 21, 34,