Login Register Appointment

Diamond Shape

Lesson 21/21 | Study Time: 10 Min
Course: Pattern Program

Pattern:

*

**

***

****

*****

*****

****

***

**

*

 

Solution

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


Pattern Breakdown

Outer Loop (Rows):

The pattern has 9 rows in total.

The first half (rows 1 to 5) increases the number of stars, and the second half (rows 6 to 9) decreases the number of stars.


First Part (Increasing Stars):

The first 5 rows (1st to 5th) contain an increasing number of stars, starting from 1 star and increasing by 1 each time.


Second Part (Decreasing Stars):

The next 4 rows (6th to 9th) contain a decreasing number of stars, starting from 5 stars and reducing by 1 each time.



Video Solution


JAVA

public class Main {

    public static void main(String[] args) {

        int total_rows = 5;  // Half of the total rows (excluding the middle row)


        // First half (increasing stars)

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

            // Print stars

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

                System.out.print("*");

            }

            System.out.println();

        }


        // Second half (decreasing stars)

        for (int row = total_rows; row >= 1; row--) {

            // Print stars

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

                System.out.print("*");

            }

            System.out.println();

        }

    }

}


C++

#include <iostream>

using namespace std;


int main() {

    int total_rows = 5;  // Half of the total rows (excluding the middle row)


    // First half (increasing stars)

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

        // Print stars

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

            cout << "*";

        }

        cout << endl;

    }


    // Second half (decreasing stars)

    for (int row = total_rows; row >= 1; row--) {

        // Print stars

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

            cout << "*";

        }

        cout << endl;

    }


    return 0;

}



Python

total_rows = 5  # Half of the total rows (excluding the middle row)


# First half (increasing stars)

for row in range(total_rows):

    # Print stars

    for star in range(row + 1):

        print("*", end="")

    print()


# Second half (decreasing stars)

for row in range(total_rows, 0, -1):

    # Print stars

    for star in range(row):

        print("*", end="")

    print()