C Program to Print Left Arrow Star Pattern

Introduction

The left arrow star pattern is formed by first printing an inverted triangle followed by a regular triangle. The pattern resembles an arrow pointing to the left. This exercise helps in understanding how to control the number of spaces and stars in a pattern using nested loops in C programming.

Problem Statement

Create a C program that:

  • Accepts the number of rows for the left arrow pattern.
  • Prints a left arrow-shaped star pattern.

Example:

  • Input: rows = 5
  • Output:
      *****
       ****
        ***
         **
          *
         **
        ***
       ****
      *****
    

Solution Steps

  1. Input the Number of Rows: The size determines the number of rows for the upper and lower triangles.
  2. Use Nested Loops: The outer loop handles the rows, and the inner loops handle printing spaces and stars for both the upper inverted triangle and the lower regular triangle.
  3. Display the Left Arrow Pattern: Print stars in decreasing order for the upper part and increasing order for the lower part, with spaces for alignment.

C Program

#include <stdio.h>

int main() {
    int i, j, rows;

    // Step 1: Accept the number of rows for the arrow pattern
    printf("Enter the number of rows: ");
    scanf("%d", &rows);

    // Step 2: Print the upper part of the left arrow (inverted triangle)
    for (i = 0; i < rows; i++) {
        // Print spaces for alignment
        for (j = 0; j < i; j++) {
            printf(" ");
        }
        // Print stars
        for (j = 0; j < rows - i; j++) {
            printf("*");
        }
        printf("\n");
    }

    // Step 3: Print the lower part of the left arrow (regular triangle)
    for (i = 1; i < rows; i++) {
        // Print spaces for alignment
        for (j = rows - i - 1; j >= 0; j--) {
            printf(" ");
        }
        // Print stars
        for (j = 0; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

    return 0;
}

Explanation

Step 1: Input Number of Rows

  • The program starts by asking the user to input the number of rows for the left arrow pattern.

Step 2: Print the Upper Part of the Left Arrow (Inverted Triangle)

  • The outer loop controls the number of rows for the upper part (inverted triangle).
  • The first inner loop prints spaces to align the stars to the right, and the second inner loop prints stars in decreasing order to form the inverted triangle.

Step 3: Print the Lower Part of the Left Arrow (Regular Triangle)

  • The second outer loop handles the rows for the lower part (regular triangle).
  • The first inner loop prints spaces for alignment, and the second inner loop prints stars in increasing order to form the regular triangle.

Output Example

For rows = 5, the output will be:

*****
 ****
  ***
   **
    *
   **
  ***
 ****
*****

For rows = 6, the output will be:

******
 *****
  ****
   ***
    **
     *
    **
   ***
  ****
 *****
******

Conclusion

This C program prints a left arrow star pattern by using nested loops to handle the spaces and stars. The program first prints an inverted triangle and then a regular triangle, forming a pattern that looks like a left-pointing arrow. This exercise helps in practicing control structures and formatting output in C programming.

Comments