C program to print square or rectangle star pattern

Write a C program to print square star(*) pattern series of N rows. C program to print rectangle star(*) pattern in C of N rows and M columns. Logic to print square or rectangle star pattern of N rows in C programming.

Example

Input

Input number of rows: 5

Output

Required knowledge

Basic C programming, For loop

Logic to print square star pattern

Have a close look to the pattern for a minute so that you can think a little basic things about the pattern.

You might have noted the below observations. The pattern is a matrix of N rows and columns containing stars(asterisks). Here, you need to iterate through N rows, and for each row iterate for N columns.

Below is the step by step descriptive logic to print the square number pattern.

  1. Read number of rows from user. Store it in some variable say N.
  2. To iterate through rows, run an outer loop from 1 to N. For that define a loop structure similar to for(i=1; i<=N; i++).
  3. To iterate through columns, run an inner loop from 1 to N. Define a loop inside above loop with structure for(j=1; j<=N; j++).
  4. Inside inner loop print * or any other character which you want to get printed on console.

Let us now implement the pattern using C program.

Program to print square star pattern

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

#include <stdio.h>

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

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

    // Iterate over rows
    for(i=1; i<=N; i++)
    {
        // Iterate over columns
        for(j=1; j<=N; j++)
        {
            // Print star for each column
            printf("*");
        }
        
        // Move to the next line/row
        printf("\n");
    }

    return 0;
}

Output

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

Hurray! done with my first star pattern. Let us now modify the pattern for rectangle.

Logic to print rectangle star pattern

Step-by-step descriptive logic to print rectangle star pattern.

  1. Read number of rows and columns from user. Store it in some variable say rows and columns.
  2. To iterate through rows, run an outer loop from 1 to rows. Define a loop with structure for(i=1; i<=rows; i++).
  3. To iterate through columns, run an inner loop from 1 to columns. Define a loop with structure for(j=1; j<=columns; j++).
  4. Inside inner 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, rows, columns;

    /* Input rows and columns from user */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &columns);

    // Iterate through each row
    for(i=1; i<=rows; i++)
    {
        // Iterate through each column
        for(j=1; j<=columns; j++)
        {
            // For each column print star
            printf("*");
        }
        
        // Move to the next line/row
        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 10
**********
**********
**********
**********
**********

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>