Program for Fibonacci Series in Java

Here you will get program for fibonacci series in Java.

It is a series in which next term is obtained by adding previous two terms. A fibonacci series will look like 0 1 1 2 3 5 8 13 21 34 . . . .

Program for Fibonacci Series in Java

package com;

import java.util.Scanner;

class FibonacciSeries
{
	public static void main(String...s)
	{
		int first=0,second=1,third,n,i;
		Scanner sc = new Scanner(System.in);
		System.out.println("How many terms?");
		n = sc.nextInt();
		
		System.out.print("\n"+first+" "+second);
		for(i=2;i<n;i++)
		{
			third=first+second;
			first=second;
			second=third;
			System.out.print(" "+third);
		}
		
		sc.close();
	}
}

 

Output

How many terms?
10

0 1 1 2 3 5 8 13 21 34

1 thought on “Program for Fibonacci Series in Java”

  1. import java.util.*;

    public class Fibo
    {
    public static void main(String args[])
    {
    int num,num1=0,num2=1,num3,i;
    Scanner sc = new Scanner(System.in);
    System.out.println(” number of series = “);
    num = sc.nextInt();
    for(i=1;i>=num;i++)
    {
    num3=num1+num2;
    num3=num2;
    num2=num1;
    System.out.println(“number = “+num3);
    }
    }
    }

    above fibo series by using For loop

Leave a Comment

Your email address will not be published. Required fields are marked *