Java Program to Generate 30 Terms of Fibonacci number in an Array

In the previous article, we have seen Java Program to Check if Array is Empty

In this article we are going to see how to generate 30 terms of Fibonacci number in an Array using Java programming language.

Java Program to Generate 30 Terms of Fibonacci number in an Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to generate 30 terms of Fibonacci number in an Array.

Method-1: Java Program to Generate 30 Terms of Fibonacci number in an Array By Using Iterative Method

Approach:

  1. Initialize the array of size 30.
  2. Initialize first two value to 1.
  3. Loop through the array.
  4. Assign the next value to the sum of the previous two values.
  5. Print the array.

Program:

public class Main
{
    public static void main(String[] args) 
    {
        // initialize array of size 30
        long[] fibonacci = new long[30];
        // initialize first two values  to 1
        fibonacci[0] = 1;
        fibonacci[1] = 1;
        // loop through the array
        for (int i = 2; i < fibonacci.length; i++) 
        {
            // assign the next value to the sum of the previous two values
            fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
        }
        System.out.print("The fibonacci Series = ");
        for(long i: fibonacci) 
        {
            System.out.print(i + " ");
        }

    }

}
Output:

The fibonacci Series = 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229 832040

Method-2: Java Program to Generate 30 Terms of Fibonacci number in an Array By Using Recursive method

Approach:

  1. Initialize the array of size 30.
  2. Initialize first two values to 1.
  3. Run a for loop from i= 2->29.
  4. Call the generate30Fibonacci() user defined method for each value of i and update that value at the ith index of the array.

Program:

public class Main 
{
    public static void main(String[] args) 
    {
        int[] fibonacci = new int[30];
        fibonacci[0] = 0;
        fibonacci[1] = 1;
        for (int i = 2; i < fibonacci.length; i++) 
        {
            fibonacci[i] = generate30FibonacciNumbers(i);
        }
        System.out.println("The fibonacci series = ");
        for (int i : fibonacci) {
            System.out.print(i + " ");
        }
    }

    static int generate30FibonacciNumbers(int n) 
    {
        if (n <= 1)
            return n;
        return generate30FibonacciNumbers(n - 1) + generate30FibonacciNumbers(n - 2);
    }

}
Output:

The fibonacci series = 
0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025 121393 196418 317811 514229

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Related Java Programs: