In C# loop i and j from 1 to size. Print "1 " when the cell is on the border; otherwise print " ".
Approach
How to Solve It
One nested loop over every cell, plus a four-way border check.
Method
Idea
Best for
Border condition
If i/j on edge → 1, else spaces
Learning, interviews, exams
Size input
Same logic with a user-chosen size
Practice / demos
Pseudocode
Pseudocode
for i from 1 to size:
for j from 1 to size:
if i is 1 or size, or j is 1 or size:
print "1 "
else:
print " "
print newline
Cheat sheet
Goal
Pattern
Walk rows
for (i = 1; i <= size; i++)
Walk columns
for (j = 1; j <= size; j++)
Border test
if (i == 1 || i == size || j == 1 || j == size)
Print border / interior
Console.Write("1 "); / Console.Write(" ");
End the row
Console.WriteLine();
Write vs WriteLine
API
Effect
Use for
Console.Write
Stays on the same line
Each cell ("1 " or " ")
Console.WriteLine
Ends the current line
After the inner loop finishes a row
Try it
Live Preview
Change the square size and the hollow border updates instantly.
Whole numbers from 2 to 9. Tap a chip or type a value — the preview redraws as you go.
Live result5 × 5 · 16 border cells
1 1 1 1 1
1 1
1 1
1 1
1 1 1 1 1
Trace
Worked Walkthrough — size = 4
Trace each row: top and bottom are solid; middle rows keep only the ends.
i
Border rule
Printed row
1
Top row — all 1
1 1 1 1
2
j == 1 or j == 4
1 1
3
j == 1 or j == 4
1 1
4
Bottom row — all 1
1 1 1 1
Border cells for size n = 4(n - 1) when n >= 2 (corners counted once).
Code
C# Programs
Three complete programs: fixed 5×5, user-input size, and an asterisk-border variant. Use View Output for sample results.
Example 1 — Fixed size = 5
Nested loops with a border condition — ideal for first demos.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
int size = 5;
for (i = 1; i <= size; i++)
{
for (j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
Console.Write("1 ");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
Output
1 1 1 1 1
1 1
1 1
1 1
1 1 1 1 1
How It Works
1. Grid walk. Outer i picks the row; inner j picks the column — every cell is visited once.
2. Border test. If i or j is on the edge, print "1 "; otherwise print " ".
3. New line.WriteLine after the inner loop starts the next row.
Example 2 — User Input (size)
Read the square size and build an n × n hollow frame.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int size, i, j;
Console.Write("Enter square size: ");
if (!int.TryParse(Console.ReadLine(), out size) || size < 2)
{
Console.WriteLine("Please enter an integer greater than 1.");
return;
}
for (i = 1; i <= size; i++)
{
for (j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
Console.Write("1 ");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
Output (when user enters 4)
Enter square size: 4
1 1 1 1
1 1
1 1
1 1 1 1
How It Works
1. Validate size.TryParse rejects non-numeric input; require size >= 2 for a hollow frame.
2. Same border logic. Only the bound changes from the literal 5 to the user value.
Example 3 — Asterisk Border (size = 5)
Same hollow frame — swap 1 for * on the border.
C#
using System;
namespace MyApp
{
class Program
{
static void Main(string[] args)
{
int i, j;
int size = 5;
for (i = 1; i <= size; i++)
{
for (j = 1; j <= size; j++)
{
if (i == 1 || i == size || j == 1 || j == size)
Console.Write("* ");
else
Console.Write(" ");
}
Console.WriteLine();
}
}
}
}
Output
* * * * *
* *
* *
* *
* * * * *
How It Works
1. Same structure. Only the border string changes from "1 " to "* ".
2. Keep spacing. Interior cells still use two spaces so columns stay aligned.
Edge Cases & Pitfalls
Check these before calling the solution done.
AND vs OR
Use && instead of ||
Corners would still print, but side edges would be empty. Border needs || so any edge matches.
one space
Interior prints a single space
Columns drift because border cells use "1 " (two characters). Match width with " ".
size = 1
Single cell
Output is just 1 — no hollow interior. Require size >= 2 for a frame.
0-based
Loops from 0 but check size
If indices are 0..size-1, test i == 0 || i == size - 1 (and the same for j).
WriteLine
WriteLine inside the inner loop
That puts every cell on its own line. Call WriteLine only after the column loop.
Bad input
Convert.ToInt32 throws
Prefer int.TryParse so non-numeric input does not crash the program.
Analysis
Time and Space Complexity
Program
Time
Extra space
Fixed / asterisk (Examples 1, 3)
O(n²)
O(1)
User input (Example 2)
O(n²)
O(1)
Every cell of the n × n grid is visited once, so work is quadratic in the side length.
Remember
Key Takeaways
Rule: print 1 when i or j is on the edge; otherwise print spaces.
Align: use "1 " and " " so every cell is two characters wide.
Write vs WriteLine: cells stay on the line; WriteLine advances after each row.
Next step: Program 43 prints a right-aligned ascending number triangle.
One line: visit every cell; print 1 on the border and spaces inside.
Frequently Asked Questions
It prints characters only on the border (first/last row and first/last column) and leaves the inside blank with spaces.
It checks if i is 1 or size, or if j is 1 or size. If any is true, print 1; otherwise print spaces.
Use a size variable and loop from 1 to size for both i and j. Update the border check to use size — see Example 2.
Each border cell uses "1 " (digit plus space). Interior cells use " " so columns stay aligned.
Size 2 gives a thin frame. Size 1 prints a single 1 with no hollow interior.
Program 41 prints a centered pyramid of perfect squares. Program 42 prints a hollow square grid using border conditions.
Replace "1 " with "* " or "# " in the if branch — see Example 3.
O(n²) for an n×n grid because each cell is visited once.
Prefer int.TryParse(Console.ReadLine(), out size) and validate size >= 2 for a meaningful hollow frame.
🤔
Did you know?
Print 1 when i == 1, i == size, j == 1, or j == size; otherwise print spaces. An n × n grid visits n² cells.