Home » Blog » Find Fibonacci Series Of 1 to n

Find Fibonacci Series Of 1 to n

A series of numbers in which each number ( Fibonacci number ) is the sum of the two preceding numbers. The simplest is the series 1, 1, 2, 3, 5, 8,…. etc.

Here first two numbers are 0 and 1 taking by us by default. These two numbers are printed manually as part of series. After that the for loop will be executed until the length entered by user minus two, so that the two numbers that we use manually are subtracted from length. Then it will perform addition of these two numbers and print that number as part of series. Then it will perform swapping so that the two preceding numbers are use next time for addition and finally the Fibonacci Series obtained.

import java.util.*;
public class fibonacci
{
	public static void main(String args[])
	{
		Scanner sc=new Scanner(System.in);
		int a=0,b=1,c,l,i;
		System.out.println("***Fibonacci Series***");
		System.out.println("Enter last term = ");
		l=sc.nextInt();
		System.out.println("Series is Below...");
		System.out.println(a);
		System.out.println(b);
		for(i=0;i<10-2;i++)
		{
			c=a+b;
			System.out.println(c);
			a=b;
			b=c;
		}
	}
}

Here is the OutPut

***Fibonacci Series***
Enter last term = 
10
Series is Below...
0
1
1
2
3
5
8
13
21
34

 

Leave a Reply

Your email address will not be published.