Thursday, May 22, 2025

c program to display alphabet pattern K with asterisk

                                                C program  pattern 

 

c program to display alphabet pattern K with asterisk


 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>
Step :2
 Main Function
int main()
{
  •  execution of the program 
Step :3
Variable Declarations
int 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 Loop
for (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 Loop
for (b = 0; b < height; b++)
  • Loop from b = 0 to b = 6 → Control  print after the first star on each row.
Step :7
Diagonal 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 :9
New Line After Each Row
printf("\n");
  • Moves to the next line 
Step :10
. Return Statement
return 0;

 

C program to Print  Alphabet  K star pattern 
Output





No comments:

Post a Comment