Whenever you prepare for Interview you must be aware with printing patterns , In coding interview interviewer may ask you print any number pattern , alphabet pattern or an character patterns. We recommend you to follow our patterns series so you will feel comfortable with printing patterns. In this blog post you will find set 5 of star patterns.
We recommend you to solve other types patterns as well –
 Click on the pattern and you will get redirected to the code.
Pattern 1
*****
*****
*****
*****
*****
Code for pattern 1
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if(j+i>=8 && j+i<=12 && i>=4)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#includeint main() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (j + i >= 8 && j + i <= 12 && i >= 4) { std::cout << "*"; } else { std::cout << " "; } } std::cout << std::endl; } return 0; };
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 9) :
j = 0
while (j < 9) :
if (j + i >= 8 and j + i <= 12 and i >= 4) :
print("*", end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 9; i++) {
for (j; j < 9; j++) {
if (j + i >= 8 && j + i <= 12 && i >= 4) {
console.log("*");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 2
*****
*****
*****
*****
******
Code for pattern 2
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if(j-i>=0 && j-i<=4 && i<=4)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#includeint main() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (j - i >= 0 && j - i <= 4 && i <= 4) { std::cout << "*"; } else { std::cout << " "; } } std::cout << std::endl; } return 0; };
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 9) :
j = 0
while (j < 9) :
if (j - i >= 0 and j - i <= 4 and i <= 4) :
print("*", end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 9; i++) {
for (j; j < 9; j++) {
if (j - i >= 0 && j - i <= 4 && i <= 4) {
console.log("*");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 3
*****
*****
*****
*****
*****
*****
*****
*****
*****
Code for pattern 3
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if(j+i>=8 && j+i<=12 && i>=4 || j-i>=0 && j-i<=4 && i<=4)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#includeint main() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (j + i >= 8 && j + i <= 12 && i >= 4 || j - i >= 0 && j - i <= 4 && i <= 4) { std::cout << "*"; } else { std::cout << " "; } } std::cout << std::endl; } return 0; };
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 9) :
j = 0
while (j < 9) :
if (j + i >= 8 and j + i <= 12 and i >= 4 or j - i >= 0 and j - i <= 4 and i <= 4) :
print("*", end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 9; i++) {
for (j; j < 9; j++) {
if (j + i >= 8 && j + i <= 12 && i >= 4 || j - i >= 0 && j - i <= 4 && i <= 4) {
console.log("*");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);
Pattern 4
*****
*****
*****
*****
*****
*****
*****
*****
*****
Code for pattern 4
Java
C++
Python
JavaScript
Java
public class MainClass {
public static void main(String[] args) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
if(j+i>=4 && j+i<=8 && i<=4 || j-i>=-4 && j-i<=0 && i>=4)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
}
}
C++
#includeint main() { for (int i = 0; i < 9; i++) { for (int j = 0; j < 9; j++) { if (j + i >= 4 && j + i <= 8 && i <= 4 || j - i >= -4 && j - i <= 0 && i >= 4) { std::cout << "*"; } else { std::cout << " "; } } std::cout << std::endl; } return 0; };
Python
class MainClass :
@staticmethod
def main( args) :
i = 0
while (i < 9) :
j = 0
while (j < 9) :
if (j + i >= 4 and j + i <= 8 and i <= 4 or j - i >= -4 and j - i <= 0 and i >= 4) :
print("*", end ="")
else :
print(" ", end ="")
j += 1
print()
i += 1
if __name__=="__main__":
MainClass.main([])
JavaScript
class MainClass {
static main(args) {
for (i; i < 9; i++) {
for (j; j < 9; j++) {
if (j + i >= 4 && j + i <= 8 && i <= 4 || j - i >= -4 && j - i <= 0 && i >= 4) {
console.log("*");
} else {
console.log(" ");
}
}
console.log();
}
}
}
MainClass.main([]);