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)
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++)
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)
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(" ");
}
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++)
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++)
{
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