Star Pattern Programs in C and Python with Output

This star pattern often makes coding fun and also helps us learn loops smoothly. Stars (*) are mostly used to create cool shapes, improve your logic, as well as problem-solving skills. They often start simple, and you can make them as creative as possible with practice.

Whether you are just starting or already know coding, know how these star pattern programs are a fun way to practice and sharpen your skills. Due to this, learning loops are made more enjoyable and fun.

Blogging Illustration

Star Pattern Programs in C and Python with Output

image

Programs :

  1. Simple right-angled triangle star pattern in C++
  2. Program Code

                        #include
                    using namespace std;
    
                    int main() {
                        int rows;
                        cout << "Rows for pattern: "; // Taking the input from user
                        cin >> rows;
    
                        for (int i = 1; i <= 5 rows; i++) { loop for rows (int j="1;" <="i;" j++) stars cout << "*"; } endl; move to the next line return 0; output : pattern: * ** *** **** ***** pre>
                            

    === Code Execution Successful ===

    Explanation :

    • A number is taken from the user to decide how many rows this pattern will have.
    • A loop is used to go through each row, and then another loop inside it would be required to print *.
    • The number of * in each row will increase as we go to the next row. After printing this * for a row so we will go to the next line to create a pattern.
  3. Inverted right-angled triangle* pattern
  4. Program Code

                            #include
                        using namespace std;
    
                        int main() {
                            int rows;
                            cout << "Rows for pattern: "; // Taking the input from user 
                            cin >> rows;
    
                            for (int i = rows; i >= 1; i--) { // Loop for rows (decreasing)
                                for (int j = 1; j <= 6 i; j++) { loop for stars cout << "*"; } endl; return 0; output : rows pattern: ****** ***** **** *** ** * < pre>
                            

    === Code Execution Successful ===

    Explanation :

    • A number is taken from the user to decide how many rows the pattern will have.
    • A first loop is used to go through each of the rows, and then another loop is simply used to print *. This will ultimately decrease self * in each row, particularly.
    • So after printing this * for a part row, we move to the next line.
  5. Pyramid star pattern in C++
  6. Program Code

                            #include
                                using namespace std;
    
                                int main() {
                                    int rows;
                                    cout << "Rows for pattern: ";
                                    cin >> rows;
    
                                    for (int i = 1; i <= 5 rows; i++) { for (int j="1;" <="rows" - i; j++) printing spaces cout << " "; * i 1; stars "*"; endl; } return 0; output : rows pattern: *** ***** ******* ********* pre>
                            

    === Code Execution Successful ===

    Explanation :

    • A number is taken from the user to decide how many rows this pattern will have.
    • A loop will create each row that will first align spaces to add * in this pyramid shape.
    • So the number of * will increase in each row, which will form a * pattern in C++ that will look like a pyramid.
  7. Diamond star pattern in C++
  8. Program Code

                           #include 
                                    using namespace std;
    
                                    int main() {
                                        int rows;
                                        cout << "Rows for pattern: "; // Taking the input form user 
                                        cin >> rows;
    
                                        // Upper part of the diamond
                                        for (int i = 1; i <= rows; i++) { for (int j="1;" <="rows" - i; j++) cout << " "; * i 1; "*"; endl; } lower part of the diamond>= 1; i--) {
                                            for (int j = 1; j <= 5 rows - i; j++) cout << " "; for (int j="1;" <="2" * i 1; "*"; endl; } return 0; output : pattern: *** ***** ******* ********* pre>
                            

    === Code Execution Successful ===

    Explanation :

    • First, we take the number of rows as input, so the user decides the size of the pattern.
    • Loops are used to print es as well as *. So the upper part of the diamond is created, and then the lower part.
    • These stars will increase in the upper part and decrease in the lower part. So, a diamond-shaped star pattern will be formed ultimately.
  9. Butterfly star pattern in C++
  10. Program Code

                           #include 
                        using namespace std;
    
                        int main() {
                            int rows;
                            cout << "Rows for pattern: ";
                            cin >> rows;
    
                            // Upper half of the butterfly
                            for (int i = 1; i <= rows; i++) { for (int j="1;" <="i;" j++) cout << "*"; * (rows - i); spaces in middle " "; right stars endl; } lower half of the butterfly i="rows;">= 1; i--) {
                                for (int j = 1; j <= 6 i; j++) cout << "*"; for (int j="1;" <="2" * (rows - i); " "; endl; } return 0; output.: rows pattern: ** *** **** ***** ************ pre>
                            

    === Code Execution Successful ===

    Explanation :

    • The number of rows is taken as input from the user to determine the size of the pattern.
    • Two halves are there, upper and lower, giving mirror images.
    • Loops are used to print * on both sides. Spaces are there in the middle to create a butterfly shape.
    • So this will form a * pattern in C++ where stars will expand outwards and then also share symmetrically.
Star pattern in Python:

Below is a comprehensive list of 5 programs that will cover all beginner, intermediate, as well advanced levels. So be ready to grasp. Important learnings from them.

  1. 1-Left-aligned right-angled triangle
  2. Program Code

                          # Left-aligned right-angled triangle
                        rows = 6
                        for i in range(1, rows + 1):
                            for j in range(1, i + 1):
                                print('*', end=' ')
                            print()
    
                        Output : 
    
                                * 
                                * * 
                                * * * 
                                * * * * 
                                * * * * * 
                                * * * * * *
    
                                

    Explanation :

    • Outer loop, which I will run from 1 to the row (6).
    • The inner loop j that is present will run from 1 to I.
    • So for each line, I stars will be printed.
    • The print () present at the end will simply move to the next line.
  3. 2- Center-aligned pyramid
  4. Program Code

                         rows = 5
                    for i in range(1, rows + 1):
                        for j in range(rows - i):
                            print(' ', end=' ')
                        for k in range(2 * i - 1):
                            print('*', end=' ')
                        print()
    
                    Output : 
    
                              * 
                            * * * 
                          * * * * * 
                        * * * * * * * 
                      * * * * * * * * *
                                

    Explanation :

    • The outer loop will simply go from 1 to 5.
    • The first inner loop will print spaces that will center-align the stars.
    • The second inner loop will simply print stars that too, in an increasing odd number pattern 1,3,5,..... so on.
    • The total stars present in per row will be equal to 2*i-1, that too for a pyramid shape.
  5. 3-Inverted Pyramid
  6. Program Code

                        rows = 5
                    for i in range(rows, 0, -1):
                        for j in range(rows - i):
                            print(' ', end=' ')
                        for k in range(2 * i - 1):
                            print('*', end=' ')
                        print()
    
                    Output : 
    
                      * * * * * * * * * 
                        * * * * * * * 
                          * * * * * 
                            * * * 
                              *
                                

    Explanation :

    • The outer loop will run in reverse order from 5 to 1.
    • The first inner loop will print the spaces that will. Align the stars downward.
    • The second inner loop will print decreasing odd numbers of stars: 9,7,5.
    • Total. Stads that are present per row = 2*i-1.
  7. 4-Hollow Diamond
  8. Program Code

                    rows = 5
    
                    # Upper half
                    for i in range(1, rows + 1):
                        for j in range(rows - i):
                            print(" ", end="")
                        for k in range(2 * i - 1):
                            if k == 0 or k == 2 * i - 2:
                                print("*", end="")
                            Else:
                                print(" ", end="")
                        print()
    
                    # Lower half
                    for i in range(rows - 1, 0, -1):
                        for j in range(rows - i):
                            print(" ", end="")
                        for k in range(2 * i - 1):
                            if k == 0 or k == 2 * i - 2:
                                print("*", end="")
                            Else:
                                print(" ", end="")
                        print()
    
                                    Output : 
                                                   *
                                                  * *
                                                 *   *
                                                *     *
                                               *       *
                                                *     *
                                                 *   *
                                                  * *
                                                   *
                                

    Explanation :

    • The upper and lower parts of the diamond are combined here.
    • Conditions like k==0 or k=2*i-2 that will print stars at the boundary of each row.
    • Inner space will just create the hollow effect.
    • Rows as well as spaces are calculated to align all the stars centrally.
  9. 5-Butterfly pattern
  10. Program Code

                    # Butterfly pattern
                    n = 5
    
                    # Upper half
                    for i in range(1, n + 1):
                        print('*' * i + ' ' * (2 * (n - i)) + '*' * i)
    
                    # Lower half
                    for i in range(n, 0, -1):
                        print('*' * i + ' ' * (2 * (n - i)) + '*' * i)
    
                    Output : 
                                *        *
                                **      **
                                ***    ***
                                ****  ****
                                **********
                                ****  ****
                                ***    ***
                                **      **
                                *        *
                                

    Explanation :

    • This pattern is symmetrical just about the middle part.
    • In this upper half :
      • Left wing - I stars are there.
      • Middle gaps - 2*n-1 spaces are there over here.
      • Right wing - I stars.
    • So the lower half will just mirror the image of the upper half by reversing it.
    • Butterfly shape will be created where wings will expand as well s contract.

Conclusion:

As we have seen here, C++ as well as Python have their own set of merits and demerits. So one should choose them according to their needs, requirements, and skillset. Budget should also be taken into account while you choose a particular program. But learning this never goes to waste, so just enroll for a C programming course in Noida and also a Python programming course in Noida at one of the institutions, and let your career leap to the next level. Make a wise choice and build your career in the right direction. Feel free to contact us or visit our website for more courses or details. All trending courses are available as per industry demands to suit your needs and help you land your dream job. You can also enroll in other courses of your choice and perform well. The right career is important, along with your interest, so look for both. Choose a job of your dreams and make your step count in this tech-savvy world. Be wise and choose what you need as per industry demands.

Placed Students

Our Clients

Partners

Uncodemy Learning Platform

Uncodemy Free Premium Features

Popular Courses