C program pattern
C program to Print Alphabet L star pattern
Example
#include <stdio.h>
int main() {
int a,b;
int n = 7; // height of the L
for (a = 0; a < n; a++)
{
for (b = 0; b < n; b++)
{
// Print * for the left vertical line and the bottom horizontal line
if (b == 0 || (a == n - 1 && b < n))
{
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
c program to display alphabet pattern L with asterisk
Explain Program
Step :1
Header
#include <stdio.h>
- standard input-output library use functions like printf().
Main Function Declaration
int main() {
- The
entry point of the program. Execution starts
Variable Declarations
int a, b;
int n = 7; // height of the L
- a
and b are loop counters.
- n is
the height of the letter L and also the number of rows in the
output
Step :4
Outer Loop
for (a = 0; a < n; a++)
iteration of this loop correspond to one
row of the output
Inner Loop (Controls Columns)
for (b = 0; b < n; b++)
- Run b = 0 to b = 6 (7 iteration).
- Each
iteration of this loop correspond to one column in the current
row
.Logic to Print
if (b == 0 || (a == n - 1 && b < n))
- This
is true for the first column in every row, Explanation we print a * in
the first column of every row — forming the vertical line
of the "L".
- This
is true only we are on the last row (a == 6) and b < 7 for all columns of the last row.
- So
on the last row, it prints * across the entire row — forming the bottom
horizontal line of the "L".
printf("*");
Prints
an asterisk the condition is true.
Step :8
printf(" ");
Prints a space
printf("\n");
- cursor to the next line after printing each row.
C program to Print Alphabet L star pattern
Output
Summary
-
Outer loop: controls rows.
-
Inner loop: controls what to print in each column of the row.
-
Two conditions decide where to print *:
-
First column (b == 0)
-
Entire last row (a == n - 1)
Related Post :-
Comments
Post a Comment