Based on user input(number of rows) to print fibonacci series in diamond pattern. The user will enter the number of rows and the output should contain those many rows printing the Fibonacci series
import java.util.*;
public class HelloWorld {
public static void main(String []args) {
Scanner sc = new Scanner(System.in);
//Taking noOfRows value from the user
System.out.println("Enter no of rows:");
int noOfRows = sc.nextInt();
//Initializing rowCount with 1
int rowCount = 1;
//Implementing the logic
int previous = 0, prevtoprev;
for (int i = noOfRows; i > 0; i--) {
//Printing i*2 spaces at the beginning of each row
for (int j = 1; j <= i*2; j++) {
System.out.print(" ");
}
//Printing j value where j value will be from 1 to rowCount
for (int j = 1; j <= rowCount; j++) {
if (j%2 == 0) {
System.out.print("+");
} else {
System.out.print(j);
}
}
//Printing j value where j value will be from rowCount-1 to 1
for (int j = rowCount-1; j >= 1; j--) {
if (j%2 == 0) {
System.out.print("+");
} else {
System.out.print(rowCount);
}
}
System.out.println();
//Incrementing the rowCount
previous = rowCount;
rowCount++;
}
}
}
Getting output as
Expected output:
Here “34” was split to place “4” into the next row to form a diamond and “144” is split to place “1” in the last row and remaining digit ‘44’ is discarded.