Python Program to Print 1 and 0 in Alternative Rows

Grab the opportunity to learn all effective java programming language concepts from basic to advance levels by practicing these Java Program Examples with Output

Given the number of rows and columns, the task is to print 1 and 0 in alternative rows in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows =4
given number of columns=3

Output:

1 1 1 
0 0 0 
1 1 1 
0 0 0

Example2:

Input:

given number of rows =7
given number of columns=15

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Program to Print 1 and 0 in Alternative Rows in C, C++, and Python

Below are the ways to print 1 and 0 in alternative rows in C, C++, and Python.

If you look closely at the pattern, you’ll see that for all odd rows, 1 is printed and for all even rows, 0 is printed. As a

result, before displaying integers inside the inner loop, you must check for even-odd conditions. If the current row is

an odd number, print 1 otherwise, print 0.

The following is a step-by-step description of the logic used to print a 1, 0 number pattern at alternate rows.

Method #1: Using For loop

Approach:

  • Give the number of rows and number of columns as static input.
  • Store them in two separate variables row numbers and column numbers.
  • Run an outer loop from 1 to rows to iterate through the rows using For loop.
  • Iterate through the columns from 1 to cols using another For inner loop.
  • Before printing any number, we must first check the condition inside the inner loop.
  • This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
  • We check whether the row is odd or not using the if statement.
  • If it is true then print 1 else print 0.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows and number of columns as static input.
# Store them in two separate variables row numbers and column numbers.
rownumbs = 15
colnumbs = 11
# Run an outer loop from 1 to rows to iterate through the rows using For loop.
for m in range(1, rownumbs+1):
  # Iterate through the columns from 1 to cols using another For inner loop.
    for n in range(1, colnumbs+1):
      # Before printing any number, we must first check the condition inside the inner loop.
      # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
          # We check whether the row is odd or not using the if statement.
      # If it is true then print 1 else print 0.
        if(m % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

2) C++ Implementation

It is the same as the python approach but just the change in syntax.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 15;
    int colnumbs = 11;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    for (int i = 1; i <= rownumbs; i++) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        for (int j = 1; j <= colnumbs; j++) {
            // Before printing any number, we must
            // first check the condition inside the
            // inner loop.
            // This means that for every odd row, 1 is
            // displayed, and for every even row,
            // 0 is displayed.
            // We check whether the row is odd or not using
            // the if statement.
            // If it is true then print 1 else print 0.

            if (i % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
        }
        cout << endl;
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

3) C Implementation

It is the same as the python approach but just the change in syntax.

Below is the implementation:

#include <stdio.h>

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 15;
    int colnumbs = 11;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    for (int i = 1; i <= rownumbs; i++) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        for (int j = 1; j <= colnumbs;
             j++) { // Before printing any number, we must
            // first check the condition inside the
            // inner loop.
            // This means that for every odd row, 1 is
            // displayed, and for every even row, 0 is
            // displayed.
            /*We check whether the row is odd or not using
              the if statement. If it is true then print 1
              else print 0.*/
            if (i % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1

Method #2: Using while loop

1) C Implementation

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

#include <stdio.h>

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 4;
    int colnumbs = 3;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    int temprow = 1;
    while (temprow <= rownumbs) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        int tempcol = 1;
        while (tempcol <= colnumbs) { // Before printing any
                                      // number, we must
            // first check the condition inside the
            // inner loop. This means that for every odd
            // row, 1 is displayed, and for every even row,
            // 0 is displayed. # We check whether the row is
            // odd or not using the if statement. # If it is
            // true then print 1 else print 0.
            if (temprow % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
            tempcol++;
        }
        temprow++;
        printf("\n");
    }
    return 0;
}

Output:

1 1 1 
0 0 0 
1 1 1 
0 0 0

2) C++ Implementation:

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows and number of columns as
    // static input.
    // Store them in two separate variables row numbers and
    // column numbers.
    int rownumbs = 2;
    int colnumbs = 19;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    int temprow = 1;
    while (temprow <= rownumbs) {
        // Iterate through the columns from 1 to cols using
        // another For inner loop.
        int tempcol = 1;
        while (tempcol <= colnumbs) { // Before printing any
                                      // number, we must
            // first check the condition inside the
            // inner loop. This means that for every odd
            // row, 1 is displayed, and for every even row,
            // 0 is displayed. # We check whether the row is
            // odd or not using the if statement. # If it is
            // true then print 1 else print 0.
            if (temprow % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
            tempcol++;
        }
        temprow++;
        cout << endl;
    }
    return 0;
}

Output:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

3) Python Implementation

We just iterate till rownumbs and columumbs using a while loop.

Below is the implementation:

# Give the number of rows and number of columns as static input.
# Store them in two separate variables row numbers and column numbers.
rownumbs = 6
colnumbs = 11
# Run an outer loop from 1 to rows to iterate through the rows using For loop.
temprow = 1
while(temprow <= rownumbs):
  # Iterate through the columns from 1 to cols using another For inner loop.
    tempcol = 1
    while(tempcol <= colnumbs):
      # Before printing any number, we must first check the condition inside the inner loop.
      # This means that for every odd row, 1 is displayed, and for every even row, 0 is displayed.
      # We check whether the row is odd or not using the if statement.
        if(temprow % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
        tempcol += 1
    temprow += 1
    print()

Output:

1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0 
1 1 1 1 1 1 1 1 1 1 1 
0 0 0 0 0 0 0 0 0 0 0

Related Programs: