C program
C program to Print Alphabet J star pattern
Example
#include <stdio.h>
int main() { int a, b; int n = 7; // Height of the J pattern
for (a = 0; a < n; a++) { for (b = 0; b < n; b++) { // Print the vertical part of J if (b == n / 2 && a != 0 || (a == n - 1 && b <= n / 2) || (a == n - 2 && b == 0)) printf("*"); else printf(" "); } printf("\n"); }
return 0;}
#include <stdio.h>
int main()
{
int a, b;
int n = 7;
// Height of the J pattern
for (a = 0; a < n; a++)
{
for (b = 0; b < n; b++)
{
// Print the vertical part of J
if (b == n / 2 && a != 0 ||
(a == n - 1 && b <= n / 2) ||
(a == n - 2 && b == 0))
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
This C Program Print a pattern in the shape of the capital letter J using asterisks "*"
Explain Program
Step :1
Header File
#include <stdio.h>
- Standard I/O library to use the printf function for output.
Step :2
.Main Function
int main()
entry point of any C program. Execution starts
Step :3
Variable Declarations
int n = 7; // Height of the J pattern
- a and b: Loop counters.
- n = 7: The size of the square grid (7 rows × 7 columns).
Step :4
Outer Loop – Row
for (a = 0; a < n; a++)
- Control the rows from a = 0 to a = 6 (7 rows in total).
Step :5
Inner Loop –
Columns
for (b = 0; b < n; b++)
- Control the columns from b = 0 to b = 6
- Prints either a * or a space " " for each cell.
Step :6
Main Logic – if Condition
if (b == n / 2 && a != 0 ||
(a == n - 1
&& b <= n / 2) ||
(a == n - 2
&& b == 0))
printf("*");
else
printf("
");
Step :7
Condition 1:
b == n / 2 && a != 0- n
/ 2 = 7 / 2 = 3 (integer division).
- This
checks:
- Column
3 (b == 3)
- For
all rows except the first (a != 0)
Draws the vertical bar of
‘J’, starting from row 1 down to 6 at the center.
Step :8
Condition 2:
a == n - 1 && b <= n / 2
- a
== 6 (bottom row)
- b
<= 3 → columns 0, 1, 2, 3
bottom
horizontal line of 'J'.
Step :9
Condition 3:
a == n - 2 && b == 0
- a
== 5 (second-last row)
- b
== 0 → first column
Add a small hook at
the bottom left (curve of ‘J’)
Step :10
7. Line Break
printf("\n");
- After finishing each row (b loop ends), moves to a new line for the next row
. Return Statement
return 0;
No comments:
Post a Comment