C program to print left arrow star pattern

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

Required knowledge:

Basic C programming, For loop

Logic:

Let's first divide this pattern in two parts to make our task easy. Where the first upper part looks like:
    *****
   ****
  ***
 **
*
and the bottom part looks like:
*
 **
  ***
   ****
    *****
Now, if you have noticed number of spaces in the upper part per row is n - rownumber and bottom part contains rownumber - 1 spaces per row (where n is the total number of rows). And if you ignore the trailing spaces in both parts you will notice that the upper part is similar to inverted right triangle and lower part is similar to right triangle star pattern.

Program:

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

#include <stdio.h>

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

    //Reads the 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 (n-rownumber) spaces
        for(j=1; j<=(n-i); j++)
        {
            printf(" ");
        }

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

        printf("\n");
    }

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

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

        printf("\n");
    }

    return 0;
} 


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


Screenshot:

C program to print mirrored 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