Example:
Input rows: 5
Input columns: 5
Output:
11111 11111 11111 11111 11111
Required knowledge
Basic C programming, LoopLogic 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.
- Run an outer loop from 1 to number of rows to be printed.
- Inside the outer loop run an inner loop from 1 to number of columns to be printed.
- Print whatever you want to get printed as output, in our case print 1.
- 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
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
Happy coding ;)