Ticker

6/recent/ticker-posts

Header Ads Widget

Fibonacci series in java

Fibonacci series in java


Introduction
Before starting a program it is important to learn what is Fibonacci series so for an idea of that that's let see its definition , definition of Fibonacci series.


Q) what is Fibonacci series?

Ans) Fibonacci series is a set of number in which the preceding number is sum of its previous number.
Let's take an example.

2 4 6 10 16 .......

1 2 3 5 8 13.........

So now you must be clear what is Fibonacci series, so let's get started.

In this tutorial we gonna make two programs one in which the variables are predefined and another program in which we give a user chance to put an number so get a Fibonacci series of its choice.


Q) write a Java program to create a Fibonacci series?

Sol.)


public class fibonacciSeries {
public static void main(String[] args) {
int x1=0,x2=1,x3,n=10;
System.out.print(x1); 
for(int i=0;i<n;++i)
{
        x3=x1+x2;    
       
           System.out.print(" "+x3);    
        
            x1=x2;    
            x2=x3 ;
}
}
}



We created code for Fibonacci series above in this first we declare some variables so that we can provide first two numbers to the series so that a program can use it to create rest of the the series afterwards , v we have a for loop in which use and as the size of window accessories and other part of the code is for creating a Fibonacci series and for creating output.

x1=x2;
x2=x3;


Where we use this to transfer the next number to the previous so that with every time loop runs have a different set off value.

Let's see the output of the following code and understand how the program will execute on  our device.

Output: 


0 1 2 3 5 8 13 21 34 55 89





Now we gonna make another program in which we give a user to chance to input the first two number of a Fibonacci series and create its own  Fibonacci series of it choice and we also allows users to to set the size of Fibonacci series.

Let's get started!!!


Q 2) write a program to create a Fibonacci series using user input.

Sol.





import java.util.Scanner;
public class fibonacciSeries {

public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x1,x2,x3,n;
System.out.print("enter the first number of series"); 
x1= in.nextInt();
System.out.print("enter the first number of series"); 
x2= in.nextInt();
System.out.print("enter the size of Fibonacci series"); 
n= in.nextInt();
  System.out.print(x1);  
for(int i=0;i<n;++i)
{
        x3=x1+x2;    
       
           System.out.print(" "+x3);    
        
            x1=x2;    
            x2=x3 ;
}
}
}



So as you can see above we created a Fibonacci series in this program first we initialise some variable so that we can store input of the user to use them for creating Fibonacci series.

After that we allow user to enter the first number and store it into X1 and after that we allow user to enter second number and store into X2 and the next part of the code allows user to to enter the size of series of its choice and afterwards we use a for loop and create a Fibonacci series using that the rest code is same as above we use in example 1.

Let's without wasting a time see what the output will be of the program.




Output:



enter the first number of series1
enter the first number of series2
enter the size of Fibonacci series10
1 3 5 8 13 21 34 55 89 144 233


now showing the output of the program you will be clearly understand how to create a Fibonacci series.


Post a Comment

0 Comments