In this article, Interviewer may ask you below given different star patterns where you need to print different patterns of star patterns, like —
- Pyramid patterns,
- Floyd’s triangle,
- Pascal’s triangle,
- Left or right aligned pyramid, etc.
Here I wrote the code for your study, and also wrote comments everywhere in the code snippets, so you can understand the inner-outer loop and how it is working internally.
1. Pattern: Right-Angled Triangle of Stars
Expected output:
* ** *** **** *****
Solution in C Program:
#include <stdio.h>
int main() {
// Define number of rows for the pattern
int i, j;
int rows = 5;
// Outer loop for each row
for(i = 1; i <= rows; i++) {
// Inner loop to print stars in each row
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing stars in a row
}
return 0;
}
Solution in Java:
public class StarPattern {
public static void main(String[] args) {
// Define the number of rows for the pattern
int rows = 5;
// Outer loop for each row
for(int i = 1; i <= rows; i++) {
// Inner loop to print stars in each row
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing stars in a row
}
}
}
Solution in Python:
# Define the number of rows for the pattern
rows = 5
# Outer loop for each row
for i in range(1, rows + 1):
# Inner loop to print stars in each row
for j in range(i):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing stars in a row
2. Pattern: Inverted Right-Angled Triangle of Stars
Expected output:
***** **** *** ** *
Solution in C Program:
#include <stdio.h>
int main() {
// Define the number of rows for the pattern
int i, j;
int rows = 5;
// Outer loop for each row
for(i = rows; i >= 1; i--) {
// Inner loop to print stars in each row
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing stars in a row
}
return 0;
}
Solution in Java:
public class InvertedStarPattern {
public static void main(String[] args) {
// Define the number of rows for the pattern
int rows = 5;
// Outer loop for each row (decreasing)
for(int i = rows; i >= 1; i--) {
// Inner loop to print stars in each row
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing stars in a row
}
}
}
Solution in Python:
# Define the number of rows for the pattern
rows = 5
# Outer loop for each row (decreasing)
for i in range(rows, 0, -1):
# Inner loop to print stars in each row
for j in range(i):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing stars in a row
3. Pattern: Right-Angled Triangle with Centered Stars (Pyramid-like)
Expected output:
* ** *** **** *****
Solution in C Program:
#include <stdio.h>
int main() {
// Define the number of rows for the pattern
int i, j, space;
int rows = 5;
// Outer loop for each row
for(i = 1; i <= rows; i++) {
// Print spaces before stars
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Print stars
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing spaces and stars
}
return 0;
}
Solution in Java:
public class PyramidStarPattern {
public static void main(String[] args) {
// Define the number of rows for the pattern
int rows = 5;
// Outer loop for each row
for(int i = 1; i <= rows; i++) {
// Inner loop for printing spaces
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Inner loop for printing stars
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing spaces and stars
}
}
}
Solution in Python:
# Define the number of rows for the pattern
rows = 5
# Outer loop for each row
for i in range(1, rows + 1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars in each row
for j in range(i):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing spaces and stars
4. Pattern: Inverted Pyramid of Stars
Expected output:
*****
****
***
**
*
Solution in C Program:
#include <stdio.h>
int main() {
// Define the number of rows for the pattern
int i, j, space;
int rows = 5;
// Outer loop for each row
for(i = rows; i >= 1; i--) {
// Print spaces before stars
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Print stars
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing spaces and stars
}
return 0;
}
Solution in Java:
public class InvertedPyramidStarPattern {
public static void main(String[] args) {
// Define the number of rows for the pattern
int rows = 5;
// Outer loop for each row (decreasing)
for(int i = rows; i >= 1; i--) {
// Inner loop for printing spaces
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Inner loop for printing stars
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing spaces and stars
}
}
}
Solution in Python:
# Define the number of rows for the pattern
rows = 5
# Outer loop for each row (decreasing)
for i in range(rows, 0, -1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars in each row
for j in range(i):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing spaces and stars
5. Pattern: Diamond of Stars
Expected output:
*
***
*****
*******
*********
*******
*****
***
*
Solution in C Program:
#include <stdio.h>
int main() {
// Define the number of rows for the upper half of the diamond
int i, j, space;
int rows = 5; // Number of rows in the upper half
// Upper half of the diamond
for(i = 1; i <= rows; i++) {
// Print spaces before stars
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Print stars
for(j = 1; j <= (2*i - 1); j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing spaces and stars
}
// Lower half of the diamond
for(i = rows - 1; i >= 1; i--) {
// Print spaces before stars
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Print stars
for(j = 1; j <= (2*i - 1); j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing spaces and stars
}
return 0;
}
Solution in Java:
public class DiamondStarPattern {
public static void main(String[] args) {
// Define the number of rows for the upper half of the diamond
int rows = 5; // Number of rows in the upper half
// Upper half of the diamond
for(int i = 1; i <= rows; i++) {
// Print spaces before stars
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Print stars
for(int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing spaces and stars
}
// Lower half of the diamond
for(int i = rows - 1; i >= 1; i--) {
// Print spaces before stars
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Print stars
for(int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing spaces and stars
}
}
}
Solution in Python:
# Define the number of rows for the upper half of the diamond
rows = 5 # Number of rows in the upper half
# Upper half of the diamond
for i in range(1, rows + 1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars
for j in range(2 * i - 1):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing spaces and stars
# Lower half of the diamond
for i in range(rows - 1, 0, -1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars
for j in range(2 * i - 1):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing spaces and stars
6. Pattern: Hollow Diamond of Stars
Expected output:
*
* *
* *
* *
* *
* *
* *
* *
*
Solution in C Program:
#include <stdio.h>
int main() {
int i, j, space;
int rows = 5; // Number of rows in the upper half
// Upper half of the diamond
for(i = 1; i <= rows; i++) {
// Print spaces before stars
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Print stars and spaces between them
for(j = 1; j <= (2 * i - 1); j++) {
if(j == 1 || j == (2 * i - 1)) {
printf("*"); // Print the star at the beginning and end of each row
} else {
printf(" "); // Print a space in between
}
}
printf("n"); // Move to the next line after printing spaces and stars
}
// Lower half of the diamond
for(i = rows - 1; i >= 1; i--) {
// Print spaces before stars
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Print stars and spaces between them
for(j = 1; j <= (2 * i - 1); j++) {
if(j == 1 || j == (2 * i - 1)) {
printf("*"); // Print the star at the beginning and end of each row
} else {
printf(" "); // Print a space in between
}
}
printf("n"); // Move to the next line after printing spaces and stars
}
Solution in Java:
public class HollowDiamondStarPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows in the upper half
// Upper half of the diamond
for(int i = 1; i <= rows; i++) {
// Print spaces before stars
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Print stars and spaces between them
for(int j = 1; j <= (2 * i - 1); j++) {
if(j == 1 || j == (2 * i - 1)) {
System.out.print("*"); // Print the star at the beginning and end of each row
} else {
System.out.print(" "); // Print a space in between
}
}
System.out.println(); // Move to the next line after printing spaces and stars
}
// Lower half of the diamond
for(int i = rows - 1; i >= 1; i--) {
// Print spaces before stars
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Print stars and spaces between them
for(int j = 1; j <= (2 * i - 1); j++) {
if(j == 1 || j == (2 * i - 1)) {
System.out.print("*"); // Print the star at the beginning and end of each row
} else {
System.out.print(" "); // Print a space in between
}
}
System.out.println(); // Move to the next line after printing spaces and stars
}
}
}
Solution in Python:
# Define the number of rows for the upper half of the diamond
rows = 5 # Number of rows in the upper half
# Upper half of the diamond
for i in range(1, rows + 1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars and spaces between them
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="") # Print a star at the beginning and end of each row
else:
print(" ", end="") # Print a space in between
print() # Move to the next line after printing spaces and stars
# Lower half of the diamond
for i in range(rows - 1, 0, -1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars and spaces between them
for j in range(1, 2 * i):
if j == 1 or j == 2 * i - 1:
print("*", end="") # Print a star at the beginning and end of each row
else:
print(" ", end="") # Print a space in between
print() # Move to the next line after printing spaces and stars
7. Pattern: Hollow Diamond-like Pattern of Stars
Expected output:
********** **** **** *** *** ** ** * * ** ** *** *** **** **** **********
Solution in C Program:
#include <stdio.h>
int main() {
int i, j, space;
int rows = 5; // Number of rows in the upper half
// Upper half of the pattern
for(i = rows; i >= 1; i--) {
// Print stars at the start
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
// Print spaces in the middle
for(space = 1; space <= (2 * (rows - i)); space++) {
printf(" "); // Print a space
}
// Print stars at the end
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing stars and spaces
}
// Lower half of the pattern
for(i = 2; i <= rows; i++) {
// Print stars at the start
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
// Print spaces in the middle
for(space = 1; space <= (2 * (rows - i)); space++) {
printf(" "); // Print a space
}
// Print stars at the end
for(j = 1; j <= i; j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing stars and spaces
}
return 0;
}
Solution in Java:
public class HollowDiamondLikePattern {
public static void main(String[] args) {
int rows = 5; // Number of rows in the upper half
// Upper half of the pattern
for(int i = rows; i >= 1; i--) {
// Print stars at the start
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
// Print spaces in the middle
for(int space = 1; space <= (2 * (rows - i)); space++) {
System.out.print(" "); // Print a space
}
// Print stars at the end
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing stars and spaces
}
// Lower half of the pattern
for(int i = 2; i <= rows; i++) {
// Print stars at the start
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
// Print spaces in the middle
for(int space = 1; space <= (2 * (rows - i)); space++) {
System.out.print(" "); // Print a space
}
// Print stars at the end
for(int j = 1; j <= i; j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing stars and spaces
}
}
}
Solution in Python:
# Define the number of rows for the upper half of the pattern
rows = 5 # Number of rows in the upper half
# Upper half of the pattern
for i in range(rows, 0, -1):
# Print stars at the start
for j in range(i):
print("*", end="") # Print a star without moving to the next line
# Print spaces in the middle
for space in range(2 * (rows - i)):
print(" ", end="") # Print a space without moving to the next line
# Print stars at the end
for j in range(i):
print("*", end="") # Print a star without moving to the next line
print() # Move to the next line after printing stars and spaces
# Lower half of the pattern
for i in range(2, rows + 1):
# Print stars at the start
for j in range(i):
print("*", end="") # Print a star without moving to the next line
# Print spaces in the middle
for space in range(2 * (rows - i)):
print(" ", end="") # Print a space without moving to the next line
# Print stars at the end
for j in range(i):
print("*", end="") # Print a star without moving to the next line
print() # Move to the next line after printing stars and spaces
8. Pattern: Upper Pyramid of Stars
Expected output:
* *** ***** ******* *********
Solution in C Program:
#include <stdio.h>
int main() {
int i, j, space;
int rows = 5; // Number of rows for the pyramid
// Outer loop for each row
for(i = 1; i <= rows; i++) {
// Inner loop to print spaces
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Inner loop to print stars
for(j = 1; j <= (2 * i - 1); j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing stars
}
return 0;
}
Solution in Java:
public class PyramidStarPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows for the pyramid
// Outer loop for each row
for(int i = 1; i <= rows; i++) {
// Inner loop to print spaces
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Inner loop to print stars
for(int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing stars
}
}
}
Solution in Python:
# Define the number of rows for the pyramid
rows = 5 # Number of rows for the pyramid
# Outer loop for each row
for i in range(1, rows + 1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars in each row
for j in range(2 * i - 1):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing stars
9. Pattern: Inverted Pyramid of Stars
Expected output:
*********
*******
*****
***
*
Solution in C Program:
#include <stdio.h>
int main() {
int i, j, space;
int rows = 5; // Number of rows for the inverted pyramid
// Outer loop for each row
for(i = rows; i >= 1; i--) {
// Inner loop to print spaces
for(space = 1; space <= rows - i; space++) {
printf(" "); // Print a space
}
// Inner loop to print stars
for(j = 1; j <= (2 * i - 1); j++) {
printf("*"); // Print a star
}
printf("n"); // Move to the next line after printing stars
}
return 0;
}
Solution in Java:
public class InvertedPyramidStarPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows for the inverted pyramid
// Outer loop for each row
for(int i = rows; i >= 1; i--) {
// Inner loop to print spaces
for(int space = 1; space <= rows - i; space++) {
System.out.print(" "); // Print a space
}
// Inner loop to print stars
for(int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*"); // Print a star
}
System.out.println(); // Move to the next line after printing stars
}
}
}
Solution in Python:
# Define the number of rows for the inverted pyramid
rows = 5 # Number of rows for the inverted pyramid
# Outer loop for each row
for i in range(rows, 0, -1):
# Print spaces before stars
for space in range(rows - i):
print(" ", end="") # Print a space without moving to the next line
# Print stars in each row
for j in range(2 * i - 1):
print("*", end="") # Print a star without a newline
print() # Move to the next line after printing stars
Hope you love these star patterns with perfect coding on BeingCoders, which contains the point-to-point descriptions.
©️ 2025, stars pattern in C, Java, and Python programming language by Rakshit Shah (Author).
You may also like,
Discover more from 9Mood
Subscribe to get the latest posts sent to your email.















0 Comments