Thursday, May 21, 2026
HomeArtificial IntelligenceProgram to Show Fibonacci Collection

Program to Show Fibonacci Collection

[ad_1]

fibonacci series in java

Fibonacci sequence refers back to the sequence the place the next quantity is the addition of the earlier two numbers. The primary two numbers of the sequence are often 0 and 1. 

Instance

enter= 9

output= 0,1,1,2,3,5,8,13,21

Right here, the primary quantity is 0 whereas the second is 1; therefore, the subsequent quantity could be the sum of those two numbers that’s 0+1=1. Equally 1+1=2; 1+2=3, 2+3=5; 3+5=8; 5+8=13; 8+13=21

There are other ways or strategies to show the Fibonacci sequence. 

Fibonacci Collection in Java with out utilizing recursion

We are able to keep away from the repeated work we carried out in recursion by the dynamic programming methodology. 

To carry out this, we’d like first to create an array arr[] of measurement N. Then, we have to initialize the array as arr[0]=0; arr[1]=1. After this we iterate the worth of i from 2 to N, and replace the array arr[] as 

arr[i]= arr[i-2] + arr[i-1]. 

Lastly, we print the worth N. 

This syntax is identical as the unique syntax of the Fibonacci sequence the one distinction is that we’ve used an array. 

The implementation is illustrated under:

class Fibonaccigreatlearning{
public static void predominant(String args[])
{  
 int n1=0,n2=1,n3,i,depend=15;  
 System.out.print(n1+" "+n2);//printing 0 and 1  
  
 for(i=2;i<depend;++i)//loop begins from 2 as a result of 0 and 1 are already printed  
 {  
  n3=n1+n2;  
  System.out.print(" "+n3);  
  n1=n2;  
  n2=n3;  
 }  

}}

Output

On this case, the time complexity and Auxiliary house are the identical: O (N).

Fibonacci Collection utilizing recursion in Java

There are some situations satisfying which we will use recursion in Java.

Firstly we would want the quantity whose Fibonacci sequence must be calculated 

Now recursively iterate the worth from N to 1. 

There are the next two instances in it:

Base case- right here, if the worth that is known as is lower than 1, then the operate returns 1 

Recursive call- the earlier two values are known as in case the bottom case will not be met. 

The syntax for which is as follows 

recursive_function (N-1) + recursive_function (N-2);

There’s additionally a time period known as recursive operate; it’s known as at every recursive name to return the earlier two values for the recursive operate.

Implementation:

class Fibonaccigreatlearning{
 static int n1=0,n2=1,n3=0;  
 static void printFibonacci(int depend){  
    if(depend>0){  
         n3 = n1 + n2;  
         n1 = n2;  
         n2 = n3;  
         System.out.print(" "+n3); 
         printFibonacci(count-1);  
     }  
 }  
 public static void predominant(String args[]){  
  int depend=20;  
  System.out.print(n1+" "+n2);//printing 0 and 1  
  printFibonacci(count-2);//n-2 as a result of 2 numbers are already printed 
 }
}

Output– 

Right here the time complexity and Auxiliary house are O(2^N) and O(1), respectively. 

Show Fibonacci Collection Utilizing For Loop

This loop is identical as that of the whereas loop. Firstly, we initialize numbers for the primary two digits, after which we print the sequence’s first time period, therefore computing the subsequent time period by the method of Fibonacci. Lastly, perform additional by assigning the worth of the second time period to the primary time period and the subsequent time period to the second time period.

Implementation:

class greatlearning{
  public static void predominant(String[] args) {

    int n =17 ,firstTerm = 0, secondTerm = 1;
    System.out.println("Fibonacci Collection until " + n + " phrases:");

    for (int i = 1; i <= n; ++i) {
      System.out.print(firstTerm + ", ");

      // compute the subsequent time period
      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;
    }
  }
}

Output

Show Fibonacci sequence utilizing Whereas loop

The whereas loop is the iterative operate the place the primary and second numbers are 0 and 1, respectively. We print these numbers after which ship them to the iterative whereas loop, the place the subsequent quantity is obtained by including the earlier two numbers. Then concurrently, we swap the numbers the place the primary quantity is the second quantity and the second quantity turns into the third.

We are able to implement the whereas loop as under

// Java program for the above method

class Greatlearning {

	// Perform to print N Fibonacci Quantity
	static void Fibonacci(int N)
	{
		int num1 = 0, num2 = 1;

		int counter = 0;

		// Iterate until counter is N
		whereas (counter < N) {

			// Print the quantity
			System.out.print(num1 + " ");

			// Swap
			int num3 = num2 + num1;
			num1 = num2;
			num2 = num3;
			counter = counter + 1;
		}
	}

	// Driver Code
	public static void predominant(String args[])
	{
		// Given Quantity N
		int N = 18;

		// Perform Name
		Fibonacci(N);
	}
}

Output

Right here the time complexity and auxiliary areas are O(N) and O(1), respectively.

Show the Fibonacci sequence as much as a given quantity

Within the earlier instances, we outlined the variety of which we printed the Fibonacci sequence, however on this case, we print the sequence as much as a quantity.

We achieve this by evaluating the primary quantity or time period with n, and if the primary quantity is proved to be lower than n, then the quantity is printed within the sequence. If not so, then the sequence is assumed to be accomplished. 

We are able to illustrate it as under

class greatlearning {
  public static void predominant(String[] args) {

    int n = 88, firstTerm = 0, secondTerm = 1;
        
    System.out.println("Fibonacci Collection Upto " + n + ": ");
    
    whereas (firstTerm <= n) {
      System.out.print(firstTerm + ", ");

      int nextTerm = firstTerm + secondTerm;
      firstTerm = secondTerm;
      secondTerm = nextTerm;

    }
  }
}

Output

Conclusion

With this, we come to the tip of this weblog. To continue to learn and upskilling your self, go to the Nice Studying Academy.

[ad_2]

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments