Login Register Appointment

Combine Both Pyramid

Lesson 20/21 | Study Time: 15 Min
Course: Pattern Program

Pattern:

      *

     ***

    *****

   *******

  *********

   *******

    *****

     ***

      *

    



Solution

Note: Don't jump directly to the solution, try it out yourself first.


Pattern Breakdown

The pattern has 4 rows, and each row contains stars (*) in a decreasing number:


Upper Half

Row 0: 3 Space 1 star 3 space

Row 1: 2 Space 3 star 2 space

Row 2: 1 Space 5 star 1 space

Row 3: 0 Space 7 star 0 space


Lower Half:

Row 4: 1 Space 5 star 1 space

Row 5: 2 Space 3 star 2 space

Row 6: 3 Space 1 star 3 space


Key observation:

UpperHalf:


The number of spaces decrease with each row, starting from 3 (for row 0) and going down to 0 (for row 3).

The number of star increase with each row, starting from 1 star (for row 0) and going up to 7 star(for row 3).


First Inner Loop (Spaces):

The first inner loop prints the spaces before the number in each row.

The number of spaces in each row is equal to middle_row - row (middle_row= total_row/2)


Second Inner Loop (Stars):

The second inner loop prints the start in each row.

The number of stars in each row increased by 2 for each row.

For the i-th row, the number of stars is 2 * row + 1

Lower Half :


The number of spaces increase with each row, starting from 1 (for row 4) and going down to 3 (for row 6).

The number of star decrease with each row, starting from 5 star (for row 4) and going up to 1 star(for row 6).


First Inner Loop (Spaces):

The first inner loop prints the spaces before the number in each row.

The number of spaces in each row is equal to middle_row - row (middle_row= total_row/2)


Second Inner Loop (Stars):

The second inner loop prints the start in each row.

The number of stars in each row decreased by 2 for each row.

For the i-th row, the number of stars is 2 * row + 1



Video Solution


Java


public class Main {

    public static void main(String[] args) {

        int total_rows = 7;  // Total number of rows in the combined pattern

        int middle_row = total_rows / 2;  // Middle row index (which is 3 for 7 rows)

        

        // Top half (Pyramid)

        for (int row = 0; row <= middle_row; row++) {

            // First inner loop for spaces

            for (int space = 0; space < middle_row - row; space++) {

                System.out.print(" ");

            }

            // Second inner loop for stars

            for (int star = 0; star < 2 * row + 1; star++) {

                System.out.print("*");

            }

            System.out.println();  // Move to the next line

        }


        // Bottom half (Reverse Pyramid)

        for (int row = middle_row - 1; row >= 0; row--) {

            // First inner loop for spaces

            for (int space = 0; space < middle_row - row; space++) {

                System.out.print(" ");

            }

            // Second inner loop for stars

            for (int star = 0; star < 2 * row + 1; star++) {

                System.out.print("*");

            }

            System.out.println();  // Move to the next line

        }

    }

}




C++


#include <iostream>

using namespace std;


int main() {

    int total_rows = 7;  // Total number of rows in the combined pattern

    int middle_row = total_rows / 2;  // Middle row index (which is 3 for 7 rows)


    // Top half (Pyramid)

    for (int row = 0; row <= middle_row; row++) {

        // First inner loop for spaces

        for (int space = 0; space < middle_row - row; space++) {

            cout << " ";

        }

        // Second inner loop for stars

        for (int star = 0; star < 2 * row + 1; star++) {

            cout << "*";

        }

        cout << endl;  // Move to the next line

    }


    // Bottom half (Reverse Pyramid)

    for (int row = middle_row - 1; row >= 0; row--) {

        // First inner loop for spaces

        for (int space = 0; space < middle_row - row; space++) {

            cout << " ";

        }

        // Second inner loop for stars

        for (int star = 0; star < 2 * row + 1; star++) {

            cout << "*";

        }

        cout << endl;  // Move to the next line

    }


    return 0;

}



Python


total_rows = 7  # Total number of rows in the combined pattern

middle_row = total_rows // 2  # Middle row index (which is 3 for 7 rows)


# Top half (Pyramid)

for row in range(middle_row + 1):

    # First inner loop for spaces

    for space in range(middle_row - row):

        print(" ", end="")

    # Second inner loop for stars

    for star in range(2 * row + 1):

        print("*", end="")

    print()  # Move to the next line


# Bottom half (Reverse Pyramid)

for row in range(middle_row - 1, -1, -1):

    # First inner loop for spaces

    for space in range(middle_row - row):

        print(" ", end="")

    # Second inner loop for stars

    for star in range(2 * row + 1):

        print("*", end="")

    print()  # Move to the next line