Signup/Sign In

C Program To Print The X Star Pattern

Logic To Print The X Star Pattern:

To print the X star pattern, the user should print n rows & n columns, hence there will be two nested loops that need to run,

The outer loop is used to print the rows one after another, The inner loop is used to print the columns of all rows

There are two loops in the program, similarly, there will be two cases that need to satisfy,

  • when both rows & columns are equal, Print star
  • for another part print stars when "j ==count -i+1"

C Program To Print The X Star Pattern:

#include <stdio.h>

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

printf("Enter no. Of Rows To Print X Pattern: ");
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;
}

Output:

X star pattern



About the author:
I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development. Founder @ Studytonight