C program to print right arrow star pattern

Write a C program to print right arrow star (*) pattern series using for loop. How to print right arrow star pattern structure using for loop in C programming. The pattern should look like:
*****
  ****
    ***
      **
        *
        *
      **
    ***
  ****
*****
If you were looking for reverse of this pattern then find it here how to print left arrow star pattern.

Required knowledge:

Basic C programming, For loop

Logic:

This pattern is a combination of two patterns hence, let's first divide it into two parts where the first upper part looks like:
*****
  ****
    ***
      **
        *
and the second bottom part looks like:
        *
      **
    ***
  ****
*****
Now if you have noticed total number of spaces in upper part is 2*rownumber - 2 per row and bottom part contains total 2*n - 2*rownumber spaces per row (where n is the total number of rows). And if you ignore the trailing spaces, then the upper part star pattern would look similar to inverted right triangle star pattern and bottom part to simple right triangle star pattern which can be easily printed.

Program:

/**
 * C program to print right arrow star pattern
 */

#include <stdio.h>

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

    //Reads number of rows from user
    printf("Enter value of n : ");
    scanf("%d", &n);

    //Prints the upper part of the arrow
    for(i=1; i<=n; i++)
    {
        //Prints trailing (2*rownumber-2) spaces
        for(j=1; j<=(2*i-2); j++)
        {
            printf(" ");
        }

        //Prints inverted right triangle star pattern
        for(j=i; j<=n; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    //Prints lower part of the arrow
    for(i=1; i<=n; i++)
    {
        //Prints trailing (2*n - 2*rownumber) spaces
        for(j=1; j<=(2*n - 2*i); j++)
        {
            printf(" ");
        }

        //Prints simple right triangle star pattern
        for(j=1; j<=i; j++)
        {
            printf("*");
        }

        printf("\n");
    }

    return 0;
} 


Output
X
_
Enter value of n : 5
*****
  ****
    ***
      **
        *
        *
      **
    ***
  ****
*****


Screenshot:

C program to print arrow star pattern

Happy coding ;)


Any doubt or suggestion write here. I will try my best to help. Before posting your code you must escape it to view. To format your source code and use format highlighting, post your source code inside
< code >< pre > -- Your source code -- < /pre >< /code > (Remove spaces from pre and code tags).

No comments:

Post a Comment