C program to print X star pattern
* *
* *
* *
* *
*
* *
* *
* *
* *
C Programming Language / Loop control in C Language
1364Program:
/** * C program to print X star pattern series * atnyla.com */ #include 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; }
Output:
Enter N: 5 * * * * * * * * * * * * * * * * *
Explanation:
Step by step descriptive logic to print X star pattern.
- The pattern consists of exactly
N * 2 - 1
rows and columns. Hence run an outer loop to iterate through rows with structurefor(i=1; i<= count; i++)
(wherecount = N * 2 - 1
). - Since each row contains exactly
N * 2 - 1
columns. Therefore, run inner loop asfor(j=1; j<=count; j++)
. - 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)
.
- For the first diagonal i.e. when row and column number both are equal. Means print star whenever
This Particular section is dedicated to Programs only. If you want learn more about C Programming Language. Then you can visit below links to get more depth on this subject.
# C Tutorials
# JAVA Tutorials
# HTML Tutorials
# Computer Fundamental
# Data Structure
# DBMS Tutorials
SQL
# C# Language
# R Language
# PHP
# Python
# Vue JS