C program to print X star pattern

Write a C program to print X star pattern series using loop. How to print the X star pattern series using for loop in C programming. Logic to print X using stars in C program.

Example

Input

Input N: 5

Output

Required knowledge

Basic C programming, If else, Loop

Logic to print X star pattern

There can be many logic to solve the particular problem. Here I am adding two methods to print the given star pattern. First I will cover the easiest method.

Logic to print the X star pattern can really be complex and confusing if you haven't gone through two previous star pattern programs.

Must know -

If you bisect the pattern horizontally you will get two pattern.

X star pattern is a combination of both of these patterns. Where the first one is similar to the hollow inverted pyramid and second one to hollow pyramid star pattern. Hence if you haven't gone through these two posts I recommend you to go through them before continuing. As I won't be getting in detail about these two patterns here.

Program to print X star pattern

/**
 * C program to print X star pattern series
 */

#include <stdio.h>

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

    printf("Enter N: ");
    scanf("%d", &N);

    // Print first upper hollow inverted pyramid
    for(i=N; i>=1; i--)
    {
        // Print trailing spaces
        for(j=i; j<N; j++)
        {
            printf(" ");
        }

        // Print upper hollow triangular part
        for(j=1; j<=(2*i-1); j++)
        {
            if(j==1 || j==(2*i-1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        printf("\n");
    }

    // Print the lower half hollow pyramid 
    for(i=2; i<=N; i++)
    {
        //Prints trailing spaces
        for(j=i; j<N; j++)
        {
            printf(" ");
        }

        //Prints lower hollow triangular part
        for(j=1; j<=(2*i-1); j++)
        {
            if(j==1 || j==(2*i-1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        printf("\n");
    }

    return 0;
}

Logic to print the X star pattern - 2

This logic was submitted by a Codeforwin lover - Md Shahbaz. Let us see the pattern once more with different angle. Divide the given pattern logically in two different sections as -

Here goes the descriptive logic to print the pattern :

  1. The pattern consists of exactly N * 2 - 1 rows and columns. Hence run an outer loop to iterate through rows using the loop formation for(i=1; i<= count; i++) (where count = N * 2 - 1).
  2. Since each row contains exactly N * 2 - 1 columns. Therefore, run inner loop as for(j=1; j<=count; j++).
  3. Inside this loop as you can notice that stars gets printed in two conditions i.e.
    • For the first diagonal i.e. when row and column number both are equal. Means print star whenever if(i == j).
    • For the second diagonal i.e. stars gets printed if(j == count - i + 1).
  4. Print spaces whenever stars doesn't gets printed.

Program to print X star pattern - 2

/**
 * C program to print X star pattern series
 */

#include <stdio.h>

int main()
{
    int i, j, N;
    int count;

    printf("Enter N: ");
    scanf("%d", &N);

    count = N * 2 - 1;

    for(i=1; i<=count; i++)
    {
        for(j=1; j<=count; j++)
        {
            if(j==i || (j==count - i + 1))
            {
                printf("*");
            }
            else
            {
                printf(" ");
            }
        }

        printf("\n");
    }

    return 0;
}

Before you move on. You might love to check the similar number pattern program

Output

Enter N: 5
*       *
 *     *
  *   *
   * *
    *
   * *
  *   *
 *     *
*       *

Happy coding 😉

Write your doubts or suggestion. I will try my best to help. You must escape source code before commenting. To format your source code paste your source code inside
<pre><code> ----Your Source Code---- </code></pre>