Monday, 17 August 2026

Star pattern in C

In C language, star patterns are a fun way to learn loops and nested loops. A c program for star pattern uses simple logic to print stars in different shapes like triangles, pyramids, or squares. These patterns help beginners understand how loops work and how to control output on the screen.

 

How to print star patterns in c language, we use two loops most of the times:

 

The outer loop controls the number of rows.

 

The inner loop controls how many stars to print in each row.

 

Hinglish

Coders' comments

Write star patterns sharp and bright, C loops make your code perfect and right!

 

Option 1: Right-Angled Triangle Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

   

    // n is number of rows

    int n = 5;

 

    // Loop for each row

    for(int i = 1; i <= n; i++) {

       

        // Print stars in a row

        for(int j = 1; j <= i; j++) {

            printf("*");

        }

       

        // Move to next row

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*

**

***

****

*****

 

Explanation

The above program sets n = 5, which means there will be 5 rows of stars.

 

The outer loop runs from i = 1 to 5, handling each row.

 

The inner loop runs from j = 1 to i, so row 1 prints 1 star, row 2 prints 2 stars, and so on.

 

After printing stars in one row, printf("\n") moves the cursor to the next line.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 2: Inverted Right-Angled Triangle Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    // Loop for each row in reverse

    for(i = n; i >= 1; i--) {

       

        // Print stars in a row

        for(j = 1; j <= i; j++) {

            printf("*");

        }

       

        // Move to next row

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*****

****

***

**

*

 

Explanation

The variable n = 5 sets the total rows of stars.

 

The outer loop runs from i = 5 down to 1, reducing stars each row.

 

The inner loop prints stars from j = 1 to i.

 

After printing stars, \n moves the cursor to the next line.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 3: Right-Aligned Triangle Star Pattern Program in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Outer loop is iterate over each row

    for(int i = 1; i <= n; i++) {

       

        // This inner loop prints spaces before stars

        for(int j = 1; j <= n - i; j++) {

            printf(" ");

        }

       

        // This inner loop prints Print stars

        for(int j = 1; j <= i; j++) {

            printf("*");

        }

       

        // Move to next row

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *

   **

  ***

 ****

*****

 

Explanation

The program sets n = 5, meaning the pattern will have 5 rows.

 

The outer loop runs from i = 1 to i = 5, controlling the row count.

 

The first inner loop prints spaces (n - i) so stars shift right, creating the triangle shape.

 

The second inner loop prints i stars, then \n moves to the next row, giving a left-aligned triangle pattern.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 4: Inverted Right-Aligned Triangle Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    for(i = n; i >= 1; i--) {

       

        // Print spaces before stars

        for(j = 1; j <= n - i; j++) {

            printf(" ");

        }

       

        // Print stars

        for(j = 1; j <= i; j++) {

            printf("*");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*****

 ****

  ***

   **

    *

 

Explanation

The program starts with n = 5, which decides the height of the star pattern.

 

The outer loop runs from i = 5 down to 1, controlling the number of stars in each row.

 

The first inner loop prints spaces using n - i, which shifts the stars to the right.

 

The second inner loop prints stars from 1 to i, creating a decreasing star pattern aligned to the right.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 5: Pyramid Star Pattern Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    // Outer loop iterate over each row

    for(i = 1; i <= n; i++) {

       

        // This inner loop prints spaces

        for(j = 1; j <= n - i; j++) {

            printf(" ");

        }

        // This inner loop prints stars in pyramid shape

        for(j = 1; j <= 2*i - 1; j++) {

            printf("*");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *

   ***

  *****

 *******

*********

 

Explanation

The variable n = 5 sets total rows for the pyramid pattern.

 

The first inner loop for(j = 1; j <= n - i; j++) prints spaces to shift stars to the center.

 

The second inner loop for(j = 1; j <= 2*i - 1; j++) prints stars, making the pyramid wider row by row.

 

After completing each row, printf("\n") moves to the next line to continue the pattern.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 6: Inverted Pyramid Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    // Outer loop iterate over each row

    for(i = n; i >= 1; i--) {

       

        // This inner loop prints leading spaces

        for(j = 1; j <= n - i; j++) {

            printf(" ");

        }

       

        // This inner loop prints stars

        for(j = 1; j <= 2*i - 1; j++) {

            printf("*");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*********

 *******

  *****

   ***

    *

 

Explanation

The program starts with n = 5 and uses two nested loops inside an outer loop to create a pattern.

 

The outer loop runs from i = 5 down to i = 1, controlling the number of rows.

 

The first inner loop prints spaces (n - i) times so that stars look centered.

 

The second inner loop prints stars (2*i - 1) times, creating an inverted pyramid shape row by row.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 7: Diamond Star Pattern in C Programming

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Below nested loop print upper part

    // of the pyramid

    for(int i = 1; i <= n; i++) {

       

        // This inner loop print leading spaces

        for(int j = 1; j <= n - i; j++) printf(" ");

       

        // This inner loop prints star (*)

        for(int j = 1; j <= 2*i - 1; j++) printf("*");

        printf("\n");

    }

 

    // Below nested loop print lower part

    // of the pyramid

    for(int i = n-1; i >= 1; i--) {

        for(int j = 1; j <= n - i; j++) printf(" ");

        for(int j = 1; j <= 2*i - 1; j++) printf("*");

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *

   ***

  *****

 *******

*********

 *******

  *****

   ***

    *

 

Explanation

The program uses a value n = 5 to control the height of the pyramid, which creates both the upper and lower parts of the diamond pattern.

 

The first outer loop (i = 1 to n) prints the upper half. Inside it, the first inner loop prints spaces (n - i) and the second inner loop prints stars (2*i - 1).

 

The second outer loop (i = n-1 to 1) prints the lower half in the same way, but in reverse order.

 

Together, these loops print a complete diamond shape with stars * and spaces arranged properly.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 8: Hollow Pyramid Star Pattern in C Programming

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    for(i = 1; i <= n; i++) {

       

        // Print leading spaces

        for(j = 1; j <= n - i; j++) printf(" ");

        for(j = 1; j <= 2*i - 1; j++) {

           

            // Print start only border of the pyramid

            if(j == 1 || j == 2*i - 1 || i == n) printf("*");

            

            // Print spaces inside triangle

            else printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *

   * *

  *   *

 *     *

*********

 

Explanation

The program sets n = 5, so the pyramid will have 5 rows.

 

For each row i, it first prints spaces using for(j = 1; j <= n - i; j++), which helps in aligning the pyramid shape.

 

The second loop for(j = 1; j <= 2*i - 1; j++) decides what to print: a * at the first (j == 1), last (j == 2*i - 1), or when i == n.

 

For other positions, it prints a space, which creates the hollow pyramid pattern.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 9: Hollow Right-Angled Triangle Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    for(int i = 1; i <= n; i++) {

        for(int j = 1; j <= i; j++) {

           

            // Print star(*) only border of the triangle

            if(j == 1 || j == i || i == n) printf("*");

           

            // Print space inside triangle

            else printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*

**

* *

*  *

*****

 

Explanation

The program sets n = 5, so it will create a triangle of 5 rows.

 

For each row i, another loop runs from j = 1 to j = i to decide what to print.

 

A star * is printed when j == 1, j == i, or i == n, which forms the triangle border.

 

In all other cases, a space " " is printed, giving the triangle a hollow shape.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 10: Inverted Hollow Right-Angled Triangle Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    for(i = n; i >= 1; i--) {

        for(j = 1; j <= i; j++) {

           

            // Print star on border

            if(j == 1 || j == i || i == n) printf("*");

           

            // Space inside triangle

            else printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*****

*  *

* *

**

*

 

Explanation

The program prints a hollow right-angled triangle using * symbols with n = 5 rows.

 

Outer for loop (i) controls rows from 5 down to 1, inner loop (j) prints columns for each row.

 

if condition prints * on the first, last, or top row; else it prints space ' ' for hollow effect.

 

Each row ends with \n to move to the next line, creating a triangle shape on the screen.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 11: Hourglass Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int i, j, n = 5;

 

    // Below nested loops print upperr

    // part the hour glass

    for(i = n; i >= 1; i--) {

       

        // This inner loop prints leading spaces

        for(j = 1; j <= n - i; j++) printf(" ");

       

        // This inner loop prints stars

        for(j = 1; j <= 2*i - 1; j++) printf("*");

        printf("\n");

    }

 

    // Below nested loops print lower

    // part the hour glass

    for(i = 2; i <= n; i++) {

        for(j = 1; j <= n - i; j++) printf(" ");

        for(j = 1; j <= 2*i - 1; j++) printf("*");

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*********

 *******

  *****

   ***

    *

   ***

  *****

 *******

*********

 

Explanation

The program uses nested loops to print an hourglass star pattern for n = 5.

 

The first for loop (i = n; i >= 1; i--) creates the upper part by printing spaces (n - i) and stars (2*i - 1).

 

The second for loop (i = 2; i <= n; i++) creates the lower part using the same logic but in reverse order.

 

Together, both parts display a symmetric hourglass shape made of * symbols.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 12: Hollow Inverted Pyramid Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Outer loop goes from n down to 1 for inverted pyramid

    for (int i = n; i >= 1; i--) {

 

        // leading spaces to shift each lower row right

        for (int j = 1; j <= n - i; j++)

            printf(" ");

 

        // print 2*i-1 positions: either star (border/top) or space

        for (int k = 1; k <= 2 * i - 1; k++) {

           

            // Print star only border of the pyramid

            if (i == n || k == 1 || k == 2 * i - 1 || i == 1)

                printf("*");

           

            // Print spaces inside the pyramid

            else

                printf(" ");

        }

        printf("\n");

    }

    return 0;

}

Run Code

 

Output:

*********

 *     *

  *   *

   * *

    *

 

Explanation

The program sets n = 5 and uses nested loops to print an inverted hollow pyramid pattern made of *.

 

The outer loop runs from i = n down to 1, controlling the number of rows in the pyramid.

 

The first inner loop prints spaces (" ") before each row to shift the stars toward the right.

 

The second inner loop prints * only at the borders (k == 1, k == 2*i-1, i == n, or i == 1) and spaces in between to keep it hollow.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 13: Hollow Diamond Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

   

    // Below nested loops prints upper part

    // of the pyramid

    for(int i = 1; i <= n; i++) {

       

        // Leading spaces

        for(int j = 1; j <= n-i; j++) printf(" ");

        for(int k = 1; k <= 2*i-1; k++) {

           

            // Print star only border of the pyramid

            if(k==1 || k==2*i-1) printf("*");

           

            // Print space inside pyramid

            else printf(" ");

        }

        printf("\n");

    }

   

    // Below nested loops prints lower part

    // of the pyramid

    for(int i = n-1; i >= 1; i--) {

        for(int j = 1; j <= n-i; j++) printf(" ");

        for(int k = 1; k <= 2*i-1; k++) {

            if(k==1 || k==2*i-1) printf("*");

            else printf(" ");

        }

        printf("\n");

    }

    return 0;

}

Run Code

 

Output:

    *

   * *

  *   *

 *     *

*       *

 *     *

  *   *

   * *

    *

 

Explanation

The program prints a hollow diamond star pattern using nested loops with size n = 5.

 

The first outer loop (i = 1 to n) prints the upper part of the diamond, using spaces and stars in a pyramid shape.

 

The second outer loop (i = n-1 to 1) prints the lower part, mirroring the upper pattern.

 

Inside both parts, stars (*) are printed only at the border positions (k == 1 or k == 2*i - 1), while spaces fill the inside.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 14: Solid Square Star Pattern in C Programming

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

   

    // Outer loop iterate over each row

    for(int i = 1; i <= n; i++) {

       

        // Inner loop print star border of the square

        for(int j = 1; j <= n; j++) {

            if(i==1 || i==n || j==1 || j==n) printf("* ");

            else printf("  ");

        }

        printf("\n");

    }

    return 0;

}

Run Code

 

Output:

* * * * *

* * * * *

* * * * *

* * * * *

* * * * *

 

Explanation

The variable n is set to 5, which means the pattern will have 5 rows and 5 columns.

 

The outer loop for(int i = 1; i <= n; i++) runs 5 times, one for each row.

 

The inner loop for(int j = 1; j <= n; j++) prints * followed by a space in every row.

 

After printing stars for one row, printf("\n") moves the cursor to the next line to continue the pattern.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 15: Hollow Square Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

   

    // Outer loop iterate over each row

    for(int i = 1; i <= n; i++) {

       

        // Inner loop print star in each row

        for(int j = 1; j <= n; j++) {

            printf("* ");

        }

       

        // Move to next row

        printf("\n");

    }

    return 0;

}

Run Code

 

Output:

* * * * *

*       *

*       *

*       *

* * * * *

 

Explanation

The program uses variable n = 5 to define the size of a square pattern.

 

The outer loop for(int i = 1; i <= n; i++) runs for each row of the square.

 

The inner loop for(int j = 1; j <= n; j++) prints stars * at the borders: first row, last row, first column, and last column.

 

Spaces " " are printed inside the square, creating a hollow square pattern of size 5×5.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 16: Hollow Hourglass Star Pattern in C Programming

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Upper inverted hollow pyramid

    for (int i = n; i >= 1; i--) {

        for (int j = n; j > i; j--) {

           

            // spaces before stars

            printf(" ");

        }

 

        for (int k = 1; k <= (2 * i - 1); k++) {

           

            // Print only border stars

            if (k == 1 || k == (2 * i - 1) || i == n)

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    // Lower hollow pyramid

    for (int i = 2; i <= n; i++) {

        for (int j = i; j < n; j++) {

           

            // Spaces before stars

            printf(" ");

        }

 

        for (int k = 1; k <= (2 * i - 1); k++) {

           

            // Print only border stars

            if (k == 1 || k == (2 * i - 1) || i == n)

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*********

 *     *

  *   *

   * *

    *

   * *

  *   *

 *     *

*********

 

Explanation

The program prints a hollow diamond shape using stars (*) with n = 5 as the size.

 

The first for loop creates the upper inverted hollow pyramid, adding spaces before the stars for alignment.

 

The second for loop prints the lower hollow pyramid, leaving spaces inside and showing only border stars.

 

The program uses conditions k == 1, k == (2*i-1), or i == n to print stars at the edges, keeping the inside hollow.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 17: Butterfly Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Upper part of butterfly

    for (int i = 1; i <= n; i++) {

        // Left wing

        for (int j = 1; j <= i; j++)

            printf("*");

 

        // Space in middle of the wings

        for (int j = 1; j <= 2 * (n - i); j++)

            printf(" ");

 

        // Right wing

        for (int j = 1; j <= i; j++)

            printf("*");

 

        printf("\n");

    }

 

    // Lower part of butterfly

    for (int i = n; i >= 1; i--) {

       

        // Left wing

        for (int j = 1; j <= i; j++)

            printf("*");

 

        // Space in middle of the wings

        for (int j = 1; j <= 2 * (n - i); j++)

            printf(" ");

 

        // Right wing

        for (int j = 1; j <= i; j++)

            printf("*");

 

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*        *

**      **

***    ***

****  ****

**********

**********

****  ****

***    ***

**      **

*        *

 

Explanation

The program prints a butterfly star pattern using loops where the size of the pattern is decided by the variable n = 5.

 

The first loop prints the upper wings by increasing stars (*) on both sides and decreasing spaces in the middle.

 

The second loop prints the lower wings by reversing the process, decreasing stars and increasing spaces.

 

Together, both loops form a symmetric butterfly shape made of stars on the console.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 18: Plus Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= n; j++) {

           

            // Print star for center row or

            // center column

            if (i == (n + 1) / 2 || j == (n + 1) / 2)

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

  * 

  * 

*****

  * 

  *

 

Explanation

The program uses nested loops to print a pattern inside a 5×5 grid.

 

The outer loop (i) runs from 1 to 5 for rows, and the inner loop (j) runs from 1 to 5 for columns.

 

It prints * when i or j is equal to (n + 1) / 2, which is the middle (3rd) row or column.

 

The final output forms a plus (+) shaped star pattern.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 19: X Star Pattern in C Programming

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= n; j++) {

           

            // Star when row = col or col = n - row + 1

            if (j == i || j == (n - i + 1))

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*   *

 * *

  * 

 * *

*   *

 

Explanation

The program prints a cross (“X”) pattern made of * for n = 5.

 

Two nested loops run from 1 to n to control rows and columns.

 

Inside the inner loop, a * is printed when j == i or j == (n - i + 1), meaning diagonal positions.

 

For all other positions, a blank space is printed, forming the cross shape on the screen.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 20: K Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 7;

 

    for (int i = 0; i < n; i++) {

       

        // Verticle line of K

        printf("*");

 

        for (int j = 0; j < n; j++) {

           

            // Upper half diagonal

            if (i <= n / 2 && j == (n / 2) - i)

                printf("*");

               

            // Lower half diagonal

            else if (i > n / 2 && j == i - (n / 2))

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*   *  

*  *   

* *    

**     

* *    

*  *   

*   *

 

Explanation

The variable n = 7 sets the size of the pattern and the outer loop runs n times to print each row of the letter ‘K’.

 

Inside the loop, the first printf("*") prints the vertical line of ‘K’ at the beginning of every row.

 

The upper diagonal line of ‘K’ is printed when i <= n/2 and j == (n/2) - i.

 

The lower diagonal line is printed when i > n/2 and j == i - (n/2), forming the complete letter ‘K’.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 21: A Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 7;

   

    // Middle row for horizontal bar

    int mid = n / 2;

 

    for (int i = 0; i < n; i++) {

        for (int j = 0; j <= n; j++) {

           

            // Upper slanting sides

            if (j == (n / 2 - i) || j == (n / 2 + i))

                printf("*");

               

            // Horizontal bar in the middle

            else if (i == mid && j > (n / 2 - i) && j < (n / 2 + i))

                printf("*");

               

            // Vertical legs below the middle

            else if (i > mid && (j == 0 || j == n))

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

   *   

  * *  

 *   * 

*******

*      *

*      *

*      *

 

Explanation

The program prints a pattern of a star-shaped figure with n = 7 rows and n + 1 columns.

 

For the first half, it prints upper slanting sides using positions (n/2 - i) and (n/2 + i).

 

The middle row (i = mid) prints a horizontal bar of stars between the slants.

 

For rows below the middle, it prints vertical legs at the first (j = 0) and last (j = n) columns to complete the shape.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 22: Z Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 7;

 

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++) {

           

            // Print star only first and last row with second diagonal

            if (i == 0 || i == n - 1 || j == n - i - 1)

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*******

     *

    * 

   *  

  *   

 *    

*******

 

Explanation

The program prints a pattern of stars (*) in a square of size n = 7.

 

It uses two nested loops: i for rows and j for columns.

 

Stars are printed in the first row (i == 0), last row (i == n-1), and along the second diagonal (j == n-i-1).

 

All other positions are filled with spaces, creating a hollow square with a diagonal line from top-right to bottom-left.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 23: Rhombus Star Pattern in C Programming

Code:

#include <stdio.h>

#include <stdio.h>

 

int main() {

    int n = 5;

 

    for (int i = 1; i <= n; i++) {

       

        // Print spaces to shift stars

        for (int j = 1; j <= n - i; j++)

            printf(" ");

 

        // Print stars after leading spaces

        for (int j = 1; j <= n; j++)

            printf("*");

 

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *****

   *****

  *****

 *****

*****

 

Explanation

The program prints a pattern of stars in 5 rows, with each row having 5 stars.

 

It uses a nested loop: the outer loop (i = 1 to n) controls the number of rows.

 

The first inner loop prints spaces (n - i) before stars to shift them to the right.

 

The second inner loop prints 5 stars (*) in each row, and then moves to the next line using \n.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 24: Hollow Rhombus Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    for (int i = 1; i <= n; i++) {

       

        // Print leading spaces

        for (int j = 1; j <= n - i; j++)

            printf(" ");

 

        // Print stars only border of the rhombus

        for (int j = 1; j <= n; j++) {

            if (i == 1 || i == n || j == 1 || j == n)

                printf("*");

            else

                printf(" ");

        }

 

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *****

   *   *

  *   *

 *   *

*****

 

Explanation

The program prints a hollow rhombus of size n = 5 using stars (*).

 

The outer for loop with i controls the rows from 1 to n.

 

The first inner loop prints spaces before the stars to shape the rhombus.

 

The second inner loop prints stars at the border positions (i == 1 || i == n || j == 1 || j == n) and spaces inside, forming a hollow rhombus.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 25: Right Arrow Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Total height of the arrow is 2*n - 1

    int totalRows = 2 * n - 1;

 

    for (int i = 1; i <= totalRows; i++) {

        int stars, spaces;

 

        if (i <= n) {

           

            // Upper part of the arrow

            stars = n - i + 1;

            spaces = 2 * (i - 1);

        } else {

           

            // Lower part of the arrow

            int k = i - n;

            stars = k + 1;

            spaces = 2 * (n - k - 1);

        }

 

        // Print spaces

        for (int j = 1; j <= spaces; j++) {

            printf(" ");

        }

 

        // Print stars

        for (int j = 1; j <= stars; j++) {

            printf("*");

        }

 

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*****

  ****

    ***

      **

        *

      **

    ***

  ****

*****

 

Explanation

The program prints a hollow arrow pattern with n = 5, making the total rows 2*n - 1 = 9.

 

In the first n rows, it calculates stars as n - i + 1 and spaces as 2*(i - 1) to form the upper arrow.

 

In the remaining rows, stars become k + 1 and spaces 2*(n - k - 1) for the lower part, where k = i - n.

 

Nested loops first print spaces and then stars in each row, creating the complete arrow shape on the console.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 26: Left Arrow Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int rows = 5;

 

    // Construct the top half of the arrow

    for (int line = 0; line < rows - 1; line++) {

        int spaces = rows - line - 1;

        int stars = line + 1;

 

        while (spaces--) {

            printf(" ");

        }

 

        for (int k = 0; k < rows - line; k++) {

            printf("*");

        }

 

        printf("\n");

    }

 

    // Construct the bottom half of the arrow

    for (int line = 0; line < rows; line++) {

        for (int s = 0; s < line; s++) {

            printf(" ");

        }

 

        for (int a = 0; a <= line; a++) {

            printf("*");

        }

 

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *****

   ****

  ***

 **

*

 **

  ***

   ****

    *****

 

Explanation

The program prints an arrow pattern using rows = 5 as the size.

 

The first for loop builds the top half by printing spaces (spaces = rows - line - 1) and stars (stars = rows - line).

 

The second for loop builds the bottom half by increasing spaces (s = 0 to line-1) and stars (a = 0 to line).

 

Each line ends with \n to move to the next line, forming a complete arrow shape on the console.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 27: Hollow Butterfly Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Upper half of butterfly

    for (int i = 1; i <= n; i++) {

       

        // Left wing (each "cell" prints 2 chars)

        for (int j = 1; j <= i; j++) {

            if (i == n || j == 1 || j == i) printf("* ");

            else printf("  ");

        }

 

        // Middle gap: scale to cell width (here 4*(n-i) spaces)

        for (int j = 1; j <= 4 * (n - i); j++) printf(" ");

 

        // Right wing

        for (int j = 1; j <= i; j++) {

            if (i == n || j == 1 || j == i) printf("* ");

            else printf("  ");

        }

 

        printf("\n");

    }

 

    // Lower half of butterfly

    for (int i = n - 1; i >= 1; i--) {

        for (int j = 1; j <= i; j++) {

            if (j == 1 || j == i) printf("* ");

            else printf("  ");

        }

 

        for (int j = 1; j <= 4 * (n - i); j++) printf(" ");

 

        for (int j = 1; j <= i; j++) {

            if (j == 1 || j == i) printf("* ");

            else printf("  ");

        }

 

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*                 *

* *             * *

*   *         *   *

*     *     *     *

* * * * * * * * * *

*     *     *     *

*   *         *   *

* *             * *

*                 *

 

Explanation

This program prints a hollow butterfly star pattern using loops. The pattern height is controlled by n = 5.

 

The upper half is made using nested loops where stars (*) print at positions j == 1, j == i, or i == n, and spaces fill the rest.

 

The middle gap is created with 4 * (n - i) spaces to maintain symmetry between the left and right wings.

 

The lower half mirrors the upper part in reverse order, forming a complete hollow butterfly shape.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 28: Heart Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 6;

 

    // Upper part of heart

    for (int i = n / 2; i <= n; i += 2) {

       

        // Print leading spaces

        for (int j = 1; j < n - i; j += 2)

            printf(" ");

        for (int j = 1; j <= i; j++)

            printf("*");

           

        // Print spaces inside between stars

        for (int j = 1; j <= n - i; j++)

            printf(" ");

        for (int j = 1; j <= i; j++)

            printf("*");

        printf("\n");

    }

 

    // Lower part of heart

    for (int i = n; i >= 1; i--) {

       

        // Print leading spaces of the lower part

        for (int j = i; j < n; j++)

            printf(" ");

           

        // Print stars

        for (int j = 1; j <= (i * 2) - 1; j++)

            printf("*");

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

 ***   ***

***** *****

***********

 *********

  *******

   *****

    ***

     *

 

Explanation

The program prints a heart shape pattern using stars (*) by dividing it into two main parts, the upper and lower portions.

 

In the upper part, loops use values from n/2 to n with steps of 2 to create two curved lobes of the heart using spaces and stars.

 

The lower part runs from n to 1 to print the tapering bottom of the heart, reducing stars line by line.

 

Here n = 6, so the pattern height and width adjust accordingly to make a balanced heart shape.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 29: Square Diagonal Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 7;

 

    // Loop through each row

    for (int i = 1; i <= n; i++) {

       

        // Loop through each column

        for (int j = 1; j <= n; j++) {

           

            // Check border and diagonals

            if (i == 1 || i == n || j == 1 || j == n ||

                              i == j || j == n - i + 1)

                              

                // Print star if condition is true

                printf("*");

               

            // Print space otherwise

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

*******

**   **

* * * *

*  *  *

* * * *

**   **

*******

 

Explanation

The program defines an integer n = 7, which represents both the number of rows and columns for the star pattern.

 

Two for loops are used, the outer loop controls rows (i), and the inner loop controls columns (j).

 

Inside the loop, stars (*) are printed for the border (i==1, i==n, j==1, j==n) and both diagonals (i==j, j==n-i+1).

 

If none of these conditions are true, a space is printed, forming a hollow square with an “X” inside.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 30: Zigzag Star Pattern in C Language

Code:

#include <stdio.h>

 

int main() {

    int n = 16;

 

    // Loop through 3 rows

    for (int i = 1; i <= 3; i++) {

       

        // Loop through each column

        for (int j = 1; j <= n; j++) {

           

            // Check zigzag star placement condition

            if ((i + j) % 4 == 0 || (i == 2 && j % 4 == 0))

           

                // Print star if condition is true

                printf("*");

            else

               

                // Print space otherwise

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

 *   *   *   *

 * * * * * * * *

 *   *   *   *

 

Explanation

The program prints a zigzag star pattern with 3 rows and 16 columns.

 

The outer loop runs 3 times for rows, while the inner loop runs 16 times for columns.

 

A star '*' is printed when the condition (i + j) % 4 == 0 or (i == 2 && j % 4 == 0) is true; otherwise, a space is printed.

 

This logic creates a diagonal zigzag shape made up of stars * across the pattern.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 31: Pyramid with Cross Line Star Pattern in C Programming

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Middle column for cross

    int mid = n;

 

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= 2 * n - 1; j++) {

           

            // Pyramid border or vertical center line

            if (j == n - i + 1 || j == n + i - 1 || j == mid)

                printf("*");

            else {

                if(i == n){

                    printf("*");

                }else{

                    printf(" ");

                }

            }

        }

        printf("\n");

    }

    return 0;

}

Run Code

 

Output:

    *   

   ***  

  * * * 

 *  *  *

*********

 

Explanation

The program sets n = 5, which controls the height and width of the pattern. It also defines mid = n to mark the center column for the cross.

 

The outer loop runs from 1 to n to print each row of the pattern.

 

Inside, the inner loop prints * when the position is on the left border, right border, or middle line (j == mid).

 

On the last row (i == n), all positions print *, forming a base and creating a pyramid with a vertical cross.

 

 

Time Complexity Analysis

Time Complexity : O(n²)

 

 

Space Complexity Analysis

Space Complexity : O(1)

 

Option 32: Left Wing Butterfly Star Pattern in C

Code:

#include <stdio.h>

 

int main() {

    int n = 5;

 

    // Upper half

    for (int i = 1; i <= n; i++) {

        for (int j = 1; j <= i; j++) {

           

            // Print border only

            if (j == 1 || j == i)

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    // Lower half

    for (int i = n - 1; i >= 1; i--) {

        for (int j = 1; j <= i; j++) {

            if (j == 1 || j == i)

                printf("*");

            else

                printf(" ");

        }

        printf("\n");

    }

 

    return 0;

}

Run Code

 

Output:

    *   

   ***  

  * * * 

 *  *  *

*********

 

Explanation

The program prints a hollow diamond shape using stars (*) and spaces for n = 5.

 

The first for loop (lines 5–14) creates the upper half, printing stars only at the first and last positions of each row.

 

The second for loop (lines 17–26) creates the lower half, reducing the number of stars in each row similarly to form a mirrored pattern.

 

The program uses simple nested loops and if conditions to control the border stars while leaving the inside empty.

No comments:

Post a Comment