Wednesday, January 18, 2017

Java Program: Print Pattern with star java example


To find out multiple example of star print pattern.


Output:



Java code.

package swain.swain;

public class PrintPattern {
public static void example1() {
int space = 0;
for (int i = 5; i >= 1; i--) {
// print first part of the row
for (int j = i; j >= 1; j--)
System.out.print("*");

// print space
for (int j = 1; j <= space; j++)
System.out.print(" ");

// print second part of the row
for (int j = i; j >= 1; j--)
System.out.print("*");

// print new lint
System.out.println();
space = space + 2;
}

space = 8;
for (int i = 1; i <= 5; i++) {
// print first part of the row
for (int j = 1; j <= i; j++)
System.out.print("*");

// print space
for (int j = 1; j <= space; j++)
System.out.print(" ");

// print second part of the row
for (int j = 1; j <= i; j++)
System.out.print("*");

// print new lint
System.out.println();
space = space - 2;
}
}

public static void example2() {
int space = 0;
for (int i = 5; i >= 1; i--) {
// print first part of the row
for (int j = i; j >= 1; j--)
System.out.print("*");

// print space
for (int j = 1; j <= space; j++)
System.out.print(" ");

// print second part of the row
for (int j = i; j >= 1; j--)
System.out.print("*");

// print new lint
System.out.println();
space = space + 2;

}
}

public static void example3() {
int space = 8;
for (int i = 1; i <= 5; i++) {
// print first part of the row
for (int j = 1; j <= i; j++)
System.out.print("*");

// print space
for (int j = 1; j <= space; j++)
System.out.print(" ");

// print second part of the row
for (int j = 1; j <= i; j++)
System.out.print("*");

// print new lint
System.out.println();
space = space - 2;
}
}

public static void example4() {
int space = 0;
for (int i = 5; i >= 1; i--) {
// print spaces
for (int j = 1; j <= space; j++)
System.out.print(" ");

// print stars
for (int j = 1; j <= i; j++)
System.out.print("* ");

// print new line
System.out.println();
space++;

}
}

public static void example5() {
int space = 4;
for (int i = 1; i <= 5; i++) {
// print spaces
for (int j = 1; j <= space; j++)
System.out.print(" ");

// print stars
for (int j = 1; j <= i; j++)
System.out.print("* ");

// print new line
System.out.println();
space--;

}
}

public static void main(String args[]) {
System.out.println("Example 1");
PrintPattern.example1();
System.out.println("Example 2");
PrintPattern.example2();
System.out.println("Example 3");
PrintPattern.example3();
System.out.println("Example 4");
PrintPattern.example4();
System.out.println("Example 5");
PrintPattern.example5();
}
}

No comments:

Post a Comment

How ChatGPT can Benefit Coding: Your Guide to Leveraging an AI Language Model

 Introduction: Hello, coders! Welcome to this blog post on how ChatGPT, an AI language model, can benefit your coding skills and projects. A...