Hourglass Pattern

Hourglass Pattern

Medium C Pattern Printing 41 views
Explanation Complexity

Problem Statement

*********
 *******
  *****
   ***
    *
   ***
  *****
 *******
*********
Inverted triangle, then normal triangle. Coordination of spaces and stars

Input Format

An integer n representing the number of rows (odd number).

Output Format

Star pattern with an inverted triangle on top and a normal triangle below.

Constraints

• n must be odd

• Use nested loops only

Concept Explanation

The pattern is formed by controlling spaces and stars row-wise:
first decreasing stars (inverted triangle), then increasing stars (normal triangle).

Step-by-Step Explanation

Upper Inverted Triangle

1.Set mid = n / 2.

2.Loop rows i from 0 to mid.

3.Print i leading spaces.

4.Print n - 2*i stars.

5.Move to the next line.

Lower Normal Triangle

1.Loop rows i from mid - 1 down to 0.

2.Print i leading spaces.

3.Print n - 2*i stars.

4.Move to the next line.

Concept Explanation

The pattern is formed by controlling spaces and stars row-wise:
first decreasing stars (inverted triangle), then increasing stars (normal triangle).

Step-by-Step Explanation

Upper Inverted Triangle

1.Set mid = n / 2.

2.Loop rows i from 0 to mid.

3.Print i leading spaces.

4.Print n - 2*i stars.

5.Move to the next line.

Lower Normal Triangle

1.Loop rows i from mid - 1 down to 0.

2.Print i leading spaces.

3.Print n - 2*i stars.

4.Move to the next line.

Input / Output Format

Input Format
An integer n representing the number of rows (odd number).
Output Format
Star pattern with an inverted triangle on top and a normal triangle below.
Constraints
• n must be odd

• Use nested loops only

Examples

Input:
Output:
********* ******* ***** *** * *** ***** ******* *********

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int i, j;
    int n = 5; // Height of the top inverted triangle

    // Top inverted triangle
    for (i = n; i >= 1; i--) {
        // Print spaces
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        // Print stars
        for (j = 1; j <= 2*i - 1; j++) {
            printf("*");
        }
        printf("n");
    }

    // Bottom normal triangle
    for (i = 2; i <= n; i++) {
        // Print spaces
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        // Print stars
        for (j = 1; j <= 2*i - 1; j++) {
            printf("*");
        }
        printf("n");
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int i, j;
    int n = 5; // Height of the top inverted triangle

    // Top inverted triangle
    for (i = n; i >= 1; i--) {
        // Print spaces
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        // Print stars
        for (j = 1; j <= 2*i - 1; j++) {
            printf("*");
        }
        printf("n");
    }

    // Bottom normal triangle
    for (i = 2; i <= n; i++) {
        // Print spaces
        for (j = 1; j <= n - i; j++) {
            printf(" ");
        }
        // Print stars
        for (j = 1; j <= 2*i - 1; j++) {
            printf("*");
        }
        printf("n");
    }

    return 0;
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.