Heart Shape

Heart Shape

Hard C Pattern Printing 45 views
Explanation Complexity

Problem Statement

 ***   ***
 ***** *****
***********
 *********
  *******
   *****
    ***
     *
Mathematical pattern - calculate specific coordinates where stars are printed.

Input Format

No input required.
Fixed number of rows and columns.

Output Format

Star pattern printed using mathematical conditions on row and column indices.

Constraints

• Fixed rows (7 rows)

• Use nested loops

• Stars printed only at specific coordinates

Concept Explanation

Stars are printed based on calculated positions using row and column indices instead of counting stars directly.

Step-by-Step Explanation

1.Consider total rows = n (here n = 7).

2.Use an outer loop for rows i from 0 to n-1.

3.Use an inner loop for columns j from 0 to 2*n.

4.For the upper part (first 3 rows):

• Print * if:

• j >= (n-1)-i and j = i and j

Concept Explanation

Stars are printed based on calculated positions using row and column indices instead of counting stars directly.

Step-by-Step Explanation

1.Consider total rows = n (here n = 7).

2.Use an outer loop for rows i from 0 to n-1.

3.Use an inner loop for columns j from 0 to 2*n.

4.For the upper part (first 3 rows):

• Print * if:

• j >= (n-1)-i and j = i and j

Input / Output Format

Input Format
No input required.
Fixed number of rows and columns.
Output Format
Star pattern printed using mathematical conditions on row and column indices.
Constraints
• Fixed rows (7 rows)

• Use nested loops

• Stars printed only at specific coordinates

Examples

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

Example Solution (Public)

C
#include <stdio.h>

int main() {
    int i, j;
    int rows = 8, cols = 11;

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {

            if (
                // Top two triangles
                (i <= 2 && (j <= 2 + i || j >= 8 - i)) ||

                // Middle full row
                (i == 2) ||

                // Bottom inverted triangle
                (i > 2 && j >= i - 2 && j <= cols - (i - 1))
            )
                printf("*");
            else
                printf(" ");
        }
        printf("n");
    }

    return 0;
}

Official Solution Code

#include <stdio.h>

int main() {
    int i, j;
    int rows = 8, cols = 11;

    for (i = 0; i < rows; i++) {
        for (j = 0; j < cols; j++) {

            if (
                // Top two triangles
                (i <= 2 && (j <= 2 + i || j >= 8 - i)) ||

                // Middle full row
                (i == 2) ||

                // Bottom inverted triangle
                (i > 2 && j >= i - 2 && j <= cols - (i - 1))
            )
                printf("*");
            else
                printf(" ");
        }
        printf("n");
    }

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

                                        
Please login to submit solutions.