Program to Print a Star Pattern in C
Introduction
Hii Ninjas, today we will learn the most common type of question in C, Star Pattern in C. These are the most triggering questions because it opens your mind and helps you develop your logical thinking. We will be doing this question using C language. Moreover, we will be seeing up to five pattern problems.

Brushing up the Basics
We will use nested for loop, which we have studied. A question might come to your mind, Why will we use nested for loops? We will use nested for loops because we must travel both the rows and columns. So one for loop will take care of the rows, and the other will take care of the columns. This is how we will be doing our questions.
Recommended Topic, Binary to Hex Converter.
Program to Print Star Pattern 1

Implementation
#include <stdio.h>
int main()
{
// Write C code here
int i, j;
for (int i = 0; i < 3; i++)
{
// For rows
for (int j = 0; j < 3; j++)
{
// For columns
printf("*");
}
printf("\n");
}
return 0;
}
Output

Explanation
Let’s understand the steps of the above code for the Star Pattern in C
- We need to take i and j for traversing over rows and columns.
- i is for rows, and j is for columns.
- i start from 0(row =0) and then it comes over j(column=0) initially.
- Then it prints the star at i=0 and j=0.
- Then, our inner loop of j moves forward, and j becomes 1(j=1).
- The star gets printed over in the next columns.
- After j reaches its last value. The i value increases.
- And hence the process goes on till the last value of i.
- Finally, we get our pattern.
Logic
Assume i and j have maximum value till 3,
- When i=0, j=0,1,2.
- When i=1, j=0,1,2.
- When i=2, j=0,1,2.
Ninjas, here j is fixed like we have to print till j=3, which means in every row, we have to fill the columns till 3.
Here, we wrote the code so directly without any manipulation since the output was easy, but as we move ahead, manipulation is needed. So let’s learn more.in Star Pattern in C
You can also read about dynamic array in c.
Program to Print Star Pattern 2

Implementation
#include <stdio.h>
int main()
{
// Write C code here
int i, j;
// Traversing over rows
for (int i = 0; i < 4; i++)
{
// Inner loop runs till j=i and then stops
for (int j = 0; j <= i; j++)
{
// Then prints star
printf("*");
}
// Change of row
printf("\n");
}
return 0;
}
Output

Explanation
Let’s understand the steps of the above code in Star Pattern in C.
- We need to take i and j for traversing over rows and columns.
- i is for rows, and j is for columns.
- i start from 0(row =0) and then it comes over j(column=0) initially.
- Then it prints the star at i=0 and j=0.
- Now here, the inner loop runs till j=i (by observation).
- Then i=1, and again inner loop, j starts from 0 and prints maximum till j=i.
- Hence, we get the required pattern.

Logic
Assume i and j have maximum value till 3,
When i=0, j=0 ----> *
When i=1, j=0,1 ----> * *
When i=2, j=0,1,2 ----> * * *
When i=3, j=0,1,2,3 ----> * * * *
Here, j is variable, and on observing, we get that it has a connection with i, so we applied that to the outer and inner loop.
Program to Print Star Pattern 3
Implementation
#include <stdio.h>
int main()
{
// Write C code here
int i, j, rows;
printf("enter number of rows: ");
// Taking input from user
scanf("%d", &rows);
for (int i = 0; i < rows; i++)
{
// Traversing over rows
for (int j = rows; j > i; j--)
{
// Traversing over columns in reverse order
// Printing star
printf("*");
}
// Changing row
printf("\n");
}
return 0;
}
Input
enter number of rows: 4
Output

Explanation
Let’s understand the steps of the above code in Star Pattern in C
- We need to take i and j for traversing over rows and columns.
- i is for rows, and j is for columns.
- Here, we can see that as we are going down, i is undoubtedly increasing, but j is decreasing.
- So, i starts from i=0 and runs till several rows, but j starts from rows, i.e., from the maximum value, and decreases by one each time.
- Now, after the first row where i=0 and j=4 (taking rows = 4), i becomes i=2(since,i++), and j starts from j=4 but ends when j becomes 2.
- Hence, we get the required pattern.
Logic
Assume i and j have maximum value till 3.
When i=0, j=4 ----> * * * *
When i=1, j=4,3 ----> * * *
When i=2, j=4,3,2 ----> * *
When i=3, j=4,3,2,1 ----> *
Here, j is variable, and on observing, we get that it has a connection with i, so we applied that to the outer and inner loop. As i increases, j decreases.
Program to Print Star Pattern 4

Following the steps to understand the code
- Step 1 Analyse the pattern first.
- Step 2 Values of i and j.
i | j |
1 | 5 |
2 | 4 5 6 |
3 | 3 4 5 6 7 |
4 | 2 3 4 5 6 7 8 |
5 | 1 2 3 4 5 6 7 8 9 |
- Step 3 Finding the relation between i and j.
i | j |
---|---|
1 | j>=5 && j<=5 |
2 | j>=4 && j<=6 |
3 | j>=3 && j<=7 |
4 | j>=2 && j<=8 |
5 | j>=1 && j<=9 |
- Step 4 Observation- j>=6-i && j<= 4-i we have a print star. Otherwise, print space.
Implementation
#include <stdio.h>
int main()
{
// Taking variables i and j for traversing.
int i, j;
for (int i = 1; i <= 5; i++)
{
// ‘i’ runs from 1 to 5, traversing over rows
for (int j = 1; j <= 9; j++)
{
// Here, j runs from 1 to 9
if (j >= 6 - i && j <= 4 + i)
printf("*");
// If above condition fails means no star hence print space
else printf(" ");
}
// We need to change row after completion of every row traversal
printf("\n");
}
return 0;
}
Output

Explanation
Let’s understand the steps of the above code in Star Pattern in C.
- We need to take i and j for traversing over rows and columns.
- i is for rows, and j is for columns.
- i run as usual from i=0 to i=rows.
- But here, we can see that j is dependent on i, as j>=6-i && j<=4+i.
- Hence we get the required pattern.
Logic
For any value of i, we get j as j>=6-i && j<=4+i for printing star; otherwise, we print space.
Program to Print Star Pattern 5

Following the steps to understand the code
- Step 1 Analyse the pattern first.
- Step 2 Observing the pattern from the above figure
i | j |
1 | 1 2 3 4 5 6 7 8 9 |
2 | 2 3 4 5 6 7 8 |
3 | 3 4 5 6 7 |
4 | 4 5 6 |
5 | 5 |
- Step 3 Finding the relation between i and j
i | j |
1 | j>=1 && j<=9 |
2 | j>=2 && j<=8 |
3 | j>=3 && j<=7 |
4 | j>=4 && j<=6 |
5 | j>=5 && j<=5 |
- Step 4 We have a print star in observation- j>=i && j<= 2*rows-i. Otherwise, print space.
Implementation
#include <stdio.h>
int main()
{
// Write C code here
int i, j, rows;
printf("enter number of rows: ");
// Taking input from users
scanf("%d", &rows);
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= 2 *rows - 1; j++)
{
// For printing stars
if (j >= i && j <= 2 *rows - i)
printf("*");
// For space
else printf(" ");
}
// Changing row
printf("\n");
}
return 0;
}
Input
enter number of rows: 4
Output

You can practice by yourself with the help of online c compiler.
Explanation
Let’s understand the steps of the above code in Star Pattern in C.
- We need to take i and j for traversing over rows and columns.
- i is for rows, and j is for columns.
- i run as usual from i=0 to i=rows.
- But here, we can see that j is dependent on i, as j>=i && j<=2*rows-i.
- Hence we get the required pattern.
Logic
For any value of i, we get j as j>=i && j<=2*rows-i for printing star; otherwise, we print space.
Frequently Asked Questions
How to solve patterns in C?
Star Patterns in C can be made by practising more questions in C language and building logic for the same.
What does %g mean in C?
It means we are printing decimal float values.
Is C a complicated language?
No, C language is not complex. It can be learned quickly by practising and understanding the syntax.
Is C++ harder or C?
C++ has new features compared to C. C can be considered the primary language to learn, whereas C++ includes some new classes and features different from C.
Which coding language is best?
There is no such answer to this question because every language has its importance. Take up any language and make yourself the best in it.
Conclusion
We have studied different Star Patterns in C. We have observed other logic here. We can build different types of Star Pattern in C. They are the basics, so they help us build our logical skills. So ninjas practice them and code themselves after understanding the logic in C language.
Refer to the below topics for more understanding
Please refer to our guided paths on Codestudio to learn more about DSA, Competitive Programming, JavaScript, System Design, etc. And also, enroll in our courses and refer to the mock test and problems available. Have a look at the interview experiences and interview bundle for placement preparations.
Happy Learning!