Print Star Pattern in Java


Prev Tutorial Next Tutorial

Java Program to Print star pattern

In Java language you can print triangle shape using for loop and also using while loop, Here we discuss about how to print Triangle of stats in very simple and easy way. But before learning this topic first read carefully if, if..else, for loop and while loop concept in detail. To Print Star Pattern in Java you need looping concept, if..else statement and print() and println() function.

To print patterns of numbers and stars (*) in Java Programming, we need to use two loops, first is outer loop and the second is inner loop. Where outer loop is responsible for print rows and the inner loop is responsible for print columns.

Difference Between print() and println()

  • System.out.print("....."): is used for display message on screen or console but cursor don't move in new line.
  • System.out.println("....."): is used for display message on screen or console and cursor move in new line.
print star pattern in java

Print Star Pattern in Java

class Star 
{
public static void main(String[] args) 
{
 int i;
for(i=5;i>=1;i--)
{
System.out.println("*");
}
}
}

Output

*
*
*
*
*

Syntax to compile and run java program

Syntax

for compile -> c:/>javac Star.java
for run -> c:/>java Star

Code Explanation: Here we use println() function to print output, after print one output cursor move in new line. The main difference between print() and println() function is; print() function don't move cursor in new line after print output on screen but in case of println() function it move cursor in new line after print output.

Print Star Pattern in Java

class Star 
{
public static void main(String[] args) 
{
 int i,j,k;
for(i=1; i<=4; i++)
{
for(j=1; j<=5; j++)
{
System.out.print("*");
}
System.out.println(" ");
}
}
}

Output

*****
*****
*****
*****

Print star pattern in java

class StarTriangle
{
public static void main(String[] args) 
{
int i,j,k;
for(i=1; i<=5; i++)
{
for(j=4; j>=i; j--)
{
System.out.print(" ");
}
for(k=1; k<=(2*i-1); k++)
{
System.out.print("*");
}
System.out.println("");
}
}
}

Output

          *
        *  *
       *  *  *
     *  *  *  *
   *   *  *  *  * 
print star pattern in java

Print Star Pattern in Java

class Star 
{
public static void main(String[] args) 
{
 int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

* * * * * * * * *
  * * * * * * *
    * * * * *
      * * * 
        *

Print Star Pattern in Java

class Star 
{
public static void main(String[] args) 
{
int i,j;
for(i=1; i<=6; i++)
{
for(j=1; j<i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

*
* *
* * *
* * * *
 * * * * *

Print * Triangle in Java

class StarPattern
{
 public static void main(String args[])
 {
 int i, j, k=1;     
 for(i=0; i<5; i++)      
 {           
 for(j=0; j<k; j++)          
 {              
  System.out.print("* ");          
 }
 k = k + 2;
 System.out.println(); 
 }
}
}

Output

* 
* * * 
* * * * * 
* * * * * * * 
* * * * * * * * * 

Print Simple Pattern in Java

class Star 
{
public static void main(String[] args) 
{
 int i, j;
 for(i=5;i>=1;i--)
{
for(j=1;j<=i;j++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

* * * * *
* * * *
* * *
* *
*

Print Triangle of Stars in Java

class Star 
{
public static void main(String[] args) 
{
int i, j, k;
for(i=5;igt;=1;i--)
{
for(j=1;jlt;i;j++)
{
System.out.print(" ");
}
for(k=5;k>=i;k--)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

        *
      * *
    * * *
  * * * *
* * * * *

Print Star Pattern in Java

class Star 
{
public static void main(String[] args) 
{
int i, j, k;
for(i=5;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<=i;k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

* * * * *
  * * * * 
    * * * 
      * * 
        *

Print * Pattern in Java

class Star 
{
public static void main(String[] args) 
{
int i, j, k;
for(i=1;i<=5;i++)
{
for(j=i;j<5;j++)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
for(i=4;i>=1;i--)
{
for(j=5;j>i;j--)
{
System.out.print(" ");
}
for(k=1;k<(i*2);k++)
{
System.out.print("*");
}
System.out.println();
}
}
}

Output

        *
      * * * 
    * * * * *
  * * * * * * *
* * * * * * * * *
  * * * * * * *
    * * * * *
      * * *
        *

Prev Tutorial Next Tutorial

Advertisements
z
Download Projects

Advertisements

Download WhatsApp Status Video

Buy This Ad Space Only $50 per Month, Ad Size 300X250 Contact on: [email protected] WhatsApp no. 8882538826

Google Adsense Advertisements