C program to print 8 star pattern
****
* *
* *
* *
* *
****
* *
* *
* *
* *
****
C Programming Language Loop control in C Language (Article) Loop control in C Language (Program)
1112Program:
/** * C program to print 8 star pattern series * atnyla.com */ #include int main() { int i, j, size; printf("Enter size: "); scanf("%d", &size); for(i=1; i
Output:
*** * * * * * * *** * * * * * * ***
Explanation:
Required knowledge
Basic C programming, If else, For loop, Nested loop
Logic to print 8 star pattern
To simply things for beginners I have divided entire logic in three main sub tasks.
- Print a hollow square star pattern with N columns and
(N*2) - 1
rows. - Modify above hollow square pattern logic. Instead of star, print space at top and bottom corners and in the center intersection edge.
- Finally modify hollow square logic to print star instead of space for Nth row.
Step by step descriptive logic to print 8 star pattern.
- Input number of columns to print from user. Store it in a variable say N.
- Iterate through
(N*2)-1
rows. Run an outer loop with structurefor(i=1; i<N*2; i++)
. - Iterate through N columns. Run an inner loop with structure
for(j=1; j<=N; j++)
. - Inside inner loop first check conditions for corner and center intersection spaces. Print space for
1st row and 1st or Nth column i.e.if(i==1 && (j==1 || j==N))
Nth row and 1st or Nth column i.e.if(i==N && (j==1 || j==N))
N*2-1
row and 1st or Nth column i.e.if(i==N*2-1 && (j==1 || j==N))
- Print stars for 1st row or 1st column i.e.
if(i==1 || j==1)
Nth row or Nth column i.e.if(i==N || j==N)
N*2-1
th row i.e.if(i==N*2-1)
. - Otherwise print spaces if above conditions does not matches.
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.