C program
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;
}
This C Program Print a pattern in the shape of the capital letter L using asterisks "*"
Explain Program
Step :1
Header
#include <stdio.h>
- standard input-output library use functions like printf().
Step :2
Main Function Declaration
int main() {
- The entry point of the program. Execution starts
Step :3
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++)
- Runs from a = 0 to a = 6
Step :5
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
Step :6
.Logic to Print
if (b == 0 || (a == n - 1 && b < n))
- b == 0
- 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".
- (a == n - 1 && b < n):
- 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".
c
Step :7
printf("*");
Prints an asterisk the condition is true.
Step :8
printf(" ");
Prints a space
Step :9
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 :-
No comments:
Post a Comment