Python Program to Print 1 and 0 in Alternative Columns

Python Program to Print 1 and 0 in Alternative Columns

Want to excel in java coding? Practice with these Java Programs examples with output and write any kind of easy or difficult programs in the java language

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

Program to Print 1 and 0 in alternative Columns in C, C++, and Python

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

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

As a result, before displaying integers inside the inner loop, you must check for even-odd conditions. If the current column 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 columns.

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 column, 1 is displayed, and for every even column, 0 is displayed.
  • We check whether the column 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:

Python Program to Print 1 and 0 in Alternative Columns

# 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 column, 1 is displayed, and for every even column, 0 is displayed.
      # We check whether the column is odd or not using the if statement.
        if(n % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
    print()

Output:

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

2) C++ Implementation

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

Below is the implementation:

C++ Program to Print 1 and 0 in Alternative Columns

#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 column, 1 is displayed, and for
                    // every even column, 0 is displayed. We
                    // check whether the column is odd or
                    // not using the if statement.
            if (j % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
        }
        cout << endl;
    }
    return 0;
}

Output:

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

3) C implementation

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

Below is the implementation:

C Program to Print 1 and 0 in Alternative Columns

#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 column, 1 is displayed, and for
            // every even column, 0 is displayed. We
            // check whether the column is odd or
            // not using the if statement.
            if (j % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
        }
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2:Using while loop

1) C Implementation

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

Below is the implementation:

Python Program to Print 1 and 0 in Alternative Columns Using While Loop

#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.
    while (rownumbs > 0) {
        // 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 column, 1 is displayed, and for
            // every even column, 0 is displayed. We
            // check whether the column is odd or
            // not using the if statement.
            if (tempcol % 2 == 1)
                printf("1 ");
            // If it is true then print 1 else print 0.
            else
                printf("0 ");
            tempcol++;
        }
        rownumbs--;
        printf("\n");
    }
    return 0;
}

2) C++ Implementation:

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

Below is the implementation:
CPP Program to Print 1 and 0 in Alternative Columns Using While Loop

#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 = 13;
    int colnumbs = 20;
    // Run an outer loop from 1 to rows to iterate through
    // the rows using For loop.
    while (rownumbs > 0) {
        // 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 column, 1 is displayed, and for
            // every even column, 0 is displayed. We
            // check whether the column is odd or
            // not using the if statement.
            if (tempcol % 2 == 1)
                cout << "1 ";
            // If it is true then print 1 else print 0.
            else
                cout << "0 ";
            tempcol++;
        }
        rownumbs--;
        cout << endl;
    }
    return 0;
}

Output:

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

3) Python Implementation

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

Below is the implementation:

Python Program to Print 1 and 0 in Alternative Columns Using While Loop

# 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.
while(rownumbs > 0):
  # 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 column, 1 is displayed, and for every even column, 0 is displayed.
      # We check whether the column is odd or not using the if statement.
        if(tempcol % 2 == 1):
            print('1', end=' ')
        # If it is true then print 1 else print 0.
        else:
            print('0', end=' ')
        tempcol += 1
    rownumbs -= 1
    print()

Output:

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

Related Programs:

Python Program to Print Hollow Rhombus Star Pattern

Python Program to Print Hollow Rhombus Using For Loop Star Pattern

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 of the rhombus, the task is to print the hollow rhombus Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of rhombus =11

Output:

          * * * * * * * * * * * 
         *                        * 
        *                        * 
       *                        * 
      *                        * 
     *                        * 
    *                        * 
   *                        * 
  *                        * 
 *                        * 
* * * * * * * * * * *

Example2:

Input:

given number of rows of rhombus =6
given character to print =<

Output:

          < < < < < < 
        <               < 
      <               < 
    <               < 
  <               < 
< < < < < <

Program to Print Hollow Rhombus Star Pattern in C, C++, and Python

Below are the ways to print Hollow Rhombus star patterns in C, C++, and Python.

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the rhombus as static input and store it in a variable.
  • Using Nested For loops print the rhombus star pattern.
  • We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

Python Program to Print Hollow Rhombus Star Pattern

# Give the number of rows of the rhombus as static input and store it in a variable.
rhombusrows = 11
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
      # We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
       # If it is true then print * else print space.
        if(m == 1 or m == rhombusrows or k == 0 or k == rhombusrows - 1):
            print('*', end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

          * * * * * * * * * * * 
         *                        * 
        *                        * 
       *                        * 
      *                        * 
     *                        * 
    *                        * 
   *                        * 
  *                        * 
 *                        * 
* * * * * * * * * * *

2) C++Implementation

Below is the implementation:

CPP Program to Print Hollow Rhombus Using For Loop Star Pattern

#include <iostream>
using namespace std;

int main(void)
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 6;
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
            cout << "* ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

          * * * * * * 
        *           * 
      *           * 
    *           * 
  *           * 
* * * * * *

3) C Implementation

Below is the implementation:

C Program to Print Hollow Rhombus Using For Loop Star Pattern

#include <stdio.h>

int main()
{
    // Give the number of rows of the rhombus as static
    // input and store it in a variable.
    int rhombusrows = 9;
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                printf("* ");
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

                * * * * * * * * * 
              *                   * 
            *                   * 
          *                   * 
        *                   * 
      *                   * 
    *                   * 
  *                   * 
* * * * * * * * *

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the rhombus as static input and store it in a variable.
  • Scan the character to print as user input and store it in a variable.
  • Using Nested For loops print the rhombus star pattern.
  • We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
  • If it is true then print * else print space.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of sides of the rhombus as user input using int(input()) and store it in a variable.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

D:\Content\Shivam\selenium\Py Images\Python Program to Print Hollow Rhombus Using For Loop User Character.png

# Give the number of sides of the rhombus as user input using int(input()) and store it in a variable.
# Give the Element as user input using int(input()) and store it in another variable.
rhombusrows = int(input('Enter some random number of rows of the rhombus= '))
characte = input('Enter some random character to print = ')
# Using Nested For loops print the rhombus star pattern.
for m in range(rhombusrows, 0, -1):
    for n in range(1, m):
        print(' ', end='')
    for k in range(0, rhombusrows):
      # We use the If Else statement to check If the side length of the rhombus(rows) is 0 or maximum – 1.
       # If it is true then print * else print space.
        if(m == 1 or m == rhombusrows or k == 0 or k == rhombusrows - 1):
            print(characte, end=' ')
        else:
            print(' ', end=' ')
    print()

Output:

Enter some random number of rows of the rhombus= 6
Enter some random character to print = <
          < < < < < < 
        <               < 
      <               < 
    <               < 
  <               < 
< < < < < <

2) C++Implementation

  • Give the number of sides of the rhombus as user input using cin and store it in a variable.
  • Create a character variable.
  • Give the character as user input using cin and store it in another variable.

Below is the implementation:

CPP Program to Print Hollow Rhombus Using For Loop User Character

#include <iostream>
using namespace std;

int main(void)
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using cin and
    // store it in a variable.
    cout << "Enter some random number of rows of the "
            "rhombus = "
         << endl;
    cin >> rhombusrows;
    // Create a character variable.
    // Give the character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character to print = "
         << endl;
    cin >> characte;
    cout << endl;
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            cout << "  ";
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                cout << characte << " ";
            else
                cout << "  ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the rhombus = 
5
Enter some random character to print = 
?
    ? ? ? ? ? 
   ?         ? 
  ?         ? 
 ?         ? 
? ? ? ? ?

3) C Implementation

  • Give the number of sides of the rhombus as user input using scanf and store it in a variable.
  • Create a character variable.
  • Give the character as user input using scanf and store it in another variable.

Below is the implementation:

D:\Content\Shivam\selenium\Py Images\Python Program to Print Hollow Rhombus Using For Loop User Character.png

#include <stdio.h>

int main()
{
    int rhombusrows;
    char characte;
    // Give the number of rows of the rhombus as user input
    // using scanf and
    // store it in a variable.
    // Create a character variable.
    // Give the character as user input using scanf and
    // store it in another variable.
    // Using Nested For loops print the rhombus pattern.
    scanf("%d%c", &rhombusrows, &characte);
    printf("\n");
    // Using Nested For loops print the rhombus star
    // pattern.
    for (int m = rhombusrows; m > 0; m--) {
        for (int n = 1; n < m; n++) {
            printf("  ");
        }
        for (int k = 0; k < rhombusrows; k++) {
            // We use the If Else statement to check If the
            // side length of the rhombus(rows) is 0 or
            // maximum – 1. If it is true then print * else
            // print space.
            if (m == 1 || m == rhombusrows || k == 0
                || k == rhombusrows - 1)
                printf("%c ", characte);
            else
                printf("  ");
        }
        printf("\n");
    }
}

Output:

5 :
    : : : : : 
   :       : 
  :       : 
 :       : 
: : : : :

Related Programs:

Python Program to Print Alternate Numbers Pattern using While Loop

Interested in programming and want to excel in it by choosing the short ways. Then, practicing with the available Java Program list is mandatory.

Given the number of rows, the task is to Print Alternate Numbers Pattern using While Loop in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows = 9

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17

Example2:

Input:

Given number of rows = 7

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13

Program to Print Alternate Numbers Pattern using While Loop in C, C++, and Python

Below are the ways to Print Alternate Numbers Pattern using While Loop in C, C++, and Python

Method #1: Using While Loop (Static Input)

Approach:

  • Give the number of rows as static input and store it in a variable.
  • Take a variable(say f ) and initialize it 1.
  • Loop till f is less than or equal to the number of rows using While Loop.
  • Take a variable(say g ) and initialize it 1.
  • Loop till g is is less than or equal to f i.e g<=f using Another While Loop(Nested While Loop).
  • Inside the inner While Loop print the value of 2*f-1 with space.
  • Increment the value of j by 1.
  • After the end of the inner while loop increment the value of variable f by 1.
  • Print the Newline character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows as static input and store it in a variable.
numberrows = 10
# Take a variable(say f ) and initialize it 1.
f = 1
# Loop till f is less than or equal to the number of rows using While Loop.
while(f <= numberrows):
    # Take a variable(say g ) and initialize it 1.
    g = 1
    # Loop till g is is less than or equal to f
    # i.e g<=f using Another While Loop(Nested While Loop).
    while g <= f:
        # Inside the inner While Loop print the value of 2*f-1 with space.
        print((f * 2 - 1), end=" ")
        # Increment the value of g by 1.
        g = g + 1
    # After the end of the inner while loop increment the value of variable f by 1.
    f = f + 1
    # Print the Newline character.
    print()

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17 
19 19 19 19 19 19 19 19 19 19

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows as static input and store it
    // in a variable.
    int numberrows = 7;

    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            cout << (f * 2 - 1) << " ";
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        cout << endl;
    }

    return 0;
}

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows as static input and store it
    // in a variable.
    int numberrows = 7;

    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            printf("%d ", (f * 2 - 1));
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        printf("\n");
    }
    return 0;
}

Output:

1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13

Method #2: Using While Loop (User Input)

Approach:

  • Give the number of rows as user input and store it in a variable.
  • Take a variable(say f ) and initialize it 1.
  • Loop till f is less than or equal to the number of rows using While Loop.
  • Take a variable(say g ) and initialize it 1.
  • Loop till g is is less than or equal to f i.e g<=f using Another While Loop(Nested While Loop).
  • Inside the inner While Loop print the value of 2*f-1 with space.
  • Increment the value of j by 1.
  • After the end of the inner while loop increment the value of variable f by 1.
  • Print the Newline character.
  • The Exit of the Program.

1) Python Implementation

Give the number of rows as user input using int(input()) and store it in a variable.

Below is the implementation:

# Give the number of rows as user input using int(input()) and store it in a variable.
numberrows = int(input('Enter some random number of rows = '))
# Take a variable(say f ) and initialize it 1.
f = 1
# Loop till f is less than or equal to the number of rows using While Loop.
while(f <= numberrows):
    # Take a variable(say g ) and initialize it 1.
    g = 1
    # Loop till g is is less than or equal to f
    # i.e g<=f using Another While Loop(Nested While Loop).
    while g <= f:
        # Inside the inner While Loop print the value of 2*f-1 with space.
        print((f * 2 - 1), end=" ")
        # Increment the value of g by 1.
        g = g + 1
    # After the end of the inner while loop increment the value of variable f by 1.
    f = f + 1
    # Print the Newline character.
    print()

Output:

Enter some random number of rows = 10
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17 
19 19 19 19 19 19 19 19 19 19

2) C++ Implementation

Give the number of rows as user input using cin and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows as user input using
    // int(input()) and store it in a variable.
    int numberrows;
    cin >> numberrows;
    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            cout << (f * 2 - 1) << " ";
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        cout << endl;
    }
    return 0;
}

Output:

8
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 

3) C Implementation

Give the number of rows as user input using scanf and store it in a variable.

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the number of rows as user input using scanf and
    // store it in a variable.
    int numberrows;
    scanf("%d", &numberrows);
    // Take a variable(say f ) and initialize it 1.
    int f = 1;
    // Loop till f is less than or equal to the number of
    // rows using While Loop.
    while (f <= numberrows) {
        // Take a variable(say g ) and initialize it 1.
        int g = 1;
        // Loop till g is is less than or equal to f
        // i.e g<=f using Another While Loop(Nested While
        // Loop).
        while (g <= f) {
            // Inside the inner While Loop print the value
            // of 2*f-1 with space.
            printf("%d ", (f * 2 - 1));
            // Increment the value of g by 1.
            g = g + 1;
        }
        // After the end of the inner while loop increment
        // the value of variable f by 1.
        f = f + 1;
        // Print the Newline character.
        printf("\n");
    }
    return 0;
}

Output:

9
1 
3 3 
5 5 5 
7 7 7 7 
9 9 9 9 9 
11 11 11 11 11 11 
13 13 13 13 13 13 13 
15 15 15 15 15 15 15 15 
17 17 17 17 17 17 17 17 17 

Related Programs:

Related Programs:

Python Program to Print Floyd’s Triangle

Beginners and experienced programmers can rely on these Best Java Programs Examples and code various basic and complex logics in the Java programming language with ease.

Given the number of rows of the triangle, the task is to print Floyd’s triangle in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the triangle = 10

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55

Example2:

Input:

Given number of rows of the triangle = 13

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 
67 68 69 70 71 72 73 74 75 76 77 78 
79 80 81 82 83 84 85 86 87 88 89 90 91

Program to Print Floyd’s Triangle in C, C++, and Python

Below are the ways to print Floyd’s triangle in C, C++, and Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows of the triangle as static input and store it in a variable.
  • Take a variable and initialize it with 1 say sampNum.
  • Loop from 1 to the number of rows of the triangle using For loop.
  • Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
  • Inside the inner for loop print the sampNum with a space character.
  • Increase the value of sampNum by 1.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the triangle as static input and store it in a variable.
triRows = 10
# Take a variable and initialize it with 1 say sampNum.
sampNum = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, triRows+1):
  # Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
    for n in range(1, m+1):
        # Inside the inner for loop print the sampNum with a space character.
        print(sampNum, end=' ')
        # Increase the value of sampNum by 1.
        sampNum = sampNum+1
    # Print the Newline Character after the end of the inner for loop.
    print()

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the triangle as static
    // input and store it in a variable.
    int triRows = 10;
    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            cout << sampNum << " ";
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the triangle as static
    // input and store it in a variable.
    int triRows = 10;
    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            printf("%d ", sampNum);
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows of the triangle as user input and store it in a variable.
  • Take a variable and initialize it with 1 say sampNum.
  • Loop from 1 to the number of rows of the triangle using For loop.
  • Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
  • Inside the inner for loop print the sampNum with a space character.
  • Increase the value of sampNum by 1.
  • Print the Newline Character after the end of the inner for loop.
  • The Exit of the program.

1) Python Implementation

Give the number of rows of the triangle as user input using the int(input()) function and store it in a variable.

Below is the implementation:

# Give the number of rows of the triangle as user input
# using the int(input()) function and store it in a variable.
triRows = int(input('Enter some random number of rows of the triangle = '))
# Take a variable and initialize it with 1 say sampNum.
sampNum = 1
# Loop from 1 to the number of rows of the triangle using For loop.
for m in range(1, triRows+1):
  # Using another For loop, loop from 1 to the parent loop iterator value (Nested For loop).
    for n in range(1, m+1):
        # Inside the inner for loop print the sampNum with a space character.
        print(sampNum, end=' ')
        # Increase the value of sampNum by 1.
        sampNum = sampNum+1
    # Print the Newline Character after the end of the inner for loop.
    print()

Output:

Enter some random number of rows of the triangle = 11
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 

2) C++ Implementation

Give the number of rows of the triangle as user input using the cin function and store it in a variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the triangle as user input
    // using the cin function and store it in a variable.
    int triRows;
    cout<<"Enter some random number of rows of the triangle = ";
    cin >> triRows;

    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            cout << sampNum << " ";
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of the triangle = 13
1 
2 3 
4 5 6 
7 8 9 10 
11 12 13 14 15 
16 17 18 19 20 21 
22 23 24 25 26 27 28 
29 30 31 32 33 34 35 36 
37 38 39 40 41 42 43 44 45 
46 47 48 49 50 51 52 53 54 55 
56 57 58 59 60 61 62 63 64 65 66 
67 68 69 70 71 72 73 74 75 76 77 78 
79 80 81 82 83 84 85 86 87 88 89 90 91

3) C Implementation

Give the number of rows of the triangle as user input using the scanf function and store it in a variable.

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the triangle as user input
    // using the scanf function and store it in a variable.
    int triRows;
    scanf("%d", &triRows);
    // Take a variable and initialize it with 1 say sampNum.
    int sampNum = 1;
    // Loop from 1 to the number of rows of the triangle
    // using For loop.
    for (int m = 1; m <= triRows; m++) {
        for (int n = 1; n <= m; n++) {
            // Inside the inner for loop print the
            // sampNum with a space character.
            printf("%d ", sampNum);
            // Increase the value of sampNum by 1.
            sampNum = sampNum + 1;
        }

        // Print the Newline Character after the end of the
        // inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

number of rows =4
1 
2 3 
4 5 6 
7 8 9 10

Related Programs:

Python Program to Print 1 and 0 in Alternative Rows

Program to Print 1 and 0 in alternative rows in C,C++ and Python

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: