1. Introduction
In this tutorial, we will develop a Java program that generates a Fibonacci pyramid, where the number of rows is determined by user input. The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones, typically starting with 0 and 1. This program not only introduces the Fibonacci sequence but also demonstrates how to apply it in a pyramid pattern, combining mathematical concepts with programming techniques.
2. Program Steps
1. Import the Scanner class to enable reading input from the user.
2. Prompt the user to enter the number of rows for the Fibonacci pyramid.
3. Initialize variables to hold the first two numbers of the Fibonacci sequence (0 and 1) and use these to generate the rest of the sequence.
4. Use nested loops: the outer loop to handle the rows and the inner loops to print spaces for alignment and the Fibonacci numbers.
5. Compile and run the program, then input the number of rows to see the Fibonacci pyramid.
3. Code Program
import java.util.Scanner; // Import Scanner class
public class FibonacciPyramid {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in); // Create Scanner object for input
System.out.print("Enter the number of rows: "); // Prompt for user input
int rows = scanner.nextInt(); // Read number of rows from user
int a = 0, b = 1; // Starting numbers of the Fibonacci sequence
for(int i = 1; i <= rows; i++) {
int aOld = a; // Temporary store for the first number
System.out.format("%" + (rows - i + 1) * 2 + "s", ""); // Align the pyramid
for(int j = 1; j <= i; j++) {
if(j == 1) {
System.out.print(a + " "); // Special case for the first number
} else {
int sum = a + b; // Calculate the next number in the sequence
System.out.print(b + " "); // Print the second number
a = b; // Move to the next pair in the sequence
b = sum;
}
}
a = aOld + b; // Update the starting number for the next row
b = a - b; // Correct the second number after row update
System.out.println(); // New line after each row
}
scanner.close(); // Close the scanner
}
}
Output:
Enter the number of rows: 4 0 1 1 2 3 5 8 13 21 34
Explanation:
1. The program starts by importing the Scanner class and prompting the user for the number of rows in the Fibonacci pyramid. This input defines how tall the pyramid will be.
2. It initializes two variables, a and b, with the first two numbers of the Fibonacci sequence (0 and 1). These variables are used to calculate the subsequent numbers in the sequence.
3. An outer loop iterates over each row of the pyramid. A temporary variable, aOld, is used to remember the value of a at the start of each row.
4. Inside the outer loop, a formatted print statement creates the necessary spacing to align the pyramid centrally. Then, an inner loop calculates and prints the Fibonacci numbers for each row. The special case of the first number in each row is handled separately to maintain the sequence's integrity.
5. After printing the numbers for a row, the variables a and b are updated to reflect the starting numbers for the next row of the pyramid, ensuring the correct continuation of the Fibonacci sequence.
6. Finally, the Scanner object is closed to prevent resource leaks, adhering to good programming practices.
Comments
Post a Comment
Leave Comment