Print Star Pattern in Java

Advertisements

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

How to Make Triangle 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

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

If you are looking for a fresh looking website that represents your business the way you want, translates effectively the core messages of your service/product, but that won't cost you the world; We are your one stop solution. Get to the best web development company in pune, Not only we are able to deliver conceptually and visually, but our team is easy to work with you too, which for this type of projects is really important.


Prev Tutorial Next Tutorial

Advertisements

Buy This Ad Space @$20 per Month, Ad Size 600X200 Contact on: hitesh.xc@gmail.com or 9999595223

Pure VPN Privide Lowest Price VPN Just @ $1.65. Per Month with Non Detected IP Lowest Price Non Detected IP VPN

Magenet is best Adsense Alternative here we earn $2 for single link, Here we get links ads. Magenet

Download Projects

Google Advertisements


Buy Websites 9999595223

Buy College Projects with Documentation Contact on whatsapp 9999595223. Contact on: hitesh.xc@gmail.com or 9999595223 Try this Keyword C++ Programs

Advertisements