Star pattern programs in Java
In this post, you will learn how to print various star pattern programs using the Java programming language. Such a type of logical question is generally asked in interviews or in competitive exams. This can be helpful to improve your logical programming skill and helpful in game development and other logical application development.
A pattern program consists of patterns of numbers, letters, symbols in a particular form. In the programming language, the loop condition is considered important for printing patterns. Generally, nested for loops is used in patterns and shapes printing programs.
Program 1: Java triangle shape star pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 8;
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=i; j++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output of the above code
*
**
***
****
*****
******
*******
********
Program 2: Java triangle shape star pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
// Print i number of stars
for (int i=1; i<=rows; i++)
{
// Print space in decreasing order
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
// Print star in increasing order
for (int k=1; k<=i; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output of the above code:
*
**
***
****
*****
******
*******
Program 3: Java square star pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
//Outer Loop for number of Rows
for(int i=0;i<rows;i++)
{
// Inner loop for printing '*' in each column.
for(int j=0;j<rows;j++)
{
// We add two spaces to match shape of a square.
System.out.print("* ");
}
System.out.println();
}
}
}
Output of the above code:
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Program 4: Java hollow square star pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
// Outer loop
for(int i=0;i<rows;i++)
{
// Inner loop for printing '*' in each column.
for(int j=0;j<rows;j++)
{
if(i==0 || i==rows-1 || j==0 || j==rows-1)
{
System.out.print("* ");
}
else
System.out.print(" ");
}
System.out.println();
}
}
}
Output of the above code:
* * * * * * *
* *
* *
* *
* *
* *
* * * * * * *
Program 5: Java pyramid star pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
for (int i=1; i<=rows; i++)
{
// Print space in decreasing order
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
// Print star in increasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
}
}
}
Output of the above code:
*
***
*****
*******
*********
***********
*************
Program 6: Java pyramid star pattern program in reverse order
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
for (int i=rows; i>=1; i--)
{
// Print star in decreasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
// Print space in increasing order
for (int j=rows; j>=i; j--)
{
System.out.print(" ");
}
}
}
}
Output of the above code:
*************
***********
*********
*******
*****
***
*
Program 7: Java triangle pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
System.out.println();
}
for (int i=1; i<=rows-1; i++)
{
// Print star in decreasing order
for (int j = rows-1; j >= i; j--)
{
System.out.print("*");
}
// Print space in increasing order
for (int k = 1; k < i; k++)
{
System.out.print(" ");
}
System.out.println();
}
}
}
Output of the above code:
*
**
***
****
*****
******
*******
******
*****
****
***
**
*
Program 8: Java rhombus star pattern program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int rows = 7;
for(int i=0;i<rows;i++)
{
// Print Spaces before each row
for(int k=0;k<rows-i;k++)
{
System.out.print(" ");
}
// printing '*' in each column.
for(int j=0;j<rows;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Output of the above code:
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
* * * * * * *
Program 9: Java hollow right angled triangle program
public class Starpattern {
public static void main(String args[]) {
// defining pattern rows
int n = 7;
for(int i=0;i<n;i++)
{
for(int k=i;k<n;k++)
System.out.print(" ");
for(int j=0;j<n;j++)
{
if(j==0 || j==i || i==n-1)
System.out.print(" *");
else
System.out.print(" ");
}
System.out.println();
}
}
}
Output of the above code:
*
* *
* *
* *
* *
* *
* * * * * * *
Related Articles
Number pattern programs in JavaJava program to find area of rectangle
Matrix multiplication in Java
Electricity bill program in Java
Java program to find area of triangle
Area of circle program in Java
Remove duplicate elements from array in Java
Capitalize first letter of each word Java
Convert binary to decimal in Java
Convert decimal to binary in Java
Convert decimal to octal in Java
Convert decimal to hexadecimal in Java
Simple interest program in Java
Check whether the given number is even or odd in java
Print prime numbers from 1 to 100 in Java
Java prime number program
Java program to convert celsius to fahrenheit
Fibonacci series program in Java
Java program to check leap year
Java program to find factorial of a number