C program to Print Alphabet O star pattern
Example
Explain Program
Variable Declaration
int a, b;
int n = 7; // height and width of the O
a and b are loop variables:a is used to track rows
b is used to track columns
n = 7 sets both the height and width of the "O". So the output will be a 7x7 grid.
Outer Loop – Controls Rows
for (a = 0; a < n; a++)
loop Run from a = 0 to a = 6 (7 row total).Each iteration represents new row.
Inner Loop – Controls Columns
for (b = 0; b < n; b++)
for each row, it prints 7 characters (b = 0 to b = 6).
Conditional Statement use
First Part:
Handles top and bottom row, but only middle part
a == 0 → first row (top of the O)
a == n-1 → last row (bottom of the O)
b > 0 && b < n-1 → avoid the corners
This draws the top and bottom horizontal lines of the 'O' (excluding corners).
Second Part:
b == 0 → first column (left edge)
b == n-1 → last column (right edge)
a > 0 && a < n-1 → avoid the top and bottom rows
This draws the left and right vertical lines of the 'O' (excluding corners).
Else Case


No comments:
Post a Comment