C program to print 0 or 1 square number pattern

Write a C program to print the given 0, 1 square number pattern using loop. C program to print binary number pattern of n rows and m columns using loop. How to print the square number patterns using for loop in C programming. Logic to print the square filled with 1 using for loop in C program.

Example

Input

Input rows: 5
Input columns: 5

Output

11111
11111
11111
11111
11111

Required knowledge

Basic C programming, Loop

Logic to print square number pattern

Logic to print this square number pattern of 1 is simple and similar to square start pattern.

We only need to replace the stars(*) with 1 or 0 whatever you want to print. Basic logic to print square number pattern of n rows and m columns.
Below is the step by step descriptive logic to print square number pattern.

  1. Input number of rows and columns to print from user. Store it in some variable say rows and cols.
  2. To print square number pattern, we need two loops. An outer loop to iterate through rows and second an inner loop to iterate through columns.
  3. Run an outer loop from 1 to total rows. The loop structure should look like for(i=1; i<=rows; i++).
  4. Inside the outer loop run an inner loop from 1 to total columns. The loop structure should look like for(j=1; j<=cols; j++).
  5. Inside the inner loop, print whatever you want to get printed as output, in our case print 1.
  6. After inner loop, advance the cursor position to next line i.e. print a dummy blank line.

Program to print square number pattern

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

#include <stdio.h>

int main()
{
    int rows, cols, i, j;

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

    /* Iterate through rows */
    for(i=1; i<=rows; i++)
    {
        /* Iterate through columns */
        for(j=1; j<=cols; j++)
        {
            printf("1");
        }

        printf("\n");
    }

    return 0;
}

Output

Enter number of rows: 5
Enter number of columns: 5
11111
11111
11111
11111
11111

Note: To print rectangle number pattern, make the rows and columns different.

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>