SlideShare una empresa de Scribd logo
1 de 66
Descargar para leer sin conexión
35 Star Pattern Programs In Java | Pattern Program
In this article, we will learn to print the different Star Pattern Programs in Java. This is one of the popular
Java pattern program interview question for fresher. The pattern program are the most recommended
programs to enhance the logical thinking and for the better understanding of flow control. Lets look into
the below possible Star Pattern Programs in Java which might help for both the fr
Star Pattern Programs In Java
Star Pattern Programs In Java 1
Star Pattern Programs In Java 3
Star Pattern Programs In Java
Pattern 1:
package com.javainterviewpoint;
import java.util.Scanner;
public class Pattern1
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// 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();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
***
****
*****
Pattern 2:
import java.util.Scanner;
public class Pattern2
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
***
****
*****
Pattern 3:
import java.util.Scanner;
public class Pattern3
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i=1; i<=rows; i++)
{
// Print star in decreasing order
for (int k=rows; k>=i; k--)
{
System.out.print("*");
}
// Print space in increasing order
for (int j=1; j<i; j++)
{
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
****
***
**
*
Pattern 4:
import java.util.Scanner;
public class Pattern4
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i=1; i<=rows; i++)
{
// Print space in increasing order
for (int j=1; j<i; j++)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k=rows; k>=i; k--)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
****
***
**
*
Pattern 5: [ Pyramid Star Pattern ]
import java.util.Scanner;
public class Pattern5
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
***
*****
*******
*********
Pattern 6:
import java.util.Scanner;
public class Pattern6
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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(" ");
}
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*********
*******
*****
***
*
Pattern 7: [Diamond Star Pattern ]
import java.util.Scanner;
public class Pattern7
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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();
}
for (int i=rows-1; i>=1; i--)
{
// Print space in increasing order
for (int j=rows-1; j>=i; j--)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k=1; k<=(i * 2) -1; k++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
***
*****
*******
*********
*******
*****
***
*
Pattern 8:
import java.util.Scanner;
public class Pattern8
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// 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();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
***
****
*****
****
***
**
*
Pattern 9:
import java.util.Scanner;
public class Pattern9
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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();
}
for (int i = 1; i <= rows-1; i++)
{
// Print space in increasing order
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k = rows-1; k >= i; k--)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
***
****
*****
****
***
**
*
Pattern 10:
package com.javainterviewpoint;
import java.util.Scanner;
public class Pattern10
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows-1; j>=i; j--)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k = 1; k <= rows; k++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
*****
*****
*****
*****
Pattern 11:
import java.util.Scanner;
public class Pattern11
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i-1; j++)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k = 1; k <= rows; k++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
*****
*****
*****
*****
Pattern 12:
import java.util.Scanner;
public class Pattern12
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = rows; i >= 1; i--)
{
for (int j = i; j >= 1; j--)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 2; i <= rows; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
****
***
**
*
**
***
****
*****
Pattern 13:
import java.util.Scanner;
public class Pattern13
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = rows-1; i >= 1; i--)
{
for (int j = 2; j <=i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
****
***
**
*
**
***
****
*****
Pattern 14:
import java.util.Scanner;
public class Pattern14
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
System.out.print("* ");
}
System.out.println();
}
for (int i = rows-1; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
System.out.print("* ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
* * * * *
* * * *
* * *
* *
*
* *
* * *
* * * *
* * * * *
Pattern 15:
import java.util.Scanner;
public class Pattern15
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=i; j++)
{
if( j == 1 || j == i || i == rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
* *
* *
*****
Pattern 16:
import java.util.Scanner;
public class Pattern16
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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++)
{
if( k == 1 || k == i || i == rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
* *
* *
*****
Pattern 17:
import java.util.Scanner;
public class Pattern17
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i=1; i<=rows; i++)
{
// Print star in decreasing order
for (int j=rows; j >=i; j--)
{
if( i == 1 || j == i || j == rows)
System.out.print("*");
else
System.out.print(" ");
}
// Print space in increasing order
for (int k=1; k<i; k++)
{
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
* *
* *
**
*
Pattern 18:
import java.util.Scanner;
public class Pattern18
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i=1; i<=rows; i++)
{
// Print space in increasing order
for (int j=1; j<i; j++)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k=rows; k>=i; k--)
{
if( i == 1 || k == i || k == rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
* *
* *
**
*
Pattern 19:
import java.util.Scanner;
public class Pattern19
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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++)
{
if( k == 1 || k == (i * 2) -1 || i == rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
* *
* *
* *
*********
Pattern 20:
import java.util.Scanner;
public class Pattern20
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i=rows; i>=1; i--)
{
// Print star in decreasing order
for (int j=1; j <=(i * 2) -1; j++)
{
if( j == 1 || j == (i * 2) -1 || i == rows)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
// Print space in increasing order
for (int k = rows; k >= i; k--)
{
System.out.print(" ");
}
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*********
* *
* *
* *
*
Pattern 21:
import java.util.Scanner;
public class Pattern21
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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++)
{
if( k == 1 || k == (i * 2) -1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
for (int i=rows-1; i>=1; i--)
{
// Print space in increasing order
for (int j=rows-1; j>=i; j--)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k=1; k<=(i * 2) -1; k++)
{
if( k == 1 || k == (i * 2) -1 )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
* *
* *
* *
* *
* *
* *
* *
*
Pattern 22:
import java.util.Scanner;
public class Pattern22
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=i; j++)
{
if( j == 1 || j == i )
System.out.print("*");
else
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--)
{
if( j == rows-1 || j == i || i == rows)
System.out.print("*");
else
System.out.print(" ");
}
// Print space in increasing order
for (int k = 1; k < i; k++)
{
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
* *
* *
* *
* *
* *
**
*
Pattern 23:
import java.util.Scanner;
public class Pattern23
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
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++)
{
if( k == 1 || k == i )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
for (int i = 1; i <= rows-1; i++)
{
// Print space in increasing order
for (int j = 1; j <= i; j++)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k = rows-1; k >= i; k--)
{
if( k == rows-1 || k == i )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
**
* *
* *
* *
* *
* *
**
*
Pattern 24:
import java.util.Scanner;
public class Pattern24
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = rows-1; j>=i; j--)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k = 1; k <= rows; k++)
{
if( i == 1 || i == rows || k == 1 || k == rows )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
* *
* *
* *
*****
Pattern 25:
import java.util.Scanner;
public class Pattern25
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i-1; j++)
{
System.out.print(" ");
}
// Print star in decreasing order
for (int k = 1; k <= rows; k++)
{
if( i == 1 || i == rows || k == 1 || k == rows )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
* *
* *
* *
*****
Pattern 26:
import java.util.Scanner;
public class Pattern26
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
for (int k = i*2; k <= rows*2-1; k++)
{
System.out.print(" ");
}
for (int l = 1; l <= i; l++)
{
System.out.print("*");
}
System.out.println();
}
for (int i=1; i<=rows-1; i++)
{
for (int j = rows-1; j >= i; j--)
{
System.out.print("*");
}
for (int k = 1; k <= i*2; k++)
{
System.out.print(" ");
}
for (int l = rows-1; l >= i; l--)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
* *
** **
*** ***
**** ****
**********
**** ****
*** ***
** **
* *
Pattern 27:
import java.util.Scanner;
public class Pattern27
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j = i; j <= rows; j++)
{
System.out.print("*");
}
for (int k = 1; k <= i*2-2; k++)
{
System.out.print(" ");
}
for (int l = i; l <= rows; l++)
{
System.out.print("*");
}
System.out.println();
}
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*");
}
for (int k = i*2-2; k < rows*2-2; k++)
{
System.out.print(" ");
}
for (int l = 1; l <= i; l++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
**********
**** ****
*** ***
** **
* *
* *
** **
*** ***
**** ****
**********
Pattern 28:
import java.util.Scanner;
public class Pattern28
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i=1; i<=rows; i++)
{
for (int j=rows; j>i; j--)
{
System.out.print(" ");
}
for (int k=1; k<=(i * 2) -1; k++)
{
if(k == 1 || k == i*2 -1 || k == 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
for (int i=rows-1; i>=1; i--)
{
for (int j=rows-1; j>=i; j--)
{
System.out.print(" ");
}
for (int k=1; k<=(i * 2) -1; k++)
{
if(k == 1 || k == i*2 -1 || k == 1)
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*
* *
* *
* *
* *
* *
* *
* *
*
Pattern 29:
import java.util.Scanner;
public class Pattern29
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
for (int i = 1; i <= rows; i++)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
if( i == 1 || k == i || k == rows)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
for (int i = rows-1; i >= 1; i--)
{
for (int j = 1; j < i; j++)
{
System.out.print(" ");
}
for (int k = i; k <= rows; k++)
{
if( i == 1 || k == i || k == rows)
System.out.print("* ");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
* * * * *
* *
* *
* *
*
* *
* *
* *
* * * * *
Pattern 30:
import java.util.Scanner;
public class Pattern30
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=rows; j++)
{
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Printing the pattern ##
*****
*****
*****
*****
*****
Pattern 31:
import java.util.Scanner;
public class Pattern31
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to print the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=rows; i++)
{
for (int j=1; j<=rows; j++)
{
if(i ==1 || i == rows || j == 1 || j == rows )
System.out.print("*");
else
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to print the pattern
5
## Print star pattern in java ##
*****
* *
* *
* *
*****
Pattern 32:
import java.util.Scanner;
public class Pattern32
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to in the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=(rows * 2 -1); i++)
{
for (int j=1; j<=rows; j++)
{
if(j==i || j==rows-i+1)
{
System.out.print("*");
}
System.out.print(" ");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to in the pattern
5
## Print triangle of stars in java ##
* *
* *
*
* *
* *
Pattern 33:
import java.util.Scanner;
public class Pattern33
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to in the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i=1; i<=(rows * 2 -1); i++)
{
if( i == rows)
{
// Printing Horizontal Line of Stars
for (int j=1; j<=(rows * 2 -1); j++)
{
System.out.print("*");
}
}
else
{
// Printing space before Vertical Line of Stars
for(int k=1; k<= rows-1; k++)
{
System.out.print(" ");
}
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to in the pattern
5
## Printing the pattern ##
*
*
*
*
*********
*
*
*
*
Pattern 34:
import java.util.Scanner;
public class Pattern34
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to in the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for (int i = 1; i <= rows * 2 - 1; i++)
{
if (i == 1 || i == rows || i == rows * 2 - 1)
{
for (int j = 1; j <= rows; j++)
{
if (j == 1 || j == rows)
System.out.print(" ");
else
System.out.print("*");
}
}
else
{
for (int k = 1; k <= rows; k++)
{
if (k == 1 || k == rows)
System.out.print("*");
else
System.out.print(" ");
}
}
System.out.println();
}
scanner.close();
}
}
Output
Enter the number of rows needed to in the pattern
5
## Printing the pattern ##
***
* *
* *
* *
***
* *
* *
* *
***
Pattern 35:
import java.util.Scanner;
public class Pattern35
{
public static void main(String[] args)
{
// Create a new Scanner object
Scanner scanner = new Scanner(System.in);
// Get the number of rows from the user
System.out.println("Enter the number of rows needed to in the pattern ");
int rows = scanner.nextInt();
System.out.println("## Printing the pattern ##");
// Print i number of stars
for(int i=1; i<= rows; i++)
{
if(i%2 != 0)
{
for(int j=1; j<= rows/2+1; j++)
{
System.out.print("* ");
}
}
else
{
for(int j=1; j<= rows/2; j++)
{
System.out.print(" * ");
}
}
System.out.println("");
}
}
}
Output
Enter the number of rows needed to in the pattern
5
## Printing the pattern ##
* * *
* *
* * *
* *
* * *
Filed Under: Java, Java Interview
Tagged With: Diamond pattern, Half Diamond Pattern, Hollow Triangle Pattern, Inverted Pyramid, Java
Star Pattern Program, Pyramid pattern, Right Triangle Pattern, Star Pattern Program Star Pattern,
Triangle Pattern

Más contenido relacionado

La actualidad más candente

Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked ListSayantan Sur
 
Java simple programs
Java simple programsJava simple programs
Java simple programsVEERA RAGAVAN
 
Algoritmos sujei
Algoritmos sujeiAlgoritmos sujei
Algoritmos sujeigersonjack
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2Ankit Gupta
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6fisher.w.y
 
WAP to find out whether the number is prime or not in java
WAP to find out whether the number is prime or not in javaWAP to find out whether the number is prime or not in java
WAP to find out whether the number is prime or not in javaOne97 Communications Limited
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittestFariz Darari
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Fariz Darari
 
Code javascript
Code javascriptCode javascript
Code javascriptRay Ray
 
Import java
Import javaImport java
Import javaheni2121
 
Exceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant BhabadExceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant Bhabadvasant Bhabad
 

La actualidad más candente (19)

R Debugging
R DebuggingR Debugging
R Debugging
 
Stack using Linked List
Stack using Linked ListStack using Linked List
Stack using Linked List
 
C# 6.0 Preview
C# 6.0 PreviewC# 6.0 Preview
C# 6.0 Preview
 
.net progrmming part2
.net progrmming part2.net progrmming part2
.net progrmming part2
 
Java simple programs
Java simple programsJava simple programs
Java simple programs
 
Algoritmos sujei
Algoritmos sujeiAlgoritmos sujei
Algoritmos sujei
 
07. Arrays
07. Arrays07. Arrays
07. Arrays
 
java program assigment -2
java program assigment -2java program assigment -2
java program assigment -2
 
Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6Rootkit on Linux X86 v2.6
Rootkit on Linux X86 v2.6
 
Managing Mocks
Managing MocksManaging Mocks
Managing Mocks
 
WAP to find out whether the number is prime or not in java
WAP to find out whether the number is prime or not in javaWAP to find out whether the number is prime or not in java
WAP to find out whether the number is prime or not in java
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittest
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)
 
C++ L01-Variables
C++ L01-VariablesC++ L01-Variables
C++ L01-Variables
 
Code javascript
Code javascriptCode javascript
Code javascript
 
Import java
Import javaImport java
Import java
 
Data Structure
Data StructureData Structure
Data Structure
 
Lab 3
Lab 3Lab 3
Lab 3
 
Exceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant BhabadExceptions Triggers function in SQL by Vasant Bhabad
Exceptions Triggers function in SQL by Vasant Bhabad
 

Similar a Star pattern programs in java Print Star pattern in java and print triangle of stars in java, print star triangle of java, * pattern in java

Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptxKimVeeL
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1Ankit Gupta
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerAiman Hud
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfanjanacottonmills
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptxKimVeeL
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdfangelsfashion1
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docxKatecate1
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdfanupamfootwear
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docxdorisc7
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docxdavinci54
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfmayorothenguyenhob69
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...Nithin Kumar,VVCE, Mysuru
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfoptokunal1
 

Similar a Star pattern programs in java Print Star pattern in java and print triangle of stars in java, print star triangle of java, * pattern in java (20)

Lab01.pptx
Lab01.pptxLab01.pptx
Lab01.pptx
 
Java Unit 1 Project
Java Unit 1 ProjectJava Unit 1 Project
Java Unit 1 Project
 
Java file
Java fileJava file
Java file
 
java program assigment -1
java program assigment -1java program assigment -1
java program assigment -1
 
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian KomputerKOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
KOLEJ KOMUNITI - Sijil Aplikasi Perisian Komputer
 
Computer programming 2 chapter 1-lesson 2
Computer programming 2  chapter 1-lesson 2Computer programming 2  chapter 1-lesson 2
Computer programming 2 chapter 1-lesson 2
 
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdfMagicSquareTest.java import java.util.Scanner;public class Mag.pdf
MagicSquareTest.java import java.util.Scanner;public class Mag.pdf
 
Lab101.pptx
Lab101.pptxLab101.pptx
Lab101.pptx
 
import java.util.Scanner;public class ArrayOperation {    inp.pdf
import java.util.Scanner;public class ArrayOperation {     inp.pdfimport java.util.Scanner;public class ArrayOperation {     inp.pdf
import java.util.Scanner;public class ArrayOperation {    inp.pdf
 
3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf3.Lesson Plan - Input.pdf.pdf
3.Lesson Plan - Input.pdf.pdf
 
JAVA.pdf
JAVA.pdfJAVA.pdf
JAVA.pdf
 
import java.uti-WPS Office.docx
import java.uti-WPS Office.docximport java.uti-WPS Office.docx
import java.uti-WPS Office.docx
 
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 Problem1 java codeimport java.util.Scanner; Java code to pr.pdf Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
Problem1 java codeimport java.util.Scanner; Java code to pr.pdf
 
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
(JAVA NetBeans) Write a Java program able to perform selection sort-So.docx
 
DS LAB RECORD.docx
DS LAB RECORD.docxDS LAB RECORD.docx
DS LAB RECORD.docx
 
Review Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdfReview Questions for Exam 10182016 1. public class .pdf
Review Questions for Exam 10182016 1. public class .pdf
 
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
VTU Design and Analysis of Algorithms(DAA) Lab Manual by Nithin, VVCE, Mysuru...
 
import java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdfimport java.util.;public class Program{public static void.pdf
import java.util.;public class Program{public static void.pdf
 
programming for Calculator in java
programming for Calculator in javaprogramming for Calculator in java
programming for Calculator in java
 
Java final lab
Java final labJava final lab
Java final lab
 

Último

Webinar on E-Invoicing for Fintech Belgium
Webinar on E-Invoicing for Fintech BelgiumWebinar on E-Invoicing for Fintech Belgium
Webinar on E-Invoicing for Fintech BelgiumFinTech Belgium
 
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdfMASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdfCocity Enterprises
 
fundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docxfundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docxssuserf63bd7
 
cost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxcost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxazadalisthp2020i
 
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsMahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsDeepika Singh
 
Q1 2024 Conference Call Presentation vF.pdf
Q1 2024 Conference Call Presentation vF.pdfQ1 2024 Conference Call Presentation vF.pdf
Q1 2024 Conference Call Presentation vF.pdfAdnet Communications
 
Economics Presentation-2.pdf xxjshshsjsjsjwjw
Economics Presentation-2.pdf xxjshshsjsjsjwjwEconomics Presentation-2.pdf xxjshshsjsjsjwjw
Economics Presentation-2.pdf xxjshshsjsjsjwjwmordockmatt25
 
FOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdf
FOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdfFOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdf
FOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdfCocity Enterprises
 
FE Credit and SMBC Acquisition Case Studies
FE Credit and SMBC Acquisition Case StudiesFE Credit and SMBC Acquisition Case Studies
FE Credit and SMBC Acquisition Case StudiesNghiaPham100
 
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...batoole333
 
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdf
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdfSeeman_Fiintouch_LLP_Newsletter_May-2024.pdf
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdfAshis Kumar Dey
 
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...batoole333
 
QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.
QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.
QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.hyt3577
 
Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024Adnet Communications
 
abortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadh
abortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadhabortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadh
abortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadhsamsungultra782445
 
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize ThemSignificant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them360factors
 
7 tips trading Deriv Accumulator Options
7 tips trading Deriv Accumulator Options7 tips trading Deriv Accumulator Options
7 tips trading Deriv Accumulator OptionsVince Stanzione
 
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...kajalverma014
 
Toronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfToronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfJinJiang6
 

Último (20)

Webinar on E-Invoicing for Fintech Belgium
Webinar on E-Invoicing for Fintech BelgiumWebinar on E-Invoicing for Fintech Belgium
Webinar on E-Invoicing for Fintech Belgium
 
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdfMASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
MASTERING FOREX: STRATEGIES FOR SUCCESS.pdf
 
fundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docxfundamentals of corporate finance 11th canadian edition test bank.docx
fundamentals of corporate finance 11th canadian edition test bank.docx
 
cost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptxcost-volume-profit analysis.ppt(managerial accounting).pptx
cost-volume-profit analysis.ppt(managerial accounting).pptx
 
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot GirlsMahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
Mahendragarh Escorts 🥰 8617370543 Call Girls Offer VIP Hot Girls
 
Q1 2024 Conference Call Presentation vF.pdf
Q1 2024 Conference Call Presentation vF.pdfQ1 2024 Conference Call Presentation vF.pdf
Q1 2024 Conference Call Presentation vF.pdf
 
Economics Presentation-2.pdf xxjshshsjsjsjwjw
Economics Presentation-2.pdf xxjshshsjsjsjwjwEconomics Presentation-2.pdf xxjshshsjsjsjwjw
Economics Presentation-2.pdf xxjshshsjsjsjwjw
 
FOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdf
FOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdfFOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdf
FOREX FUNDAMENTALS: A BEGINNER'S GUIDE.pdf
 
W.D. Gann Theory Complete Information.pdf
W.D. Gann Theory Complete Information.pdfW.D. Gann Theory Complete Information.pdf
W.D. Gann Theory Complete Information.pdf
 
FE Credit and SMBC Acquisition Case Studies
FE Credit and SMBC Acquisition Case StudiesFE Credit and SMBC Acquisition Case Studies
FE Credit and SMBC Acquisition Case Studies
 
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
Certified Kala Jadu, Black magic specialist in Rawalpindi and Bangali Amil ba...
 
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdf
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdfSeeman_Fiintouch_LLP_Newsletter_May-2024.pdf
Seeman_Fiintouch_LLP_Newsletter_May-2024.pdf
 
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
Famous Kala Jadu, Black magic expert in Faisalabad and Kala ilam specialist i...
 
QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.
QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.
QATAR Pills for Abortion -+971*55*85*39*980-in Dubai. Abu Dhabi.
 
Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024Lion One Corporate Presentation May 2024
Lion One Corporate Presentation May 2024
 
abortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadh
abortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadhabortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadh
abortion pills in Jeddah Saudi Arabia (+919707899604)cytotec pills in Riyadh
 
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize ThemSignificant AI Trends for the Financial Industry in 2024 and How to Utilize Them
Significant AI Trends for the Financial Industry in 2024 and How to Utilize Them
 
7 tips trading Deriv Accumulator Options
7 tips trading Deriv Accumulator Options7 tips trading Deriv Accumulator Options
7 tips trading Deriv Accumulator Options
 
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
Female Escorts Service in Hyderabad Starting with 5000/- for Savita Escorts S...
 
Toronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdfToronto dominion bank investor presentation.pdf
Toronto dominion bank investor presentation.pdf
 

Star pattern programs in java Print Star pattern in java and print triangle of stars in java, print star triangle of java, * pattern in java

  • 1. 35 Star Pattern Programs In Java | Pattern Program In this article, we will learn to print the different Star Pattern Programs in Java. This is one of the popular Java pattern program interview question for fresher. The pattern program are the most recommended programs to enhance the logical thinking and for the better understanding of flow control. Lets look into the below possible Star Pattern Programs in Java which might help for both the fr Star Pattern Programs In Java Star Pattern Programs In Java 1 Star Pattern Programs In Java 3 Star Pattern Programs In Java Pattern 1: package com.javainterviewpoint; import java.util.Scanner; public class Pattern1 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##");
  • 2. // 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(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** ***** Pattern 2: import java.util.Scanner; public class Pattern2 {
  • 3. public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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(); } scanner.close(); } } Output
  • 4. Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** ***** Pattern 3: import java.util.Scanner; public class Pattern3 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print star in decreasing order
  • 5. for (int k=rows; k>=i; k--) { System.out.print("*"); } // Print space in increasing order for (int j=1; j<i; j++) { System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** * Pattern 4: import java.util.Scanner;
  • 6. public class Pattern4 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in increasing order for (int j=1; j<i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k=rows; k>=i; k--) { System.out.print("*"); } System.out.println(); } scanner.close(); }
  • 7. } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** * Pattern 5: [ Pyramid Star Pattern ] import java.util.Scanner; public class Pattern5 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++)
  • 8. { // 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(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * *** ***** ******* ********* Pattern 6:
  • 9. import java.util.Scanner; public class Pattern6 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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(" "); }
  • 10. } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ********* ******* ***** *** * Pattern 7: [Diamond Star Pattern ] import java.util.Scanner; public class Pattern7 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern ");
  • 11. int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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(); } for (int i=rows-1; i>=1; i--) { // Print space in increasing order for (int j=rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k=1; k<=(i * 2) -1; k++) { System.out.print("*");
  • 12. } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * *** ***** ******* ********* ******* ***** *** * Pattern 8: import java.util.Scanner; public class Pattern8 { public static void main(String[] args)
  • 13. { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // 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++)
  • 14. { System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** ***** **** *** ** * Pattern 9: import java.util.Scanner;
  • 15. public class Pattern9 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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(); } for (int i = 1; i <= rows-1; i++) {
  • 16. // Print space in increasing order for (int j = 1; j <= i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = rows-1; k >= i; k--) { System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** *** **** ***** **** *** ** *
  • 17. Pattern 10: package com.javainterviewpoint; import java.util.Scanner; public class Pattern10 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { System.out.print("*"); }
  • 18. System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** ***** ***** ***** ***** Pattern 11: import java.util.Scanner; public class Pattern11 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user
  • 19. System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i-1; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** ***** *****
  • 20. ***** ***** Pattern 12: import java.util.Scanner; public class Pattern12 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = rows; i >= 1; i--) { for (int j = i; j >= 1; j--) { System.out.print("*"); } System.out.println(); }
  • 21. for (int i = 2; i <= rows; i++) { for (int j = i; j >= 1; j--) { System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** * ** *** **** ***** Pattern 13:
  • 22. import java.util.Scanner; public class Pattern13 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("*"); } System.out.println(); }
  • 23. for (int i = rows-1; i >= 1; i--) { for (int j = 2; j <=i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** **** *** ** * ** *** ****
  • 24. ***** Pattern 14: import java.util.Scanner; public class Pattern14 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("* ");
  • 25. } System.out.println(); } for (int i = rows-1; i >= 1; i--) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { System.out.print("* "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * *
  • 26. * * * * * * * * * * * * * * Pattern 15: import java.util.Scanner; public class Pattern15 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=i; j++) { if( j == 1 || j == i || i == rows)
  • 27. System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * ***** Pattern 16: import java.util.Scanner; public class Pattern16 { public static void main(String[] args) {
  • 28. // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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++) { if( k == 1 || k == i || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } }
  • 29. Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * ***** Pattern 17: import java.util.Scanner; public class Pattern17 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##");
  • 30. for (int i=1; i<=rows; i++) { // Print star in decreasing order for (int j=rows; j >=i; j--) { if( i == 1 || j == i || j == rows) System.out.print("*"); else System.out.print(" "); } // Print space in increasing order for (int k=1; k<i; k++) { System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * *
  • 31. ** * Pattern 18: import java.util.Scanner; public class Pattern18 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in increasing order for (int j=1; j<i; j++) { System.out.print(" "); } // Print star in decreasing order
  • 32. for (int k=rows; k>=i; k--) { if( i == 1 || k == i || k == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * ** * Pattern 19: import java.util.Scanner; public class Pattern19
  • 33. { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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++) { if( k == 1 || k == (i * 2) -1 || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); }
  • 34. scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * ********* Pattern 20: import java.util.Scanner; public class Pattern20 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern ");
  • 35. int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=rows; i>=1; i--) { // Print star in decreasing order for (int j=1; j <=(i * 2) -1; j++) { if( j == 1 || j == (i * 2) -1 || i == rows) System.out.print("*"); else System.out.print(" "); } System.out.println(); // Print space in increasing order for (int k = rows; k >= i; k--) { System.out.print(" "); } } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5
  • 36. ## Printing the pattern ## ********* * * * * * * * Pattern 21: import java.util.Scanner; public class Pattern21 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { // Print space in decreasing order for (int j=rows; j>i; j--)
  • 37. { System.out.print(" "); } // Print star in increasing order for (int k=1; k<=(i * 2) -1; k++) { if( k == 1 || k == (i * 2) -1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i=rows-1; i>=1; i--) { // Print space in increasing order for (int j=rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k=1; k<=(i * 2) -1; k++) { if( k == 1 || k == (i * 2) -1 ) System.out.print("*"); else System.out.print(" "); }
  • 38. System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * * * Pattern 22: import java.util.Scanner; public class Pattern22 { public static void main(String[] args)
  • 39. { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=i; j++) { if( j == 1 || j == i ) System.out.print("*"); else 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--) { if( j == rows-1 || j == i || i == rows)
  • 40. System.out.print("*"); else System.out.print(" "); } // Print space in increasing order for (int k = 1; k < i; k++) { System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * * * * * * * ** *
  • 41. Pattern 23: import java.util.Scanner; public class Pattern23 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); 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++) {
  • 42. if( k == 1 || k == i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i = 1; i <= rows-1; i++) { // Print space in increasing order for (int j = 1; j <= i; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = rows-1; k >= i; k--) { if( k == rows-1 || k == i ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output
  • 43. Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * ** * * * * * * * * * * ** * Pattern 24: import java.util.Scanner; public class Pattern24 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt();
  • 44. System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = rows-1; j>=i; j--) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++) { if( i == 1 || i == rows || k == 1 || k == rows ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * *
  • 45. * * ***** Pattern 25: import java.util.Scanner; public class Pattern25 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i-1; j++) { System.out.print(" "); } // Print star in decreasing order for (int k = 1; k <= rows; k++)
  • 46. { if( i == 1 || i == rows || k == 1 || k == rows ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** * * * * * * ***** Pattern 26: import java.util.Scanner; public class Pattern26 {
  • 47. public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int k = i*2; k <= rows*2-1; k++) { System.out.print(" "); } for (int l = 1; l <= i; l++) { System.out.print("*"); } System.out.println(); }
  • 48. for (int i=1; i<=rows-1; i++) { for (int j = rows-1; j >= i; j--) { System.out.print("*"); } for (int k = 1; k <= i*2; k++) { System.out.print(" "); } for (int l = rows-1; l >= i; l--) { System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * ** ** *** *** **** ****
  • 49. ********** **** **** *** *** ** ** * * Pattern 27: import java.util.Scanner; public class Pattern27 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j = i; j <= rows; j++) {
  • 50. System.out.print("*"); } for (int k = 1; k <= i*2-2; k++) { System.out.print(" "); } for (int l = i; l <= rows; l++) { System.out.print("*"); } System.out.println(); } for (int i = 1; i <= rows; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } for (int k = i*2-2; k < rows*2-2; k++) { System.out.print(" "); } for (int l = 1; l <= i; l++) { System.out.print("*"); } System.out.println();
  • 51. } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ********** **** **** *** *** ** ** * * * * ** ** *** *** **** **** ********** Pattern 28: import java.util.Scanner; public class Pattern28 { public static void main(String[] args) {
  • 52. // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i=1; i<=rows; i++) { for (int j=rows; j>i; j--) { System.out.print(" "); } for (int k=1; k<=(i * 2) -1; k++) { if(k == 1 || k == i*2 -1 || k == 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } for (int i=rows-1; i>=1; i--) { for (int j=rows-1; j>=i; j--) { System.out.print(" ");
  • 53. } for (int k=1; k<=(i * 2) -1; k++) { if(k == 1 || k == i*2 -1 || k == 1) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * * * * Pattern 29:
  • 54. import java.util.Scanner; public class Pattern29 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); for (int i = 1; i <= rows; i++) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { if( i == 1 || k == i || k == rows) System.out.print("* "); else
  • 55. System.out.print(" "); } System.out.println(); } for (int i = rows-1; i >= 1; i--) { for (int j = 1; j < i; j++) { System.out.print(" "); } for (int k = i; k <= rows; k++) { if( i == 1 || k == i || k == rows) System.out.print("* "); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## * * * * *
  • 56. * * * * * * * * * * * * * * * * * * Pattern 30: import java.util.Scanner; public class Pattern30 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++)
  • 57. { for (int j=1; j<=rows; j++) { System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5 ## Printing the pattern ## ***** ***** ***** ***** ***** Pattern 31: import java.util.Scanner; public class Pattern31 { public static void main(String[] args)
  • 58. { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to print the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=rows; i++) { for (int j=1; j<=rows; j++) { if(i ==1 || i == rows || j == 1 || j == rows ) System.out.print("*"); else System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to print the pattern 5
  • 59. ## Print star pattern in java ## ***** * * * * * * ***** Pattern 32: import java.util.Scanner; public class Pattern32 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=(rows * 2 -1); i++) { for (int j=1; j<=rows; j++)
  • 60. { if(j==i || j==rows-i+1) { System.out.print("*"); } System.out.print(" "); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to in the pattern 5 ## Print triangle of stars in java ## * * * * * * * * * Pattern 33: import java.util.Scanner;
  • 61. public class Pattern33 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i=1; i<=(rows * 2 -1); i++) { if( i == rows) { // Printing Horizontal Line of Stars for (int j=1; j<=(rows * 2 -1); j++) { System.out.print("*"); } } else { // Printing space before Vertical Line of Stars for(int k=1; k<= rows-1; k++) {
  • 62. System.out.print(" "); } System.out.print("*"); } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## * * * * ********* * * * * Pattern 34: import java.util.Scanner;
  • 63. public class Pattern34 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for (int i = 1; i <= rows * 2 - 1; i++) { if (i == 1 || i == rows || i == rows * 2 - 1) { for (int j = 1; j <= rows; j++) { if (j == 1 || j == rows) System.out.print(" "); else System.out.print("*"); } } else { for (int k = 1; k <= rows; k++)
  • 64. { if (k == 1 || k == rows) System.out.print("*"); else System.out.print(" "); } } System.out.println(); } scanner.close(); } } Output Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## *** * * * * * * *** * * * * * * *** Pattern 35:
  • 65. import java.util.Scanner; public class Pattern35 { public static void main(String[] args) { // Create a new Scanner object Scanner scanner = new Scanner(System.in); // Get the number of rows from the user System.out.println("Enter the number of rows needed to in the pattern "); int rows = scanner.nextInt(); System.out.println("## Printing the pattern ##"); // Print i number of stars for(int i=1; i<= rows; i++) { if(i%2 != 0) { for(int j=1; j<= rows/2+1; j++) { System.out.print("* "); } } else {
  • 66. for(int j=1; j<= rows/2; j++) { System.out.print(" * "); } } System.out.println(""); } } } Output Enter the number of rows needed to in the pattern 5 ## Printing the pattern ## * * * * * * * * * * * * * Filed Under: Java, Java Interview Tagged With: Diamond pattern, Half Diamond Pattern, Hollow Triangle Pattern, Inverted Pyramid, Java Star Pattern Program, Pyramid pattern, Right Triangle Pattern, Star Pattern Program Star Pattern, Triangle Pattern