Create a number pyramid that decreases in value but each iteration goes up by odd numbers

enter image description here

We we’re tasked to create a number pyramid that decreases its value as as it goes down however each iteration repeats it by odd numbers. The output that I’m trying to aim is:

    5
   444
  33333
 2222222
111111111

But it doesn’t print the complete pyramid, only those numbers from 5 to 3 get printed.

Here’s the code that I used:

import java.util.Scanner;

public class LoopExercise2 {

    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        
        int rows = 5;
        
        // row counter; total rows will be decided by the scanned input
        for (int counter = 1; counter <= rows ; ++counter) {
            //System.out.print("7");
            // space counter before the number; creates the pyramid effect
            for (int space = 0; space <= rows ; ++space ) {
                System.out.print(" ");
            }
            
            // per iteration, prints an odd number of values
            int n_counter = 0; // Initialize value to 0
            while (n_counter != 2 * counter - 1) {
                System.out.print(rows);
                ++n_counter;
            }
            // deducts 1 from the scanned input
            rows--;
            // prints a space to break the line
            System.out.println();
        }
    }
}

Answer

Try this.

public static void main(String[] args) {
    int rows = 5;
    for (int i = rows, j = 1; i > 0; --i, j += 2)
        System.out.println(" ".repeat(i) + Integer.toString(i).repeat(j));
}

output:

     5
    444
   33333
  2222222
 111111111