Recently i was learning Java programming and while practicing some of the fundamentals i created some star patterns.
Working with pattern program in Java helped me think clearly the flow and working of fundamentals like if-else and loops.
1. Square star pattern
*****
*****
*****
*****
*****
Here is code to create it:
class squarePattern {
public static void main(String[] args) {
// size of the square
int size = 5;
// outer loop
for (int i = 0; i < size; i++) {
// inner loop
for (int j = 0; j < size; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
2. Left triangle star pattern
*
**
***
****
*****
Here is the code to create it.
class leftTrianlge {
public static void main(String[] args) {
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print column
for (int j = 0; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
3. Left triangle star pattern
*
**
***
****
*****
Here is the code:
class rightTrianlge {
public static void main(String[] args) {
// size of the triangle
int size = 5;
// loop to print the pattern
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 1; j < size - i; j++) {
System.out.print(" ");
}
// print stars
for (int k = 0; k <= i; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
4. Pyramid star pattern in Java
*
***
*****
*******
*********
Here is complete code for this:
class pyramid {
// pyramid star pattern
public static void main(String[] args) {
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
System.out.print(" ");
}
// print stars
for (int k = 0; k < 2 * i + 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
5. Hollow pyramid pattern
*
* *
* *
* *
*********
Here is complete code:
class hollowPyramid {
public static void main(String[] args) {
// size of the pyramid
int size = 5;
for (int i = 0; i < size; i++) {
// print spaces
for (int j = 0; j < size - i - 1; j++) {
System.out.print(" ");
}
// print stars
for (int k = 0; k < 2 * i + 1; k++) {
if (i == 0 || i == size - 1) {
System.out.print("*");
}
else {
if (k == 0 || k == 2 * i) {
System.out.print("*");
}
else {
System.out.print(" ");
}
}
}
System.out.println();
}
}
}
6. Diamond star pattern
*
***
*****
*******
*********
*******
*****
***
*
Here is complete code:
class diamond {
public static void main(String[] args) {
int size = 5;
// upside pyramid
for (int i = 1; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
System.out.print(" ");
}
// printing star
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print("*");
}
System.out.println();
}
// downside pyramid
for (int i = 1; i <= size - 1; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing star
for (int k = (size - i) * 2 - 1; k > 0; k--) {
System.out.print("*");
}
System.out.println();
}
}
}
7. Hourglass pattern
*********
*******
*****
***
*
***
*****
*******
*********
Here is complete code for this:
class hourGlass {
public static void main(String[] args) {
int size = 5;
// reversed pyramid star pattern
for (int i = 0; i < size; i++) {
// printing spaces
for (int j = 0; j < i; j++) {
System.out.print(" ");
}
// printing star
for (int k = 0; k < (size - i) * 2 - 1; k++) {
System.out.print("*");
}
System.out.println();
}
// pyramid star pattern
for (int i = 2; i <= size; i++) {
// printing spaces
for (int j = size; j > i; j--) {
System.out.print(" ");
}
// printing star
for (int k = 0; k < i * 2 - 1; k++) {
System.out.print("*");
}
System.out.println();
}
}
}
8. Heart pattern
*** ***
***** *****
***********
*********
*******
*****
***
*
Here is complete code:
class heart {
public static void main(String[] args) {
// heart star pattern
int size = 6;
for (int i = size / 2; i < size; i += 2) {
// print first spaces
for (int j = 1; j < size - i; j += 2) {
System.out.print(" ");
}
// print first stars
for (int j = 1; j < i + 1; j++) {
System.out.print("*");
}
// print second spaces
for (int j = 1; j < size - i + 1; j++) {
System.out.print(" ");
}
// print second stars
for (int j = 1; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// lower part
// inverted pyramid
for (int i = size; i > 0; i--) {
for (int j = 0; j < size - i; j++) {
System.out.print(" ");
}
for (int j = 1; j < i * 2; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Reference: TutorialsTonight.com
Top comments (0)