CPP Programming

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:

Program to Print Rectangle Star Pattern in C,C++ and Python

Python Program to Print Rectangle Star Pattern

Enhancing programming skills is very important no matter what language you have chosen. So, practice frequently with these simple java programs examples and excel in coding the complex logic.

Given the length, breadth of the rectangle the task is to print the rectangle star pattern in C, C++, and Python.

Examples:

Example1:

Input:

given length of rectangle =8
given breadth of rectangle =29

Output:

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

Example2:

Input:

given length of rectangle =19
given breadth of rectangle =5

Output:

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

Program to Print Rectangle Star Pattern in C, C++, and Python

Below are the ways to print the rectangle star pattern in C, C++, and Python.

Method #1: Using For loop (Static Input)

Approach:

  • Give the length and breadth as static input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the length and breadth as static input and store them in two variables.
lengthnum = 8
breadthnum = 11
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 11;
    int breadthnum = 23;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;n++) { // Print the * character inside the
                    // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{
    // Give the length and breadth as static input and store
    // them in two variables.
    int lengthnum = 9;
    int breadthnum = 29;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

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

Method #2: Using For loop (User Input)

Approach:

  • Give the length and breadth as user input and store them in two variables.
  • Loop till the length of the rectangle using For loop.
  • Loop till the breadth of the rectangle using another nested For loop.
  • Print the * character inside the nested For loop.
  • The Exit of the Program.

1) Python Implementation

Give the length and breadth of the rectangle as user input using the int(input()) function and store them in two separate variables.

Below is the implementation:

# Give the length and breadth of the rectangle as user input using the int(input()) function
# and store them in two separate variables.
lengthnum = int(input('Enter some random length value of the rectangle = '))

breadthnum=int(input('Enter some random breadth value of the rectangle = '))
# Loop till the length of the rectangle using For loop.
for m in range(lengthnum):
    # Loop till the breadth of the rectangle using another nested For loop.
    for n in range(breadthnum):
        # Print the * character inside the nested For loop.
        print('* ', end=' ')
    print()

Output:

Enter some random length value of the rectangle = 9
Enter some random breadth value of the rectangle = 4
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 
* * * * 

2) C++ Implementation

Give the length and breadth of the rectangle as user input using the cin and store them in two separate variables.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the length and breadth of the rectangle as user
    // input using the cin
    // and store them in two separate variables.
    int lengthnum, breadthnum;
    cout << "Enter some random length and breadth of "
            "rectangle separated by spaces"
         << endl;
    cin >> lengthnum >> breadthnum;
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum;
             n++) { // Print the * character inside the
            // nested For loop.
            cout << "* ";
        }
        cout << endl;
    }

    return 0;
}

Output:

Enter some random length and breadth of rectangle separated by spaces
4 16
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * * 

3) C Implementation

Give the length and breadth of the rectangle as user input using the scanf and store them in two separate variables.

Below is the implementation:

#include <stdio.h>

int main()
{
    //    Give the length and breadth of the rectangle as
    // user input using the scanf
    // and store them in two separate variables.
    int lengthnum,breadthnum ;
    printf("Enter some random length and breadth of rectangle separated by spaces \n");
    scanf("%d%d", &lengthnum, &breadthnum);
    // Loop till the length of the rectangle using For loop.
    for (int m = 0; m < lengthnum; m++) {
        // Loop till the breadth of the rectangle using
        // another nested For loop.
        for (int n = 0; n < breadthnum; n++) {
            // Print the * character inside the
            // nested For loop.
            printf("* ");
        }
        printf("\n");
    }
}

Output:

Enter some random length and breadth of rectangle separated by spaces
8 15
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * * 
* * * * * * * * * * * * * * *

Related Programs:

Python Program to Print Hollow Inverted Right Triangle

Have you mastered basic programming topics of java and looking forward to mastering advanced topics in a java programming language? Go with these ultimate Advanced java programs examples with output & achieve your goal in improving java coding skills.

Given the number of rows of the Triangle, the task is to Print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =7

Output:

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

 

Example2:

Input:

Given number of rows of the Hollow Inverted Right Triangle Pattern =9
Given character to print ='^'

Output:

^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

Program to Print Hollow Inverted Right Triangle Star Pattern in C, C++, and Python

Below are the ways to print a Hollow Inverted Right Triangle Star Pattern in C, C++, and Python.

Method #1: Using For Loop(Star Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
triNumRows = 7
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print('*', end='')
        else:
            print(' ', end='')
    # Print the newline character after inner for loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << "* ";
            else
                cout << "  ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 7;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("* ");
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

  • Give the number of rows of the Hollow Inverted Right Triangle as static input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop from the iterator value of the first loop to 0 using another nested For loop.
  • If m equals zero, rows, n, or n equals one value, the if statement prints stars.
  • Else print space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle 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:

# Give the number of rows of the Inverted Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Inverted Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, 0, -1):
    # Loop from the  iterator value of the first loop to 0 using another nested For loop.
    for n in range(m, 0, -1):
        # If m equals zero, rows, n, or n equals one value, the if statement prints stars.
        if m == 1 or m == triNumRows or n == 1 or n == m:
            print(givencharacter, end='')
        else:
            print(' ', end='')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

2) C++ Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle as user input using cin and store it in a variable.
  • Give the Character as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows of the Inverted Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 9
Enter some random character = ^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

3) C Implementation

  • Give the number of rows of the Hollow Inverted Right Triangle as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.

    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m > 0; m--) {

        // Loop from the iterator value of the first loop to
        // 0 using another nested For loop.
        for (int n = m; n > 0; n--) {
            // If m equals zero, rows, n, or n equals one
            // value, the if statement prints stars.
            if (m == 1 || m == triNumRows || n == 1
                || n == m)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9^
^^^^^^^^^
^               ^
^            ^
^          ^
^        ^
^      ^
^    ^
^ ^
^

Related Programs:

Python Program to Print Diamond Star Pattern

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Given the number of rows of the diamond pattern, the task is to print the diamond star pattern in C, C++, and Python

Examples:

Example1:

Input:

given number of rows of diamond =5

Output:

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

Example2:

Input:

given number of rows of diamond =7
given character to print ='$'

Output:

          $
        $$$
       $$$$$
     $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
$$$$$$$$$$$$$
  $$$$$$$$$$$
    $$$$$$$$$
      $$$$$$$
        $$$$$
         $$$
           $

Program to Print Diamond Star Pattern in C, C++, and Python

Below are the ways to print Diamond Star Pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the diamond pattern as static input and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the 2* iterator value – 1 of the parent For loop using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 1 to 2*(number of rows – iterator value of the parent for loop) using another For loop(Nested For loop).
  • Print the star character.
  • After the end of the inner for Loops print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the diamond pattern as static input and store it in a variable.

diamondrows = 5
# Loop from 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the 2* iterator value - 1 of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, (2 * m) - 1):
        # Print the star character
        print('*', end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# Loop from 1 to the number of rows using For loop.
for m in range(1, diamondrows):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for n in range(1, m+1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to 2*(number of rows - iterator value of the parent for loop)
    # using another For loop(Nested For loop).
    for l in range(1, (2 * (diamondrows - m))):
        # Print the star character
        print('*', end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 5;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the star character
            cout << "*";
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the star character
            cout << "*";
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{ // Give the number of rows of the diamond pattern
    // as static input and store it in a variable.

    int diamondrows = 5;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the star character
            printf("*");
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the star character
            printf("*");
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the diamond pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from 1 to the number of rows using For Loop.
  • Loop from 1 to the number of rows -iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the 2* iterator value – 1 of the parent For loop using another For loop(Nested For loop).
  • Print the given character.
  • After the end of the inner for Loops print the Newline Character.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to iterator value of the parent For loop using another For loop(Nested For loop).
  • Print the space character in the inner For loop.
  • Loop from 1 to 2*(number of rows – iterator value of the parent for loop) using another For loop(Nested For loop).
  • Print the given character.
  • After the end of the inner for Loops 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.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

# Give the number of rows  as user input using int(input()) and store it in a variable.
diamondrows = int(input(
    'Enter some random number of rows  = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 1 to the number of rows using For Loop.
for m in range(1, diamondrows+1):
    # Loop from 1 to the number of rows -iterator value of the parent
    # For loop using another For loop(Nested For loop).
    for n in range(1, diamondrows - m + 1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 0 to the 2* iterator value - 1 of the parent For loop
    # using another For loop(Nested For loop).
    for l in range(0, (2 * m) - 1):
        # Print the given character
        print(givencharacter, end='')
    # After the end of the inner for Loops print the Newline Character.
    print()

# Loop from 1 to the number of rows using For loop.
for m in range(1, diamondrows):
    # Loop from 1 to iterator value of the parent For loop
    # using another For loop(Nested For loop).
    for n in range(1, m+1):
        # Print the space character in the inner For loop.
        print(end=' ')
    # Loop from 1 to 2*(number of rows - iterator value of the parent for loop)
    # using another For loop(Nested For loop).
    for l in range(1, (2 * (diamondrows - m))):
        # Print the given character
        print(givencharacter, end='')

    # After the end of the inner for Loops print the Newline Character.
    print()

Output:

Enter some random number of rows = 7
Enter some random character = $
          $
        $$$
       $$$$$
     $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
$$$$$$$$$$$$$
  $$$$$$$$$$$
    $$$$$$$$$
      $$$$$$$
        $$$$$
         $$$
           $

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows
    // as user input using cin and store it in a
    // variable.
    int diamondrows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> diamondrows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the given character
            cout << givencharacter;
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        cout << endl;
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            cout << " ";
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the  given character
            cout << givencharacter;
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows = 
7
Enter some random character = 
$
          $
        $$$
       $$$$$
     $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
$$$$$$$$$$$$$
  $$$$$$$$$$$
    $$$$$$$$$
      $$$$$$$
        $$$$$
         $$$
           $

3) C Implementation

  • Give the number of rows as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another 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 diamondrows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &diamondrows);
    scanf("%c", &givencharacter);
    // Loop from 1 to the number of rows using For Loop.
    for (int m = 1; m <= diamondrows; m++) {

        // Loop from 1 to the number of rows- iterator value
        // of the parent For loop using another For
        // loop(Nested For loop).
        for (int n = 1; n <= (diamondrows - m); n++) {

            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 0 to the 2 * iterator value- 1 of the
        // parent For loop using another For loop(Nested For
        // loop).
        for (int l = 0; l < ((2 * m) - 1); l++) {
            // Print the given character
            printf("%c", givencharacter);
        }
        // After the end of the inner for Loops print the
        // Newline Character.
        printf("\n");
    }
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m < diamondrows; m++) {
        // Loop from 1 to iterator value of the parent For
        // loop using another For loop(Nested For loop).
        for (int n = 1; n <= m; n++) {
            // Print the space character in the inner For
            // loop.
            printf(" ");
        }
        // Loop from 1 to 2* (number of rows- iterator value
        // of the parent for loop) using another For
        // loop(Nested For loop).
        for (int l = 1; l < (2 * (diamondrows - m)); l++) {
            // Print the givencharacter
            printf("%c", givencharacter);
        }

        // After the end of the inner for Loops print the
        // Newline    Character.
        printf("\n");
    }
    return 0;
}

Output:

7$
          $
        $$$
       $$$$$
     $$$$$$$
   $$$$$$$$$
  $$$$$$$$$$$
$$$$$$$$$$$$$
  $$$$$$$$$$$
    $$$$$$$$$
      $$$$$$$
        $$$$$
         $$$
           $

Related Programs:

Python Program to Print Hollow Half Diamond 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 diamond pattern, the task is to print the Hollow Half diamond Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows =7

Output:

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

Example2:

Input:

Given number of rows =9
Given Character to print ='-'

Output:

- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

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

Below are the ways to print Hollow Half Diamond Star Pattern in C, C++, and python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the number of diamond pattern as static input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop till the first iterator value using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to 0 using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print star character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • After the end of two For loops Loop from 1 to the number of rows using For loop.
  • Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to the number of rows using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print star character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the number of diamond pattern as static input and store it in a variable.
rowsnumber = 7
# Loop from 0 to the number of rows using For loop.
for m in range(0, rowsnumber):
        # Loop till the first iterator value using another For loop(Nested For loop).
    for n in range(0, m+1):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
        # Check if the second loop iterator value is equal to 0 using the If Statement.
        # Combine these two conditions using or operator.
        if(m == n or n == 0):
            # If the statement is true then print star character with space.
            print("*", end=" ")
          # Else print space character.
        else:
            print(' ', end=' ')
    # After the end of the inner for loop print the Newline Character.
    print()
# After the end of two For loops Loop from 1 to the number of rows using For loop.
for m in range(1, rowsnumber):
    # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
    for n in range(m, rowsnumber):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
                # Check if the second loop iterator value is equal to the number of rows using the If Statement.
                # Combine these two conditions using or operator.
        if(m == n or n == rowsnumber-1):
         # If the statement is true then print star character with space.
            print('*', end=' ')
          # Else print space character.
        else:
            print(' ', end=' ')

    # After the end of the inner for loop print the Newline Character.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    //Give the number of rows of the number of diamond pattern  \
  //  as static input and store it in a variable.
    int rowsnumber = 7;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {
            /*
            Check if the first loop iterator value is equal
            to the second loop iterator value using the If
            Statement. Check if the second loop iterator
            value is equal to 0 using the If Statement.
              Combine these two conditions using or
            operator. If the statement is true then print
            star character with space. Else print space
            character.*/
            if (m == n || n == 0)
                cout << "* ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print star character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                cout << "* ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    //Give the number of rows of the number of diamond pattern  \
  //  as static input and store it in a variable.
    int rowsnumber = 7;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {

            /*
           Check if the first loop iterator value is equal
           to the second loop iterator value using the If
           Statement. Check if the second loop iterator
           value is equal to 0 using the If Statement.
             Combine these two conditions using or
           operator. If the statement is true then print
           star character with space. Else print space
           character.*/
            if (m == n || n == 0)
                printf("* ");
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print star character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                printf("* ");
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character)

Approach:

  • Give the number of rows of the number of diamond pattern as user input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from 0 to the number of rows using For loop.
  • Loop till the first iterator value using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to 0 using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print the given character with space.
  • Else print space character.
  • After the end of the inner for loop print the Newline Character.
  • After the end of two For loops Loop from 1 to the number of rows using For loop.
  • Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
  • Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
  • Check if the second loop iterator value is equal to the number of rows using the If Statement.
  • Combine these two conditions using or operator.
  • If the statement is true then print the given character with space.
  • Else print space character.
  • After the end of the inner for loop 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.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

# Give the number of rows  as user input using int(input()) and store it in a variable.
rowsnumber = int(input(
    'Enter some random number of rows  = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from 0 to the number of rows using For loop.
for m in range(0, rowsnumber):
        # Loop till the first iterator value using another For loop(Nested For loop).
    for n in range(0, m+1):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
        # Check if the second loop iterator value is equal to 0 using the If Statement.
        # Combine these two conditions using or operator.
        if(m == n or n == 0):
            # If the statement is true then print the given character with space.
            print(givencharacter, end=" ")
          # Else print space character.
        else:
            print(' ', end=' ')
    # After the end of the inner for loop print the Newline Character.
    print()
# After the end of two For loops Loop from 1 to the number of rows using For loop.
for m in range(1, rowsnumber):
    # Loop from the first iterator value to the given number of rows using another For loop(Nested For loop).
    for n in range(m, rowsnumber):
        # Check if the first loop iterator value is equal to the second loop iterator value using the If Statement.
                # Check if the second loop iterator value is equal to the number of rows using the If Statement.
                # Combine these two conditions using or operator.
        if(m == n or n == rowsnumber-1):
         # If the statement is true then print the given character with space.
            print(givencharacter, end=' ')
          # Else print space character.
        else:
            print(' ', end=' ')

    # After the end of the inner for loop print the Newline Character.
    print()

Output:

Enter some random number of rows = 9
Enter some random character = -
- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows
    // as user input using cin and store it in a
    // variable.
    int rowsnumber;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> rowsnumber;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {
            /*
            Check if the first loop iterator value is equal
            to the second loop iterator value using the If
            Statement. Check if the second loop iterator
            value is equal to 0 using the If Statement.
              Combine these two conditions using or
            operator. If the statement is true then print
            given character with space. Else print space
            character.*/
            if (m == n || n == 0)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print given character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                cout << givencharacter << " ";
            else
                cout << "  ";
        }
        // After the end of the inner for loop print the
        // Newline Character.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows = 9
Enter some random character = -
- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

3) C Implementation

  • Give the number of rows as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another 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 rowsnumber;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &rowsnumber);
    scanf("%c", &givencharacter);
 
    // Loop from 0 to the number of rows using For loop.
    for (int m = 0; m < rowsnumber; m++) {
        // Loop till the first iterator value using another
        // For loop(Nested For loop)
        for (int n = 0; n < m + 1; n++) {

            /*
           Check if the first loop iterator value is equal
           to the second loop iterator value using the If
           Statement. Check if the second loop iterator
           value is equal to 0 using the If Statement.
             Combine these two conditions using or
           operator. If the statement is true then print
           given character with space. Else print space
           character.*/
            if (m == n || n == 0)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    // After the end of two For loops Loop from 1 to the
    // number of rows using For loop.
    for (int m = 1; m < rowsnumber; m++)
    // Loop from the first iterator value to the given
    // number of rows using another For loop(Nested For
    // loop)
    {
        for (int n = m; n < rowsnumber; n++) {

            /*
             Check if the first loop iterator value is equal
             to the second loop iterator value using the If
             Statement. Check if the second loop iterator
             value is equal to the number of rows using the
             If Statement. Combine these two conditions
             using or operator. If the statement is true
             then print given character with space. Else
             print space character.*/
            if (m == n || n == rowsnumber - 1)
                printf("%c ", givencharacter);
            else
                printf("  ");
        }
        // After the end of the inner for loop print the
        // Newline Character.
        printf("\n");
    }
    return 0;
}

Output:

9-
- 
- - 
-   - 
-     - 
-       - 
-         - 
-           - 
-         - 
-       - 
-     - 
-   - 
- - 
-

Related Programs:

Python Program to Print Inverted Right Triangle Star Pattern

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Given the number of rows of the Triangle, the task is to Print an Inverted Right Triangle Star Pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the Inverted Right Triangle Pattern =13

Output:

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

Example2:

Input:

Given number of rows of the Inverted Right Triangle Pattern =9
Given character to print ='<'

Output:

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

Program to Print Inverted Right Triangle Star Pattern in C, C++, and Python

Below are the ways to print an Inverted Right Triangle Star Pattern in C, C++, and Python.

Method #1: Using For Loop(Star Character)

Approach:

  • Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop till the iterator value of the first loop using another nested For loop.
  • Print the star and space character in the inner for loop.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
triNumRows = 13
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the star and space character in the inner for loop.
        print('*', end=' ')
    # Print the newline character after inner for loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 5;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the star and space character in the
            // inner for loop.
            cout << "* ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

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

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 13;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the star and space character in the
            // inner for loop.
            printf("* ");
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop(User Character)

Approach:

  • Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
  • Give the character to print as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop till the iterator value of the first loop using another nested For loop.
  • Print the given character and space character in the inner for loop.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the Inverted Right Triangle 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:

# Give the number of rows of the Inverted Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Inverted Right Triangle Pattern = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the given character with the space
        print(givencharacter, end=' ')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 7
Enter some random character = @
@ @ @ @ @ @ @ 
@ @ @ @ @ @ 
@ @ @ @ @ 
@ @ @ @ 
@ @ @ 
@ @ 
@

2) C++ Implementation

  • Give the number of rows of the Inverted Right Triangle as user input using cin and store it in a variable.
  • Give the Character as user input using cin and store it in another variable.

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // Give the Character as user input using cin and store
    // it in another variable.
    cout << "Enter some random character = " << endl;
    cin >> givencharacter;
    cout << endl;

    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the given character with the space
            cout << givencharacter << " ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 
15
Enter some random character = 
^
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ 
^ ^ ^ ^ 
^ ^ ^ 
^ ^ 
^

3) C Implementation

  • Give the number of rows of the Inverted Right Triangle as user input using scanf and store it in a variable.
  • Give the Character as user input using scanf and store it in another variable.

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.

    scanf("%d", &triNumRows);
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the given character and space character
            // in the inner for loop.
            printf("%c ", givencharacter);
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

9<
< < < < < < < < < 
< < < < < < < < 
< < < < < < < 
< < < < < < 
< < < < < 
< < < < 
< < < 
< < 
<

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 Inverted Right Triangle of Numbers

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 an Inverted Right Triangle of Numbers in C, C++, and Python

Examples:

Example1:

Input:

given number of rows of the Inverted Right Triangle Pattern =13

Output:

13 13 13 13 13 13 13 13 13 13 13 13 13 
12 12 12 12 12 12 12 12 12 12 12 12 
11 11 11 11 11 11 11 11 11 11 11 
10 10 10 10 10 10 10 10 10 10 
9 9 9 9 9 9 9 9 9 
8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

Example2:

Input:

given number of rows of the Inverted Right Triangle Pattern =8

Output:

8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

Program to Print an Inverted Right Triangle of Numbers in C, C++, and Python

Below are the ways to Print an Inverted Right Triangle of Numbers in C, C++, and Python.

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop till the iterator value of the first loop using another nested For loop.
  • Print the First Loop iterator value that is m and space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the Inverted Right Triangle as static input and store it in a variable.
triNumRows = 13
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the First Loop iterator value that is m and space character.
        print(m, end=' ')
    # Print the newline character after inner for loop.
    print()

Output:

13 13 13 13 13 13 13 13 13 13 13 13 13 
12 12 12 12 12 12 12 12 12 12 12 12 
11 11 11 11 11 11 11 11 11 11 11 
10 10 10 10 10 10 10 10 10 10 
9 9 9 9 9 9 9 9 9 
8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 8;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the First Loop iterator value that is m
            // and space character.
            cout << m << " ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the Inverted Right
    // Triangle as static input and store it in a variable.
    int triNumRows = 8;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the First Loop iterator value that is m
            // and space character.
            printf("%d ", m);
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1 

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows of the Inverted Right Triangle as user input and store it in a variable.
  • Loop from the given number of rows to 0 using For loop and take iterator value as m.
  • Loop till the iterator value of the first loop using another nested For loop.
  • Print the First Loop iterator value that is m and space character.
  • Print the newline character after inner for loop.
  • The Exit of the Program.

1) Python Implementation

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

Below is the implementation:

# Give the number of rows of the Inverted Right Triangle as user input using int(input()) and store it in a variable.
triNumRows = int(input(
    'Enter some random number of rows of the Inverted Right Triangle Pattern = '))
# Loop from the given number of rows to 0 using For loop and take iterator value as m.
for m in range(triNumRows, -1, -1):
    # Loop till the iterator value of the first loop using another nested For loop.
    for n in range(m):
        # Print the First Loop iterator value that is m and space character.
        print(m, end=' ')
    # Print the newline character after inner for loop.
    print()

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 7
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

2) C++ Implementation

Give the number of rows of the Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using cin and store it in a
    // variable.
    int triNumRows;
    cout << "Enter some random number of rows of the "
            "Inverted Right Triangle Pattern = "
         << endl;
    cin >> triNumRows;
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the First Loop iterator value that is m
            // and space character.
            cout << m << " ";
        }
        // Print the newline character after inner for loop.
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the Inverted Right Triangle Pattern = 
6
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

3) C Implementation

Give the number of rows of the Inverted Right Triangle 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 of the Inverted Right
    // Triangle as user input using scanf and store it in a
    // variable.
    int triNumRows;
    scanf("%d", &triNumRows);
    // Loop from the given number of rows to 0 using For
    // loop and take iterator value as m.
    for (int m = triNumRows; m >= 0; m--) {

        // Loop till the iterator value of the first loop
        // using another nested For loop.
        for (int n = 0; n < m; n++) {
            // Print the First Loop iterator value that is m
            // and space character.
            printf("%d ", m);
        }
        // Print the newline character after inner for loop.
        printf("\n");
    }
    return 0;
}

Output:

15
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 
14 14 14 14 14 14 14 14 14 14 14 14 14 14 
13 13 13 13 13 13 13 13 13 13 13 13 13 
12 12 12 12 12 12 12 12 12 12 12 12 
11 11 11 11 11 11 11 11 11 11 11 
10 10 10 10 10 10 10 10 10 10 
9 9 9 9 9 9 9 9 9 
8 8 8 8 8 8 8 8 
7 7 7 7 7 7 7 
6 6 6 6 6 6 
5 5 5 5 5 
4 4 4 4 
3 3 3 
2 2 
1

Related Programs:

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 Box Number Pattern

Explore complete java concepts from the Java programming examples and get ready to become a good programmer and crack the java software developer interview with ease.

Given the number of rows and number of columns of the box, the task is to print the Box Number pattern in C, C++, and python.

Examples:

Outer Elements 1 and inner Elements 0:

Example1:

Input:

given number of rows of the box =11
given number of columns of the box =19

Output:

1 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 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 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 1 1 1 1 1

Outer Elements 0 and inner Elements 1:

Example2:

Input:

given number of rows of the box =11
given number of columns of the box =19

Output:

0 0 0 0 0 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 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 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 0

Program to Print Box Number Pattern in C, C++, and Python

Below are the ways to Print Box Number Pattern in C, C++, and python.

Method #1: Box Pattern with 1 and 0 characters (Outer Elements 1 and Inner elements 0)

Approach:

  • Give the number of rows and number of columns of the box as static or user input and store them in two separate variables.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print 1 with a space character.
  • Else print 0 with a space character.
  • 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 and number of columns of the box as static or user input
# and store them in two separate variables.
boxrows = 11
boxcolumns = 19
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print 1 with a space character.
            print("1", end=" ")
        else:
            # Else print 0 with a space character.
            print("0", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

1 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 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 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 1 1 1 1 1

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                cout << "1 ";
            }
            else {
                // Else print 0 with a space character.
                cout << "0 ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

1 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 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 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 1 1 1 1 1

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 1
                // with a space character.
                printf("1 ");
            }
            else {
                // Else print 0 with a space character.
                printf("0 ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

1 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 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
1 0 0 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 1 1 1 1 1

Method #2: Box Pattern with 1 and 0 characters (Outer Elements 0 and Inner elements 1)

Approach:

  • Give the number of rows and number of columns of the box as static or user input and store them in two separate variables.
  • Loop from 1 to the number of rows of the box +1 using a For loop.
  • Loop from 1 to the number of columns of the box +1 using another Nested For loop.
  • Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
  • Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
  • Merge these two conditions in a single If statement using or operator.
  • If the condition is true then print 0 with a space character.
  • Else print 1 with a space character.
  • 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 and number of columns of the box as static or user input
# and store them in two separate variables.
boxrows = 11
boxcolumns = 19
# Loop from 1 to the number of rows of the box +1 using a For loop.
for m in range(1, boxrows+1):
    # Loop from 1 to the number of columns of the box +1 using another Nested For loop.
    for n in range(1, boxcolumns+1):
        '''Check if the parent iterator value is equal to 1 or the number of rows of the box using the If statement.
            Check if the inner loop iterator value is equal to 1 or the number of columns of the box using the If statement.
            Merge these two conditions in a single If statement using or operator.       
                  '''
        if(m == 1 or m == boxrows or n == 1 or n == boxcolumns):
            # If the condition is true then print 0 with a space character.
            print("0", end=" ")
        else:
            # Else print 1 with a space character.
            print("1", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

0 0 0 0 0 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 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 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 0

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{
    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 0
                // with a space character.
                cout << "0 ";
            }
            else {
                // Else print 1 with a space character.
                cout << "1 ";
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        cout << endl;
    }

    return 0;
}

Output:

0 0 0 0 0 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 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 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 0

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows and number of columns of the
    // box as static or user input and store them in two
    // separate variables.
    int boxrows = 11;
    int boxcolumns = 19;
    // Loop from 1 to the number of rows of the box +1 using
    // a For loop.
    for (int m = 1; m <= boxrows; m++) {
        // Loop from 1 to the number of columns of the box
        // +1 using another Nested For loop.
        for (int n = 1; n <= boxcolumns; n++) {
            /*Check if the parent iterator value is equal to
               1 or the number of rows of the box using the
               If statement. Check if the inner loop
               iterator value is equal to 1 or the number of
               columns of the box using the If statement.
                Merge these two conditions in a single If
               statement using or operator. */
            if (m == 1 || m == boxrows || n == 1
                || n == boxcolumns) {
                // If the condition is true then print 0
                // with a space character.
                printf("0 ");
            }
            else {
                // Else print 1 with a space character.
                printf("1 ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

0 0 0 0 0 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 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 
0 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 0

Related Programs: