Star pattern problems are one of the most common beginner-level programming exercises used to build logical thinking and strengthen control structures.
Here, we’ll cover all important types of Star Pattern Problems, provide detailed questions, explain the logic, and show the C code.
Pre-requisite: Learn basic loops of C programming.
All Start Pattern Problems:
Pattern 1: Square Star Pattern
Question: In a square star pattern, you print the same number of stars in each row and column. For n = 5
, you’ll have 5 rows and 5 columns. Use two nested for
loops.
*****
*****
*****
*****
Logic: Use two nested loops. Outer loop for rows, inner loop for columns. Print *
in each column.
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 2: Right-Angled Triangle
Question:
*
**
***
****
*****
Logic: In a right-angled triangle pattern, the number of stars increases with each row.
For row i
, print i
stars.
Inner loop for printing stars up to the row number (i
)
Outer loop for rows
Code:
#include
int main() {
int n = 5;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 3: Inverted Right-Angled Triangle
Question: The goal is to decrease the number of stars in each row, starting from n
down to 1, aligned to the left.
*****
****
***
**
*
Logic: Start printing from n
stars, decrease in each row.
Code:
#include <stdio.h>
int main() {
int n = 5;
for (int i = n; i >= 1; i--) {
for (int j = 1; j <= i; j++) {
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 4: Right-Aligned Triangle
Question: Print a right-aligned triangle of stars for a given number n
.
*
**
***
****
*****
Logic: Print spaces (n-i)
and stars i
for each row.
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i; j++) {
printf(" ");
}
for(int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 5: Pyramid Start Pattern
Question: Print a centered pyramid pattern of stars for a given n
rows.
*
***
*****
*******
*********
Logic: Each row has (2*i - 1)
stars. Spaces: (n - i)
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i; j++) {
printf(" ");
}
for(int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 6: Inverted Pyramid Star Pattern
Question:The goal is to reduce stars and increase spaces in each row to form a centered upside-down triangle.
*********
*******
*****
***
*
Logic: Stars = 2*i - 1
, decrease i
. Spaces increase with row number.
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = n; i >= 1; i--) {
for(int j = 1; j <= n - i; j++) {
printf(" ");
}
for(int k = 1; k <= 2 * i - 1; k++) {
printf("*");
}
printf("\n");
}
return 0;
}
Pattern 7: Diamond Star Pattern
Question: The goal is to combine a pyramid and an inverted pyramid, first increasing then decreasing stars, while maintaining center alignment.
*
***
*****
*******
*********
*******
*****
***
*
Logic: First print a pyramid. Then inverted pyramid.
Code:
#include <stdio.h>
int main() {
int n = 5;
// Top half
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i; j++) printf(" ");
for(int k = 1; k <= 2 * i - 1; k++) printf("*");
printf("\n");
}
// Bottom half
for(int i = n - 1; i >= 1; i--) {
for(int j = 1; j <= n - i; j++) printf(" ");
for(int k = 1; k <= 2 * i - 1; k++) printf("*");
printf("\n");
}
return 0;
}
Pattern 8: Hollow Square Star Pattern
Question: The goal is to print stars only on the borders (first/last row or column), and spaces in the center.
*****
* *
* *
* *
*****
Logic: Print *
only at border positions.
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++) {
if(i == 1 || i == n || j == 1 || j == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Pattern 9: Hollow Pyramid Pattern
Question: The goal is to print stars at the edges of the pyramid and on the base, leaving spaces in the middle for a hollow effect.
*
* *
* *
* *
*********
Logic: Star at first or last position in row or in the last row.
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n - i; j++) printf(" ");
for(int k = 1; k <= 2 * i - 1; k++) {
if(k == 1 || k == 2 * i - 1 || i == n)
printf("*");
else
printf(" ");
}
printf("\n");
}
return 0;
}
Pattern 10: Pascal-like Half Pyramid
Question: The goal is to print stars with a space after each one, increasing the count row by row to form a spaced triangle.
*
* *
* * *
* * * *
* * * * *
Logic: Just alternate stars and spaces, inner loop till current row.
Code:
#include <stdio.h>
int main() {
int n = 5;
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}
return 0;
}
Tips to Solve Any Star Pattern
- Break the pattern into rows and columns.
- Observe how stars and spaces are increasing or decreasing.
- Use nested
for
loops:- Outer for rows.
- Inner for columns.
- Use
if-else
for hollow or conditional patterns.
Final Words
Star pattern problems are an essential part of learning C programming, especially for beginners aiming to strengthen their understanding of loops, nested loops, and conditional logic.
These problems not only help in building a strong programming foundation but also enhance problem-solving skills, which are crucial for competitive programming and technical interviews.
Awesome! Its genuinely remarkable post, I have got much clear idea regarding from this post
Thanks