C program to print number pattern with 1, 0 at alternate rows

Write a C program to print the given number pattern of 1’s and 0’s at alternate rows using loop. How to print square number pattern of one and zero with one at odd row and zero at even row using loop in C programming. Logic to print number pattern of 1 and 0 with alternate rows.

Example

Input

Input rows: 5
Input columns: 5

Output

11111
00000
11111
00000
11111

Required knowledge

Basic C programming, Loop

Must know – Program to print square rectangle number pattern

Logic to print 1, 0 number pattern at alternate rows

If you look at the pattern carefully you will notice that for all odd rows 1 is printed and for even rows 0 is printed. Hence, before printing numbers inside inner loop, you need to check even odd condition. If the current row is odd then print 1 otherwise 0.
Below is the step by step descriptive logic to print 1, 0 number pattern at alternate rows.

  1. Input number of rows and columns to print from user. Store it in some variable say rows and cols.
  2. To iterate through rows run an outer loop from 1 to rows. The loop structure should look like for(i=1; i<=rows; i++).
  3. Run another inner loop to iterate through columns from 1 to cols. The loop structure should look like for(i=1; i<=cols; i++).
  4. Inside the inner loop before printing any number, we need to check condition. Which is for every odd rows 1 is printed and for every even rows 0 is printed. Hence, check if(i%2 == 1) then print 1 otherwise 0.
  5. Finally, after the inner loop when all columns are printed move to next line. Which is print a dummy new line.

Program to print number pattern of 1’s and 0’s at alternate rows

/**
 * C program to print number pattern of 1, 0 at even/odd rows
 */

#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);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            // Print 1 if current row is odd
            if(i%2 == 1)
            {
                printf("1");
            }
            else
            {
                printf("0");
            }
        }

        printf("\n");
    }

    return 0;
}

The above method if fine and easy to understand. However, you can further optimize the above program. i%2 returns 1 in case of odd and 0 in case of even. Hence, you can remove the if else checking condition. The whole if else condition can be transformed to single printf(“%d”, (i%2));

Program to print number pattern with 1, 0

/**
 * C program to print number pattern of 1, 0 at even/odd rows
 */

#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);

    for(i=1; i<=rows; i++)
    {
        for(j=1; j<=cols; j++)
        {
            printf("%d", (i%2));
        }

        printf("\n");
    }

    return 0;
}

Output

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

Happy coding 😉