C program pattern
C program to Print Alphabet K star pattern
Example
#include <stdio.h>
int main()
{
int a, b;
int height = 7;
// Height of the K
for(a = 0; a < height;a++)
{
printf("*");
// First vertical line
for(b = 0; b < height;b++) {
if (a + b == height / 2 || a - b == height / 2)
{
printf("*");
} else {
printf(" ");
}
}
printf("\n");
}
return 0;
}
c program to display alphabet pattern K with asterisk
Explain Program
Step :1
Header File#include <stdio.h>
- This includes the Standard Input Output library.
- Needed for printf() function to work.
Main Functionint main(){
- execution of the program
Step :3Variable Declarationsint a, b;int height = 7;
- a: loop counter for rows.
- b: loop counter for columns.
- height = 7: defines the number of rows in the output
Step :4
Outer Loopfor (a = 0; a < height; a++)
- Loop from a = 0 to a = 6 → 7 rows in total.
Step :5
First Vertical Line
printf("*");
- Prints a * at the start of every line →
- This forms the vertical left leg of the letter 'K'.
Step :6. Inner Loopfor (b = 0; b < height; b++)
- Loop from b = 0 to b = 6 → Control print after the first star on each row.
Step :7Diagonal Logic of 'K'if (a + b == height / 2 || a - b == height / 2)break this condition:
- height / 2 = 7 / 2 = 3
- a + b == 3 → Draws the upper diagonal arm of the 'K'.
- a - b == 3 → Draws the lower diagonal arm of the 'K'.
Step :8
Printing * or Space
printf("*");
- If the condition is true → prints a *.
printf(" ");
- Else → prints a space to maintain alignment.
Step :9New Line After Each Rowprintf("\n");
- Moves to the next line
Step :10
. Return Statementreturn 0;
C program to Print Alphabet K star pattern
Output
Related Post :-

No comments:
Post a Comment