Home C Programming Language / Programs / C program to print 8 star pattern **** * * * * * * * * **** * * * * * * * * ****
🚀 Programming Example

C program to print 8 star pattern
 ****
*    *
*    *
*    *
*    *
 ****
*    *
*    *
*    *
*    *
 ****

👁 1,227 Views
💻 Practical Program
📘 Step Learning
Write a C program to print 8 star pattern series using loop. How to print 8 star pattern series using for loop in C program. Logic to print 8 star pattern series of N columns in C programming.

💻 Program Code

/**
 * 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;
}
                        

🖥 Program 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.

  1. Input number of columns to print from user. Store it in a variable say N.
  2. Iterate through (N*2)-1 rows. Run an outer loop with structure for(i=1; i.
  3. Iterate through N columns. Run an inner loop with structure for(j=1; j<=N; j++).
  4. 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))
  5. 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-1th row i.e. if(i==N*2-1).
  6. Otherwise print spaces if above conditions does not matches.
📚 Learning Subject

Master Programming Through Practical Examples

Improve your coding logic, problem-solving skills and programming confidence by practicing real-world examples with explanations.

🎯 How to learn from this example

First understand the algorithm carefully. Then study the program line-by-line and compare it with the output. Finally, review the explanation section to strengthen your logic and programming understanding.

🔥 Practice suggestion

Rewrite the program without looking at the code. Modify values, conditions or logic and run it again. This helps improve confidence and strengthens coding skills much faster.