C program to Print Alphabet O star pattern
C program to Print Alphabet O star pattern
Example
#include <stdio.h>
int main()
{
int a, b;
int n = 7; // height and width of the O
for (a = 0; a < n; a++)
{
for (b = 0; b < n; b++)
{
// Print * for the boundary of O
if ((a == 0 || a == n-1) && (b > 0 && b < n-1)
|| (b == 0 || b == n-1) && (a > 0 && a < n-1))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This C Program Print pattern in the shape of the capital letter O using asterisks "*"
Explain Program
Step :1
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.
Step :2
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.
Step :3
Inner Loop – Controls Columns
for (b = 0; b < n; b++)
for each row, it prints 7 characters (b = 0 to b = 6).
Step :4
Conditional Statement use
if ((a == 0 || a == n-1) && (b > 0 && b < n-1)
|| (b == 0 || b == n-1) && (a > 0 && a < n-1))
printf("*");
else
printf(" ");
Let’s break this condition down:Step :5
First Part:
(a== 0 || a == n-1) && (b > 0 && b < n-1)
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
Step :6
This draws the top and bottom horizontal lines of the 'O' (excluding corners).
Second Part:
(b == 0 || b == n-1) && (a > 0 && a < n-1)
b == 0 → first column (left edge)
b == n-1 → last column (right edge)
a > 0 && a < n-1 → avoid the top and bottom rows
Step :7
This draws the left and right vertical lines of the 'O' (excluding corners).
Else Case
printf(" ");
Step :8
printf("\n");
No comments:
Post a Comment