C program to print square or rectangle star pattern

Previous Pattern Next Pattern
Write a C program to print square star(*) pattern series of N rows. Write a C program to print rectangle star(*) pattern program in C of n rows and m columns. How to print square, rectangle star pattern series using for loop in C programming. Logic to print square or rectangle star pattern of N rows using loops in C programming.

Example:
Input N: 5


Required knowledge

Basic C programming, For loop

Logic to print square star pattern

Before I get into logic to print this pattern, I recommend you to have a close look to the pattern for a minute so that you can think a little basic things about the pattern.

Now consider the pattern, if you have looked to the pattern carefully you have noticed that that pattern is a matrix of N rows and columns. What we need to do is, we need to iterate through N rows, and for each row we need to iterate N times to print each column per row. For that we would use the below logic to print the given pattern:
  1. To iterate through rows, run an outer loop from 1 to N (where N is the total number of rows to be printed).
  2. To print the columns, run an inner loop from 1 to N. Inside this loop print single * or any other character which you want to get printed on console.
And we are done, lets now implement this on code.

Program to print square star pattern

/**
 * C program to print square star pattern
 */

#include <stdio.h>

int main()
{
    int i, j, N;

    printf("Enter number of rows: ");
    scanf("%d", &N);

    //Used to iterate over each row
    for(i=1; i<=N; i++)
    {
        //Used to print columns of each row
        for(j=1; j<=N; j++)
        {
            printf("*");
        }
        
        //Moves to the next line/row
        printf("\n");
    }

    return 0;
} 


Output
Enter number of rows: 5
*****
*****
*****
*****
*****


Square star pattern screenshot

Square star pattern in C


Logic to print rectangle star pattern

Logic printing rectangle star pattern isn't much different with the square star pattern which we did just now. Below is the step-by-step descriptive logic to print rectangle star pattern:
  1. To iterate through rows, run an outer loop from 1 to M (where M is the total number of rows).
  2. To print columns, run an inner loop from 1 to N (where N is the total number of columns per row).
    Inside this loop print star(*) or any other character which you want to get printed as output on console.


Program to print rectangle star pattern

/**
 * C program to print rectangle star pattern
 */

#include <stdio.h>

int main()
{
    int i, j, M, N;

    printf("Enter number of rows: ");
    scanf("%d", &M);
    printf("Enter number of columns: ");
    scanf("%d", &N);

    //Used to iterate over each row
    for(i=1; i<=M; i++)
    {
        //Used to iterate over columns of each rows
        for(j=1; j<=N; j++)
        {
            printf("*");
        }
        
        //Move to the next line/row
        printf("\n");
    }

    return 0;
}


Rectangle star pattern screenshot

Rectangle star pattern in C


Happy coding ;)


Previous Pattern Next Pattern
Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment