C program to Print Square Star Pattern

square pattern print in C

In this article, I will show you, How to write a C program to print square star pattern.  Here, one thing is important to know that all sides of the square must be the same.

Logic to write C program to print square star pattern:

It is very easy to print a square star pattern in C, below I have mentioned a few steps to print a square pattern in C:

  • You must know the side of the square.
  • There should be two-loop, inner and outer.
  • Inner loop creates the column of the square. The inner loop runs 1 to N, where N is the side of the square.
  • You need to print * inside the inner loop.
  • Outer loop creates the rows of the square. The outer loop runs 1 to N, where N is the side of the square.

C program to Print Square Star Pattern:

 

#include<stdio.h>

int main()
{
    int x = 0,y = 0;
    unsigned int squareSide = 0;

    printf("Enter Side of a Square = ");
    scanf("%u",&squareSide);

    //outer loop
    for(x = 0; x < squareSide; ++x)
    {
        //inner loop
        for(y = 0; y < squareSide; ++y)
        {
            printf("*");
        }
        printf("\n");
    }
    return 0;
}

Output:

star pattern in C

 

Code Analysis:

It asks the user to enter the side of the square.

printf("Enter Side of a Square = ");
scanf("%u",&squareSide);

 

Code contains the two for loop, inner loop, and outer loop.  Inner loop prints the * using the printf function.  The outer loop print newline after each iteration of the inner loop.

 

//outer loop
for(x = 0; x < squareSide; ++x)
{
    //inner loop
    for(y = 0; y < squareSide; ++y)
    {
        printf("*");
    }
    printf("\n");
}

 

Using the above logic you can also write a C program to print any character in a square pattern. You just need to ask character from the user and print it on the console.

#include<stdio.h>

int main()
{
    int x = 0,y = 0;
    int ch = 0;
    unsigned int squareSide = 0;

    printf("Enter Side of a Square = ");
    scanf("%u",&squareSide);

    printf("\nEnter Character want to print in square pattern = ");
    fflush(stdin);
    ch = fgetc(stdin);

    //outer loop
    for(x = 0; x < squareSide; ++x)
    {
        //inner loop
        for(y = 0; y < squareSide; ++y)
        {
            fputc(ch,stdout);
        }
        printf("\n");
    }
    return 0;
}

Output:

square pattern any character in C

 

Code Analysis:

It asks the user to enter the side of the square.

printf("Enter Side of a Square = ");
scanf("%u",&squareSide);

It asks the user to enter the character to print pattern. I am using fgetc function to read the user input.

printf("\nEnter Character want to print in square pattern = ");
fflush(stdin);
ch = fgetc(stdin);

 

Code contains the two for loop, inner loop, and outer loop.  Inner loop prints the character using the fputc function.  The outer loop print newline after each iteration of the inner loop.

//outer loop
for(x = 0; x < squareSide; ++x)
{
    //inner loop
    for(y = 0; y < squareSide; ++y)
    {
        fputc(ch,stdout);
    }
    printf("\n");
}

 

Recommended Post:



Leave a Reply

Your email address will not be published. Required fields are marked *