Internal Varsity Square Pattern
Program to Print Internal Varsity Square Pattern
PRINTING PATTERN:
333
313
323
333
PREREQUISITE:
Basic knowledge of Java language and loops.
ALGORITHM:
1. Take the size of the square (n) as input from the user. This will determine the number of rows and columns.
2. Initialize a loop that runs from 0 to n-1 (for rows).
3. For each row (let's call the current row index 'i'), initialize another loop that runs from 0 to n-1 (for columns).
4. Inside the inner loop, determine the value to print based on the position in the square:
- If the current position is on the border (i.e., if it's the first or last row or column), print '3'.
- If the current position is not on the border, print '1' if it's in the middle of the square (i.e., not on the border).
5. After finishing the inner loop for a row, print a newline character to move to the next row.
6. Repeat steps 2 to 5 until all rows are printed.
Code in Java:
import java.util.Scanner;
public class InternalVarsitySquarePattern {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter the size of the square (n):");
int n = scanner.nextInt(); // Taking input for the size of the square
// Loop to print the internal varsity square pattern
for (int i = 0; i < n; i++) { // Loop for each row
for (int j = 0; j < n; j++) { // Loop for each column in the current row
// Check if we are on the border
if (i == 0 || i == n - 1 || j == 0 || j == n - 1) {
System.out.print("3"); // Print '3' for border positions
} else {
System.out.print("1"); // Print '1' for internal positions
}
}
System.out.println(); // New line after each row
}
scanner.close();
}
}
Output:
Enter the size of the square (n):
4
333
313
323
333