Star Patterns in Java
First, let us begin with some of the basic and the commonly asked pattern program in Java.
Such as:
- Pyramid Star Program,
- Left Triangle Star Program,
- Right Triangle Star Program, etc.
Below Example prints the star(*), you can replace star to number or character as your wish to print.
1. Pyramid Program:
Code:
public class Main
{
public static void main(String[] args) {
for (int i=0; i<5; i++)
{
for (int j=5;j>i;j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
{
public static void main(String[] args) {
for (int i=0; i<5; i++)
{
for (int j=5;j>i;j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
2. Left Triangle Star Pattern:
Code:
public class Main
{
public static void main(String[] args) {
for (int i=0; i<5; i++)
{
for (int j=2*(5-i);j>0;j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
{
public static void main(String[] args) {
for (int i=0; i<5; i++)
{
for (int j=2*(5-i);j>0;j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
3. Right Triangle Star Pattern:
Code:
public class Main
{
public static void main(String[] args) {
for (int i=0; i<5; i++)
{
for (int j=2*(5-i);j>0;j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
{
public static void main(String[] args) {
for (int i=0; i<5; i++)
{
for (int j=2*(5-i);j>0;j--)
{
System.out.print(" ");
}
for (int j=0; j<=i; j++ )
{
System.out.print("*");
System.out.print(" ");
}
System.out.println();
}
}
}
Thank You For Reading !!! Keep Reading !!!
Comments
Post a Comment