Home C Programming Language / Programs / C program to print X star pattern * * * * * * * * * * * * * * * * *
🚀 Programming Example

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

👁 1,413 Views
💻 Practical Program
📘 Step Learning
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 program. Logic to print X using stars in C programming.

💻 Program Code

/**
 * C program to print X star pattern series
* atnyla.com
 */

#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;
}
                        

🖥 Program Output

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

📘 Explanation

Step by step descriptive logic to print X star pattern.

  1. The pattern consists of exactly N * 2 - 1 rows and columns. Hence run an outer loop to iterate through rows with structure 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 are printed for the below two cases, otherwise print space.
    • 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 are printed if(j == count - i + 1).
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.