ad

Friday, April 25, 2025

c program to display alphabet pattern G with asterisk

C Program

C program to Print  Alphabet  G star pattern 

Example :-


#include <stdio.h>

int main() {
    int a, b;
    int n = 7;

    for (a = 0; a < n; a++) {
        for (b = 0; b < n; b++) {
           
            if ((a == 0 ||a == n - 1) && b > 0 && b < n - 1)
                printf("*");
           
            else if ((b == 0 && a > 0 && a < n - 1))
                printf("*");
           
            else if ((a == n / 2 && b >= n / 2) || (a >= n / 2 && b == n - 1))
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}

This C Program Print a pattern in the shape of the capital letter G using asterisks "*"

Explain Program

Step :1

#include <stdio.h>

Includes the standard input-output library to use functions like printf().

Step :2

int main() { ... }

The entry point of the program.

Step :3

int a, b;

Variables a and b are loop counters:

  • a controls rows
  • b controls columns

Step :4

int n = 7;

 dimension of the pattern is 7 × 7.

Step :5

Outer Loops

 for (a = 0; a < n; a++)

loop goes from row 0 to 6 (7 rows in total).

Step :6

Inner Loop: 

for (b = 0; b < n; b++)

inner loop  loop goes from column 0 to 6 

Step :7

First Condition

if ((a == 0 || a == n - 1) && b > 0 && b < n - 1)


  • When the current row is the first (0) or last (6) AND
  • column is not the first or last 

Step :8

Second Condition

else if ((b == 0 && a > 0 && a < n - 1))

  • Column is the first (0) AND
  • Row is between top and bottom (i.e., not row 0 or 6)

Step :9

🔹 Third Condition

else if ((a == n / 2 && b >= n / 2) || (a >= n / 2 && b == n - 1))

(a == n / 2 && b >= n / 2)

  • Row is the middle row 
  • Column is middle or to the right 

Step :10

(a >= n / 2 && b == n - 1)

  • From the middle row down 
  • At the last column

Step :11

 Else Condition

else

    printf(" ");

  • above conditions match, just print a space.

Final Line

printf("\n");

returen 0;

C program to Print  Alphabet  G star pattern 

Output :-


C program to Print  Alphabet  G star pattern

Related Post :-

C program to print alphabet e star

No comments:

Post a Comment