Python

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 Floyd’s Triangle Read More »

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

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 1 and 0 in Alternative Columns Read More »

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:

Python Program to Print Box Number Pattern Read More »

Program to Print an Inverted Star Pattern

Python Program to Print an Inverted Star Pattern | Python code to create an inverted Pyramid start pattern

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.

Python Program to Print the pattern of an Inverted Star: Given a number, the task is to print an Inverted Star Pattern in Python. In our python programming articles, you can also learn how to print a program of Inverted pyramid pattern and print inverted pyramid star pattern in python language.

Let’s see the examples of python program to print inverted star pattern from here

Example1:

Input:

given rows = 8

Output:

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

Example2:

Input:

given rows=10

Output:

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

Here is the program to print Inverted Star Pattern:

# python 3 code to print inverted star
# pattern 
  
# n is the number of rows in which
# star is going to be printed.
n=11
  
# i is going to be enabled to
# range between n-i t 0 with a
# decrement of 1 with each iteration.
# and in print function, for each iteration,
# ” ” is multiplied with n-i and ‘*’ is
# multiplied with i to create correct
# space before of the stars.
for i in range (n, 0, -1):
    print((n-i) * ' ' + i * '*')

python 3 code to print inverted star pattern

Program to Print an Inverted Star Pattern in Python

Below are the ways to print an Inverted Star Pattern in  Python :

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

1)Using for loop printing the inverted star pattern in Python(Static Input)

Using for loop printing the inverted star pattern in Python(Static Input)

Approach:

  • Give the number as static and store it in a variable
  • Use a for loop in which the value of k varies between n-1 and 0 and is decremented by one with each iteration.
  • Multiply empty spaces by n-i and ‘*’ by k then print both of them.
  • Exit of program.

Below is the implementation:

# given number numb
numb = 8
# Use a for loop in which the value of k varies
# between n-1 and 0 and is decremented by one with each iteration.
for k in range(numb, 0, -1):
  # Multiply empty spaces by n-i and '*' by k then print both of them.
    print((numb-k) * ' ' + k * '*')

Output:

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

Explanation:

  • Give the number as static and stored it in  a variable numb
  • The for loop allows I to range between n-1 and 0, with each iteration decrementing by one.
  • To maintain proper star spacing, ” ” is multiplied by n-i and ‘*’ is multiplied by I for each iteration.
  • The requisite pattern has been printed.

2)Using for loop printing the inverted star pattern in Python(User Input)

Using for loop printing the inverted star pattern in Python(User Input)

Approach:

  • Scan the given number using int(input()) and store it in a variable.
  • Use a for loop in which the value of k varies between n-1 and 0 and is decremented by one with each iteration.
  • Multiply empty spaces by n-i and ‘*’ by k then print both of them.
  • Exit of program.

Below is the implementation:

# given number numb
numb = int(input("enter the number of rows required = "))
# Use a for loop in which the value of k varies
# between n-1 and 0 and is decremented by one with each iteration.
for k in range(numb, 0, -1):
  # Multiply empty spaces by n-i and '*' by k then print both of them.
    print((numb-k) * ' ' + k * '*')

Output:

enter the number of rows required = 10
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *

3)Using while loop printing the inverted star pattern in Python(Static Input)

Using while loop printing the inverted star pattern in Python(Static Input)

Approach:

  • Give the number as static and store it in a variable
  • Using while loop iterate till number is greater than 0.
  • Multiply empty spaces by tem-i and ‘*’ by numb then print both of them.
  • Decrement the number by 1.
  • The Exit of the program.

Below is the implementation:

# given number numb
numb = 10
# Make a duplicate of the integer by storing it in a variable
tem = numb
# Use a for loop in which the value of k varies
# between n-1 and 0 and is decremented by one with each iteration.
while(numb > 0):
  # Multiply empty spaces by n-i and '*' by k then print both of them.
    print((tem-numb) * ' ' + numb * '*')
    # decrement the number by 1
    numb = numb-1

Output:

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

4)Using while loop printing the inverted star pattern in Python(User Input)

Using while loop printing the inverted star pattern in Python(User Input)

Approach:

  • Scan the given number using int(input()) and store it in a variable.
  • Using while loop iterate till number is greater than 0.
  • Multiply empty spaces by tem-i and ‘*’ by numb then print both of them.
  • Decrement the number by 1.
  • Exit of program.

Below is the implementation:

# given number numb
numb = int(input("enter the number of rows required = "))
# Make a duplicate of the integer by storing it in a variable
tem = numb
# Use a for loop in which the value of k varies
# between n-1 and 0 and is decremented by one with each iteration.
while(numb > 0):
  # Multiply empty spaces by n-i and '*' by k then print both of them.
    print((tem-numb) * ' ' + numb * '*')
    # decrement the number by 1
    numb = numb-1

Output:

enter the number of rows required = 10
**********
 *********
  ********
   *******
    ******
     *****
      ****
       ***
        **
         *

Programs to print inverted half pyramid using *

This instance is similar to an upright pyramid but that here we begin from the total number of rows and in each iteration, we decrease the number of rows by 1.

Programs to print inverted half pyramid using star

Source Code:

rows = int(input("Enter number of rows: "))

for i in range(rows, 0, -1):
    for j in range(0, i):
        print("* ", end=" ")
    
    print("\n")

output: 

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

Related Programs:

Python Program to Print an Inverted Star Pattern | Python code to create an inverted Pyramid start pattern Read More »

Python Program to Print Right Triangle Number Pattern

Guys who are serious about learning the concepts of the java programming language should practice this list of programs in java and get a good grip on it for better results in exams or interviews.

Given the number of rows(largest number) the task is to print the Right Triangle Number pattern in C, C++, and Python.

Examples:

Example1:

Input:

given number of rows of the right triangle = 11

Output:

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

Example2:

Input:

given number of rows of the right triangle = 9

Output:

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

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

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

Method #1: Using For Loop (Static Input)

Approach:

  • Give the number of rows of the right-angled triangle Number 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 first loop iterator value using another Nested For loop.
  • Print the iterator value of the parent loop with space in the inner For loop. (This prints the same number parent loop number of times)
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the right-angled triangle Number pattern
# as static input and store it in a variable.
triangleNum = 11
# Loop from 1 to the number of rows using For loop.
for m in range(1, triangleNum+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the nested loop with space in the inner For loop.
        # (This prints the same number parent loop number of times)
        print(m, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the right-angled triangle
    // Number pattern
    // as static input and store it in a variable.
    int triangleNum = 19;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= triangleNum;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            cout << m << " ";
        }
        // Print the iterator value of the parent loop with
        // space in the inner For loop.
        /*(This prints the same number parent loop number of
        times*/
        cout << endl;
    }

    return 0;
}

Output:

1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11 
12 12 12 12 12 12 12 12 12 12 12 12 
13 13 13 13 13 13 13 13 13 13 13 13 13 
14 14 14 14 14 14 14 14 14 14 14 14 14 14 
15 15 15 15 15 15 15 15 15 15 15 15 15 15 15 
16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 16 
17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 17 
18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 
19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19 19

3) C Implementation

Below is the implementation:

#include <stdio.h>

int main()
{

    // Give the number of rows of the right-angled triangle
    // Number pattern
    // as static input and store it in a variable.
    int trianglerows = 9;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the iterator value of the parent loop
            // with space in the inner For loop.
            //(This prints the same number parent loop
            //number of times)
            printf("%d ", m);
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For Loop (User Input)

Approach:

  • Give the number of rows of the right-angled triangle Number pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the iterator value of the parent loop with space in the inner For loop. (This prints the same number parent loop number of times)
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

  • Give the number of rows of the right-angled triangle Number pattern as user input using int(input()) and store it in a variable.

Below is the implementation:

# Give the number of rows of the right-angled triangle Number pattern as user input using int(input())
# and store it in a variable.
triangleNum = int(
    input('Enter some random number of rows of the triangle Number pattern ='))
# Loop from 1 to the number of rows using For loop.
for m in range(1, triangleNum+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the iterator value of the nested loop with space in the inner For loop.
        # (This prints the same number parent loop number of times)
        print(m, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows of the triangle Number pattern =12
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5 
6 6 6 6 6 6 
7 7 7 7 7 7 7 
8 8 8 8 8 8 8 8 
9 9 9 9 9 9 9 9 9 
10 10 10 10 10 10 10 10 10 10 
11 11 11 11 11 11 11 11 11 11 11 
12 12 12 12 12 12 12 12 12 12 12 12

2) C++ Implementation

  • Give the number of rows of the right-angled triangle Number pattern 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 right-angled triangle
    // Number pattern
    // as user input using cin and store it in a variable.
    int triangleNum;
    cout << "Enter some random number of rows of the "
            "triangle Number pattern = "
         << endl;
    cin >> triangleNum;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= triangleNum;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            cout << m << " ";
        }
        // Print the iterator value of the parent loop with
        // space in the inner For loop.
        /*(This prints the same number parent loop number of
        times*/
        cout << endl;
    }

    return 0;
}

Output:

Enter some random number of rows of the triangle Number pattern = 
5
1 
2 2 
3 3 3 
4 4 4 4 
5 5 5 5 5

3) C Implementation

  • Give the number of rows of the right-angled triangle Number pattern 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 right-angled triangle
    // Number pattern
    // as user input using scanf and store it in a variable.
    int trianglerows;
    scanf("%d", &trianglerows);
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the iterator value of the parent loop
            // with space in the inner For loop.
            //(This prints the same number parent loop
            // number of times)
            printf("%d ", m);
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Related Programs:

Python Program to Print Right Triangle Number Pattern Read More »

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

Python Program to Print 1 and 0 in Alternative Rows

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

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

Examples:

Example1:

Input:

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

Output:

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

Example2:

Input:

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

Output:

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

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

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

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

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

an odd number, print 1 otherwise, print 0.

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

Method #1: Using For loop

Approach:

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

1) Python Implementation

Below is the implementation:

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

Output:

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

2) C++ Implementation

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

Below is the implementation:

#include <iostream>
using namespace std;

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

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

Output:

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

3) C Implementation

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

Below is the implementation:

#include <stdio.h>

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

Output:

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

Method #2: Using while loop

1) C Implementation

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

Below is the implementation:

#include <stdio.h>

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

Output:

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

2) C++ Implementation:

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

Below is the implementation:

#include <iostream>
using namespace std;

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

Output:

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

3) Python Implementation

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

Below is the implementation:

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

Output:

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

Related Programs:

Python Program to Print 1 and 0 in Alternative Rows Read More »

Python Program to Print Inverted Pyramid 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 number of rows of the pyramid, the task is to print the Inverted Pyramid Star pattern in C, C++, and Python.

Examples:

Example1:

Input:

Given number of rows of the pyramid  =10

Output:

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

Example2:

Input:

Given number of rows of the pyramid  =10
Given character to print='&'

Output:

& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

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

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

Method #1: Using For Loop (Star Character)

Approach:

  • Give the number of rows of the pyramid as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the star character with a space character in the inner For loop.
  • After the end of the two 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 pyramid as static input and store it in a variable.
pyRows = 10
# Loop from the number of rows to 0 in decreasing order using For Loop.
for m in range(pyRows, 0, -1):
    # Loop from 0 to the number of rows - iterator value of the parent
    # For loop using another Nested For loop(Inner For loop)..(0, rows - i):
    for n in range(0, pyRows-m):
        # Print the space character in the inner For loop.
        print(end=' ')
     # Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).

    for l in range(0, m):
        # Print the star character with a space character in the inner For loop.
        print('*', end=' ')
    print()

Output:

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

2) C++ Implementation

Below is the implementation:

#include <iostream>
using namespace std;

int main()
{

    // Give the number of rows of the pyramid as static
    // input and store it in a variable.
    int pyRows=10;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << "* ";
        }

        // Print the Newline Character after the end of the
        // 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 pyramid as static
    // input and store it in a variable.
    int pyRows = 10;
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            printf("* ");
        }

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

Output:

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

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows of the pyramid as user input and store it in a variable.
  • Give the character as static input and store it in a variable.
  • Loop from the number of rows to 0 in decreasing order using For Loop.
  • Loop from 0 to the number of rows – iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the space character in the inner For loop.
  • Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).
  • Print the given character with a space character in the inner For loop.
  • After the end of the two inner For loops print the newline character.
  • The Exit of the Program.

1) Python Implementation

  • Give the number of rows of the pyramid 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 pyramid as user input using int(input()) and store it in a variable.
pyRows = int(input(
    'Enter some random number of rows of pyramid = '))
# Give the Character as user input using input() and store it in another variable.
givencharacter = input('Enter some random character = ')
# Loop from the number of rows to 0 in decreasing order using For Loop.
for m in range(pyRows, 0, -1):
    # Loop from 0 to the number of rows - iterator value of the parent
    # For loop using another Nested For loop(Inner For loop).
    for n in range(0, pyRows-m):
        # Print the space character in the inner For loop.
        print(end=' ')
     # Loop from 0 to the iterator value of the parent For loop using another Nested For loop(Inner For loop).

    for l in range(0, m):
        # Print the given character with a space character in the inner For loop.
        print(givencharacter, end=' ')
    print()

Output:

Enter some random number of rows of pyramid = 10
Enter some random character = &
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

2) C++ Implementation

  • Give the number of rows of the pyramid as user input using scanf 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 pyRows;
    char givencharacter;
    cout << "Enter some random number of rows = " << endl;
    cin >> pyRows;
    // 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 number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            cout << " ";
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the star character with a space
            // character in the inner For loop.
            cout << givencharacter << " ";
        }

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

Output:

Enter some random number of rows of pyramid = 
10
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 pyRows;
    char givencharacter;
    // Give the Character as user input using scanf and
    // store it in another variable.
    scanf("%d", &pyRows);
    scanf("%c", &givencharacter);
    // Loop from the number of rows to 0 in decreasing order
    // using For Loop.
    for (int m = pyRows; m > 0; m--) {
        //  Loop from 0 to the number of rows - iterator
        //  value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int n = 0; n < pyRows - m; n++) {
            //  Print the space character in the inner For
            //  loop.
            printf(" ");
        }
        // Loop from 0 to the iterator value of the parent
        // For loop using another Nested For loop(Inner For
        // loop).
        for (int l = 0; l < m; l++) {
            // Print the given character with a space
            // character in the inner For loop.
            printf("%c ", givencharacter);
        }

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

Output:

10&
& & & & & & & & & & 
 & & & & & & & & & 
  & & & & & & & & 
   & & & & & & & 
    & & & & & & 
     & & & & & 
      & & & & 
       & & & 
        & & 
         &

Related Programs:

Python Program to Print Inverted Pyramid Star Pattern Read More »

Python Program to Print Right Angled Triangle Star Pattern

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 of the right-angled triangle star pattern in C, C++, and python.

Examples:

Example1:

Input:

given number of rows of the right-angled triangle star pattern=19

Output:

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

Example2:

Input:

given number of rows of the right-angled triangle star pattern=11
given character to print =^

Output:

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

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

Below are the ways to print the right-angled triangle star pattern in C, C++, and Python.

Method #1: Using For loop (Star Character)

Approach:

  • Give the number of rows of the right-angled triangle star 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 first loop iterator value using another Nested For loop.
  • Print the star character with space in the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

Below is the implementation:

# Give the number of rows of the right-angled triangle star pattern
# as static input and store it in a variable.
trianglerows = 19
# Loop from 1 to the number of rows using For loop.
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the star character with space in the inner For loop.
        print('*', end=' ')
    #Print the newline character after ending of 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 right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 11;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            cout << "* ";
        }
        // Print the newline character after ending of 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 right-angled triangle
    // star pattern as static input and store it in a
    // variable.
    int trianglerows = 9;
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            printf("* ");
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

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

Method #2: Using For loop (User Character )

Approach:

  • Give the number of rows of the right-angled triangle star pattern as user input and store it in a variable.
  • Loop from 1 to the number of rows using For loop.
  • Loop from 1 to first loop iterator value using another Nested For loop.
  • Print the star character with space in the inner For loop.
  • Print the newline character after ending of inner For loop.
  • The Exit of the program.

1) Python Implementation

  • Give the number of rows of the right-angled 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 right-angled triangle as user input using int(input()) and store it in a variable.
trianglerows = 17
# Give the Character as user input using input() and store it in another variable.
givencharacter = '{'
for m in range(1, trianglerows+1):
    # Loop from 1 to first loop iterator value using another Nested For loop.
    for n in range(1, m+1):
        # Print the star character with space in the inner For loop.
        print(givencharacter, end=' ')
    # Print the newline character after ending of inner For loop.
    print()

Output:

Enter some random number of rows of the right-angled triangle = 14
Enter some random character = |
| 
| | 
| | | 
| | | | 
| | | | | 
| | | | | | 
| | | | | | | 
| | | | | | | | 
| | | | | | | | | 
| | | | | | | | | | 
| | | | | | | | | | | 
| | | | | | | | | | | | 
| | | | | | | | | | | | | 
| | | | | | | | | | | | | |

2) C++ Implementation

  • Give the number of rows of the right-angled 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()
{
    int trianglerows;
    char givencharacter;

    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    cout << "Enter some random number of rows of the "
            "right-angled triangle = "
         << endl;
    cin >> trianglerows;
    // 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 <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            cout << givencharacter << " ";
        }
        // Print the newline character after ending of inner
        // For loop.
        cout << endl;
    }
    return 0;
}

Output:

Enter some random number of rows of the right-angled triangle = 
7
Enter some random character = 
&
& 
& & 
& & & 
& & & & 
& & & & & 
& & & & & & 
& & & & & & & 

3) C Implementation

  • Give the number of rows of the right-angled 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()
{

    int trianglerows;
    char givencharacter;
    // Give the number of rows of the right-angled triangle
    // as user input using cin and store it in a variable.
    scanf("%d", &trianglerows);

    // Give the Character as user input using cin and store
    // it in another variable.
    scanf("%c", &givencharacter);
    printf("\n");
    // Loop from 1 to the number of rows using For loop.
    for (int m = 1; m <= trianglerows;
         m++) { // Loop from 1 to first loop iterator value
                // using another Nested For loop.
        for (int n = 1; n <= m; n++) {
            // Print the star character with space in the
            // inner For loop.
            printf("%c ", givencharacter);
        }
        // Print the newline character after ending of inner
        // For loop.
        printf("\n");
    }
    return 0;
}

Output:

11^
^ 
^ ^ 
^ ^ ^ 
^ ^ ^ ^ 
^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ 
^ ^ ^ ^ ^ ^ ^ ^ ^ ^ ^

Related Programs:

Python Program to Print Right Angled Triangle Star Pattern Read More »

Python Program to Print Hollow Box Pattern of Numbers

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 and number of columns of the box, the task is to print the Hollow Box Number pattern in C, C++, and python.

Examples:

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

Example2:

Input:

given number of rows of the box =11
given number of columns of the box =19
given character to print ='&'

Output:

& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

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

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

Method #1: Using For Loop (Static Character)

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 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 space character.
            print(" ", 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                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   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 space character.
                cout << "  ";
            }
        }
        // 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                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   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 space character.
                printf("  ");
            }
        }
        // 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                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1                                                   1 
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1

Method #2: Using For Loop (User Character)

Approach:

  • Give the number of rows and number of columns of the box as user input and store them in two separate variables.
  • Give the character to print as a box pattern as user input and store it in a variable.
  • 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 the given character with a space character.
  • Else print space character.
  • 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 box as user input using int(input()) and store it in a variable.
  • Give the number of columns of the box as user input using int(input()) and store it in another variable.
  • Give the Character as user input using input() and store it in another variable.

Below is the implementation:

# Give the number of rows and number of columns of the box as user input and store them in two separate variables.
boxrows = int(input("Enter some random number of rows of the box = "))
boxcolumns = int(input("Enter some random number of columns of the box = "))
# Give the character to print as a box pattern as user input and store it in a variable.
userchar = input('Enter some random character to print = ')
# 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 the given character with a space character.
            print(userchar, end=" ")
        else:
            # Else print space character.
            print(" ", end=" ")
    # Print the newline character after the end of the inner For loop.
    print()

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

2) C++ Implementation

  • Give the number of rows of the box as user input using cin and store it in a variable.
  • Give the number of columns of the box as user input using cin and store it in another 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 box as user input
    // using cin and store it in a variable.
    // Give the number of columns of the box as user input
    // using cin and store it in another variable.
    int boxrows;
    int boxcolumns;
    cin >> boxrows;
    cin >> boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    cin >> userchar;
    cout << endl;
    // 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 user
                // character with a space character.
                cout << userchar << " ";
            }
            else {
                // Else print space character.
                cout << "  ";
            }
        }
        // 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 box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

3) C Implementation

  • Give the number of rows of the box as user input using scanf and store it in a variable.
  • Give the number of columns of the box as user input using scanf and store it in another 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 box as user input
    // using scanf and store it in a variable.
    // Give the number of columns of the box as user input
    // using scanf and store it in another variable.
    int boxrows;
    int boxcolumns;
    // Give the Character as user input using cin and store
    // it in another variable.
    char userchar;
    scanf("%d%d%c",&boxrows,&boxcolumns,&userchar);
    printf("\n");
    // 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 user
                // character with a space character.
                printf("%c ", userchar);
            }
            else {
                // Else print space character.
                printf("  ");
            }
        }
        // Print the newline character after the end of the
        // inner For loop.
        printf("\n");
    }
    return 0;
}

Output:

Enter some random number of rows of the box = 9
Enter some random number of columns of the box = 13
Enter some random character to print = &
& & & & & & & & & & & & & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
&                                            & 
& & & & & & & & & & & & &

Related Programs:

 

Python Program to Print Hollow Box Pattern of Numbers Read More »