C Program to Print Right Arrow Star Pattern

Introduction

The right arrow star pattern is a triangular star pattern that first grows in size and then shrinks to form an arrow pointing to the right. This exercise helps in understanding the use of nested loops and how to manipulate output using spaces and stars.

Problem Statement

Create a C program that:

  • Accepts the number of rows for the right arrow pattern.
  • Prints the right arrow 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 triangle, and the lower part is symmetrical to the upper part.
  2. Use Nested Loops: The outer loops handle the rows, and the inner loops handle printing the stars and spaces.
  3. Display the Right Arrow Pattern: Print stars increasing for the upper part and decreasing for the lower part.

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 right arrow
    for (i = 1; i <= rows; i++) {
        // Print stars
        for (j = 1; j <= i; j++) {
            printf("*");
        }
        printf("\n");
    }

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

    return 0;
}

Explanation

Step 1: Input Number of Rows

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

Step 2: Print the Upper Part of the Right Arrow

  • The outer loop controls how many rows are printed for the upper part.
  • The inner loop prints stars (*) in increasing order from 1 to rows.

Step 3: Print the Lower Part of the Right Arrow

  • The second outer loop handles the rows for the lower part, which is the inverted version of the upper part.
  • The inner loop prints stars in decreasing order.

Output Example

For rows = 5, the output will be:

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

For rows = 6, the output will be:

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

Conclusion

This C program prints a right arrow star pattern by using nested loops to handle the increasing and decreasing number of stars. It is a useful exercise to practice working with loops, conditional statements, and formatting in C programming.

Comments