Java Program to Print a Fibonacci Pyramid

Introduction

A Fibonacci pyramid is a pattern where the numbers displayed follow the Fibonacci sequence, arranged in a pyramid format. The Fibonacci sequence is a series where each number is the sum of the two preceding ones, starting from 0 and 1. In this guide, we will write a Java program to print a Fibonacci pyramid.

Problem Statement

Create a Java program that:

  • Accepts the size of the pyramid.
  • Prints a Fibonacci sequence in a pyramid format.

Example:

  • Input: size = 5
  • Output:
        0
       1 1
      2 3 5
     8 13 21 34
    55 89 144 233 377
    

Solution Steps

  1. Generate the Fibonacci Sequence: First, generate the required Fibonacci numbers based on the pyramid size.
  2. Use Nested Loops: Use nested loops to print the Fibonacci numbers in a pyramid structure.
  3. Display the Fibonacci Pyramid: Print the Fibonacci sequence in a pyramid format based on the size input.

Java Program

// Java Program to Print a Fibonacci Pyramid
// Author: [Your Name]

import java.util.Scanner;

public class FibonacciPyramid {

    public static void main(String[] args) {
        // Step 1: Accept the size of the pyramid
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the size of the Fibonacci pyramid: ");
        int size = sc.nextInt();

        // Step 2: Calculate how many Fibonacci numbers are required
        int totalNumbers = size * (size + 1) / 2;
        int[] fibonacci = new int[totalNumbers];

        // Step 3: Generate the Fibonacci sequence
        fibonacci[0] = 0;
        if (totalNumbers > 1) {
            fibonacci[1] = 1;
        }
        for (int i = 2; i < totalNumbers; i++) {
            fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2];
        }

        // Step 4: Print the Fibonacci Pyramid
        int index = 0;
        for (int i = 1; i <= size; i++) {
            // Step 5: Print spaces for alignment
            for (int j = i; j < size; j++) {
                System.out.print("  ");
            }

            // Step 6: Print Fibonacci numbers for each row
            for (int j = 1; j <= i; j++) {
                System.out.print(fibonacci[index] + " ");
                index++;
            }

            // Move to the next line after each row
            System.out.println();
        }

        // Closing the scanner object
        sc.close();
    }
}

Explanation

Step 1: Input Size

  • The program starts by taking input for the pyramid size using a Scanner object.

Step 2: Calculate the Total Fibonacci Numbers

  • The total number of Fibonacci numbers required is calculated using the formula size * (size + 1) / 2. This is because each row contains an increasing number of elements, forming a triangular number sequence.

Step 3: Generate Fibonacci Sequence

  • The Fibonacci numbers are stored in an array. The first two numbers in the Fibonacci sequence are set manually (0 and 1), and the rest are calculated using the formula fibonacci[i] = fibonacci[i - 1] + fibonacci[i - 2].

Step 4: Print the Pyramid

  • The outer loop controls the rows of the pyramid. For each row, the inner loops print the appropriate number of spaces for alignment and then print the Fibonacci numbers.

Step 5-6: Printing Spaces and Numbers

  • Spaces are printed to align the pyramid. Then, Fibonacci numbers from the precomputed array are printed in increasing order for each row.

Output Example

For size = 5, the output is:

        0
      1 1
    2 3 5
   8 13 21 34
 55 89 144 233 377

For size = 6, the output is:

         0
       1 1
     2 3 5
   8 13 21 34
 55 89 144 233 377
610 987 1597 2584 4181

Conclusion

This Java program prints a Fibonacci pyramid by first generating the required Fibonacci numbers and then arranging them in a pyramid format. This exercise demonstrates the use of loops, array manipulation, and pattern printing in Java, which are essential concepts for beginner and intermediate programmers.

Comments