C Program to Print X Star Pattern

X star pattern in C: In the following C program we will learn how to print X star pattern and X number pattern.

C program to print X star pattern

#include<stdio.h>

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

	printf("How many lines to print:- ");
	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 of above code:-

How many lines to print:- 5

*       *
 *     * 
  *   *  
   * *   
    *    
   * *   
  *   *  
 *     * 
*       *

*/ 

C program to print X number pattern

#include<stdio.h>

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

	printf("How many lines to print:- ");
	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 of above code:-

How many lines to print:- 5

1       9
 2     8 
  3   7  
   4 6   
    5    
   4 6   
  3   7  
 2     8 
1       9


*/