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 rows: 5
Input columns: 5
Output:
11111
11111
11111
11111
11111

Required knowledge

Basic C programming, Loop

Logic to print square number pattern of 1

Logic to print this square number pattern of 1 is simple and similar to the square start pattern.
*****
*****
*****
*****
*****
We only need to replace the stars(*) with 1 or 0 whatever we want to print. Basic logic to print number pattern of 1 of n rows and m columns.
  1. Run an outer loop from 1 to number of rows to be printed.
  2. Inside the outer loop run an inner loop from 1 to number of columns to be printed.
  3. Print whatever you want to get printed as output, in our case print 1.
  4. After the termination of inner loop advance the cursor position to next line.


Program to print 1 square number pattern

/**
 * C program to print 1 and 0 square number pattern
 */

#include <stdio.h>

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

    /*
     * Reads number of rows, columns to be printed
     */
    printf("Enter number of rows: ");
    scanf("%d", &rows);
    printf("Enter number of columns: ");
    scanf("%d", &cols);

    for(i=1; i<=rows; i++)
    {
        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: You can also print rectangle number pattern just make the rows and columns different. Also you can print 0 if you wish, just change the inner printf("1"); statement to printf("0");.


Screenshot

Square number pattern of 1


Happy coding ;)


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