Internal Varsity Square Pattern
Program to Print Internal Varsity Square Pattern
PRINTING PATTERN:
333 313 323 333
PREREQUISITE:
Basic knowledge of C 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 C:
#include <stdio.h> int main() { int n, i, j; // Declaring integers for size and loop counters printf("Enter the size of the square (n):\n"); scanf("%d", &n); // Taking input for the size of the square // Loop to print the internal varsity square pattern for (i = 0; i < n; i++) { // Loop for each row for (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) { printf("3"); // Print '3' for border positions } else { printf("1"); // Print '1' for internal positions } } printf("\n"); // New line after each row } return 0; }
Output:
Enter the size of the square (n): 4 333 313 323 333