ad

Sunday, May 25, 2025

c program to display alphabet pattern N with asterisk

 C program to Print  Alphabet  N star pattern 

C program to Print  Alphabet  N star pattern 

Example 


#include <stdio.h>

int main()
{
    int a,b;
    int n = 7; // height of the N (can be adjusted)

    for (a = 0; a < n; a++)
{
        for (b = 0; b < n; b++)
{
            // Print * for the first column, last column, and diagonal
            if (b == 0 || b == n - 1 || b == a)
                printf("*");
            else
                printf(" ");
        }
        printf("\n");
    }

    return 0;
}


This C Program Print a pattern in the shape of the capital letter R using asterisks (*) 

Explain Program

Step :1

#include <stdio.h>

This includes the Standard Input Output library. It is necessary to use functions like printf() for output.

Step :2

int main()

This is the main function from where the execution of the program 

Step :3

int a, b;

These are loop variables:

  • a: for controlling rows
  • b: for controlling columns

Step :4

int n = 7;

 defines the height (and width) of the pattern. The 'N' pattern will be print in a 7x7 grid. You can change n to any positive integer to scale the size of the 'N'

Step :5

Outer Loop: 

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

This controls the rows of the pattern.

  • a = 0 to a = 6 ( n = 7)
  • For each value of a, one full line (row) of output is printed

Step :6

Inner Loop:

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

This controls the columns within each row.

  • b = 0 to b = 6 for each row

Step :7

Conditional:

 if (b == 0 || b == n - 1 || b == a)

This is the heart of the logic. It decides where to print an asterisk *.

  • b == 0: First column → left vertical line of the 'N'
  • b == n - 1: Last column → right vertical line of the 'N'
  • b == a: Diagonal  top-left to bottom-right (row index == column index)

If any of the three conditions is true, print a *. Otherwise, print a space ( ).

Step :8

printf("*"); 


 printf(" ");

  • Print a * when the condition is met (position of vertical or diagonal part of 'N')
  • Print a space otherwise

Step :9

printf("\n");

After each row is completed move courser next line to start the next

C program to Print  Alphabet  N star pattern 

Output

C program to Print  Alphabet  N star pattern

Related Post :-


C program to print alphabet k star Pattern

C program to prin alphabet j star Pattern


No comments:

Post a Comment