Write a C Program to Print X Star Pattern using for loop. This c example uses nested for loop and if condition to pint the X pattern.
#include<stdio.h> int main() { int i, j, k, rows; printf("Please Enter Number of rows = "); scanf("%d", &rows); k = rows * 2 - 1; for(i = 1; i <= k; i++) { for(j = 1; j <= k; j++) { if(j == i || j == (k - i + 1)) { printf("*"); } printf(" "); } printf("\n"); } return 0; }
This C Program uses the while loop to Print X Star Pattern.
#include<stdio.h> int main() { int i, j, k, rows; printf("Please Enter Number of rows = "); scanf("%d", &rows); k = rows * 2 - 1; i = 1; while(i <= k) { j = 1; while(j <= k) { if(j == i || j == (k - i + 1)) { printf("*"); } printf(" "); j++; } printf("\n"); i++; } return 0; }
Please Enter Number of rows = 8
* *
* *
* *
* *
* *
* *
* *
*
* *
* *
* *
* *
* *
* *
* *
In this C Program, we enter a symbol and print that symbol in X Pattern using functions.
#include<stdio.h> void xPatternFunc(int rows, char symbol) { int i, j, k; k = rows * 2 - 1; for(i = 1; i <= k; i++) { for(j = 1; j <= k; j++) { if(j == i || j == (k - i + 1)) { printf("%c", symbol); } printf(" "); } printf("\n"); } } int main() { int rows; char symbol; printf("Please Enter Symbol to print = "); scanf("%c", &symbol); printf("Please Enter Number of rows = "); scanf("%d", &rows); xPatternFunc(rows, symbol); return 0; }
Please Enter Symbol to print = $
Please Enter Number of rows = 7
$ $
$ $
$ $
$ $
$ $
$ $
$
$ $
$ $
$ $
$ $
$ $
$ $