on
CSS
- Get link
- X
- Other Apps
#include <stdio.h>
int main() {
int a, b;
int n = 7;
height of the T
n is set to 7, which means the height of the "T" will be 7 rows variable a and b are use as loop
This loop iterates from 0 to 6 (total 7 times),
each iteration representing one row of output.
Inner Loop – Columns
For each row, this loop iterates 7 times (columns from 0 to 6).
Step 4]
if (a == 0 || b == n / 2)
printf("*");
else
printf(" ");
a == 0: For the first row, print a * in every column — this forms the top horizontal bar of the "T".
b == n / 2: For all rows,
print a * in the middle column (
since n = 7, n / 2 = 3) — this forms the vertical bar of the "T".
Else, print a space.
printf("\n");
Moves the cursor to the next line after printing each row.
This program uses nested loops and conditionals to:
Print a full row of asterisks on the first row (a == 0)
Print a vertical column of asterisks in the middle (b == n/2) for all rows
Together, this forms a "T" shape.
Comments
Post a Comment