**** * * * * * * * * **** * * * * * * * * ****
/**
* C program to print 8 star pattern series
* atnyla.com
*/
#include <stdio.h>
int main()
{
int i, j, size;
printf("Enter size: ");
scanf("%d", &size);
for(i=1; i<size*2; i++)
{
for(j=1; j<=size; j++)
{
// Condition for corner and center intersection space
if((i==1 && (j==1 || j==size)) ||
(i==size && (j==1 || j==size)) ||
(i==size*2-1 && (j==1 || j==size)))
{
printf(" ");
}
else if(i==1 || i==size || i==(size*2)-1 || j==1 || j==size)
{
printf("*");
}
else
{
printf(" ");
}
}
printf("\n");
}
return 0;
}
***
* *
* *
* *
***
* *
* *
* *
***
Basic C programming, If else, For loop, Nested loop
To simply things for beginners I have divided entire logic in three main sub tasks.
(N*2) - 1 rows.Step by step descriptive logic to print 8 star pattern.
(N*2)-1 rows. Run an outer loop with structure for(i=1; i. for(j=1; j<=N; j++).if(i==1 && (j==1 || j==N))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))if(i==1 || j==1) Nth row or Nth column i.e. if(i==N || j==N) N*2-1th row i.e. if(i==N*2-1).First read the algorithm, then study the program code line by line. After that, compare the code with the output and finally go through the explanation. This approach helps learners understand both the logic and the implementation properly.
After understanding this example, try to rewrite the same program without looking at the code. Then change some values or logic and run it again. This helps improve confidence and keeps learners engaged on the page for longer.