Basic Square Incrementing Pattern

Program to Print Basic Square Incrementing Pattern

PRINTING PATTERN:

1111
2222
3333
4444
            

PREREQUISITE:

Basic knowledge of Java language and loops.

ALGORITHM:

1. Take the number of rows (and columns, since it's a square) as input from the user and store it in a variable ('n').
2. Run a loop 'n' times to iterate through the rows (i=1 to i<=n).
3. For each row, run another loop 'n' times to print the current row number ('i').
4. Print the current row number ('i') for each column in that row.
5. Move to the next line after each row has been printed.

Code in Java:

import java.util.Scanner;

public class BasicSquareIncrementingPattern {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.println("Enter the number of rows for the square incrementing pattern:");
        int n = scanner.nextInt();  // Taking input for number of rows

        // Loop to print the square incrementing pattern
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.print(i);  // Printing the current row number
            }
            System.out.println();  // New line after each row
        }

        scanner.close();
    }
}
            

Output:

Enter the number of rows for the square incrementing pattern:
4
1111
2222
3333
4444