25 Pattern Programs in Java
Star Patterns in Java
Pattern 1
/*Star Pattern 1
*
**
***
****
* * * * * */
package Patterns;
public class Star {
public static void main(String[] args) {
int rows = 5;
for (int i = 1; i <= rows; ++i) { //Outer loop for rows
for (int j = 1; j <= i; ++j) { //Inner loop for Col
[Link]("* "); //Print *
[Link](); //New line
}
Pattern 2
/*Star Pattern 2
*****
****
***
**
* */
package Patterns;
public class Star {
public static void main(String[] args) {
int rows = 5;
for(int i = rows; i >= 1; --i) { //For Loop for Row
for(int j = 1; j <= i; ++j) { //For Loop for Col
[Link]("* "); //Prints *
[Link](); //Get to newline
}
Pattern 3
/*Star Pattern 3
**
***
****
*****
****
***
**
* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]); //Input
[Link]("Enter the number of rows: ");
int rows = [Link]();
for (int i = 0; i <= rows - 1; i++) { //For Loop for Row
for (int j = 0; j <= i; j++) { //For Loop for Col
[Link]("*" + " "); //prints * and blank space
[Link](""); // new line
}
for (int i = rows - 1; i >= 0; i--) { //For Loop for Row
for (int j = 0; j <= i - 1; j++) { //For Loop for Col
[Link]("*" + " "); //prints * and blank space
[Link]("");// new line
[Link]();
Pattern 4
/*Star Pattern 4
**
***
****
* * * * * */
package Patterns;
public class Star {
public static void printStars(int n) {
int i, j;
for (i = 0; i < n; i++) {
for (j = 2 * (n - i); j >= 0; j--) { //For Loop for Row
[Link](" "); // Print Spaces
for (j = 0; j <= i; j++) { //For Loop for col
[Link]("* "); // Print Star
}
[Link]();
public static void main(String args[]) {
int n = 5; //Number of Rows
printStars(n);
Pattern 5
/*Star Pattern 5
*****
****
***
**
* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner S = new Scanner([Link]); //Input
[Link]("Enter row value ");
int r = [Link]();
for (int i = r; i >= 1; i--) {
for (int j = r; j > i; j--) {
[Link](" "); // Prints Blank space
for (int k = 1; k <= i; k++) {
[Link]("*"); //Prints *
}
[Link](" "); //Prints blank spaces
[Link]();
Pattern 6
/*Star Pattern 6
**
***
****
*****
****
***
**
* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int rows = [Link]();
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) { //Rows Loop
[Link](" "); // Blank Space
}
for (int k = 1; k <= i; k++) { //Cols Loop
[Link]("*"); // Prints *
[Link]("");
for (int i = rows; i >= 1; i--) {
for (int j = i; j <= rows; j++) { //Rows Loop
[Link](" "); // Prints blank spaces
for (int k = 1; k < i; k++) { //Col Loop
[Link]("*"); // Prints *
[Link](""); // New Line1
[Link]();
}
}
Pattern 7
/*Star Pattern 7
**
***
****
***** */
package Patterns;
public class Star {
public static void printTriagle(int n) {
for (int i = 0; i < n; i++) {
for (int j = n - i; j > 1; j--) { //Loop for blank space
[Link](" "); //Print Space
for (int j = 0; j <= i; j++) { loop for star
[Link]("* "); //Print Star
[Link](); //Newline
public static void main(String args[]) {
int n = 5;
printTriagle(n);
}
}
Pattern 8
/*Star Pattern 8
*****
****
***
**
* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int rows = [Link]();
for (int i = 0; i <= rows - 1; i++) { //For loop for Rows
for (int j = 0; j <= i; j++) { //For loop for Col
[Link](" "); // blank space
for (int k = 0; k <= rows - 1 - i; k++) {
[Link]("*" + " "); // prints * and blank space
[Link](); //Next line
[Link]();
}
Pattern 9
/*Star Pattern 9
***
*****
*******
*********
*******
*****
***
* */
package Patterns;
import [Link];
public class Star {
public static void main(String args[]) {
int n, x, j, blank = 1;
[Link]("Enter the value for rows: ");
Scanner s = new Scanner([Link]);
n = [Link](); //input
blank = n - 1; // logic for balck spaces
//Upper star Pyramid
for (j = 1; j <= n; j++) {
for (x = 1; x <= blank; x++) {
[Link](" "); //print blank space
blank--;
for (x = 1; x <= 2 * j - 1; x++) {
[Link]("*"); //Print Star
[Link]("");
//Lower star Pyramid
blank = 1;
for (j = 1; j <= n - 1; j++) {
for (x = 1; x <= blank; x++) {
[Link](" "); //Print Spaces
blank++;
for (x = 1; x <= 2 * (n - j) - 1; x++) {
[Link]("*"); //Print Star
[Link](""); //Print new line
}
Pattern 10
/*Star Pattern 10
*****
****
***
**
**
***
****
* * * * * */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int rows = [Link](); //Input
//Upper Inverted Pyramid
for (int i = 0; i <= rows - 1; i++) {
for (int j = 0; j < i; j++) {
[Link](" "); Print blank space
for (int k = i; k <= rows - 1; k++) {
[Link]("*" + " "); //Print star and blank space
[Link](""); //New line
//For lower Pyramid
for (int i = rows - 1; i >= 0; i--) {
for (int j = 0; j < i; j++) {
[Link](" "); //Print spaces
for (int k = i; k <= rows - 1; k++) {
[Link]("*" + " "); //Print Star and Space
}
[Link](""); //Print New line
[Link]();
}
Pattern 11
/*Diagonal 11
*
*
* */
package Patterns;
public class Star {
public static void main(String[] args) {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 0; j < 5 - i; j++) {
[Link](" "); //Print blank space
[Link]("*"); //Print Star and newline
}
X-pattern
It creates a diagonal pattern of X characters. This pattern can be created
with a nested loop that prints X characters in specific positions based on the
row and column numbers.
Pattern 12
/*X Pattern 12
* *
* *
**
**
* *
* * */
package Patterns;
import [Link].*;
public class Star {
public static void main(String args[]) {
int i, j, n;
Scanner sc = new Scanner([Link]);
[Link]("Enter a value for n");
n = [Link](); //Input
//Upper V pattern
for (i = n; i >= 1; i--) {
for (j = i; j < n; j++) {
[Link](" ");//print spaces
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1))//Logic for printing star
[Link]("*");
else
[Link](" ");//if logic fails print space
[Link]("");
//Lower Inverted V pattern
for (i = 2; i <= n; i++) {
for (j = i; j < n; j++) {
[Link](" ");//Print spaces
for (j = 1; j <= (2 * i - 1); j++) {
if (j == 1 || j == (2 * i - 1))//Logic for printing star
[Link]("*");
else
[Link](" ");//if logic fails print space
[Link]("");
}
Pattern 13
/*Inverted V 13
**
* *
* *
* * */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner cs = new Scanner([Link]); //Input
[Link]("Enter the row size:");
int out, in;
int row_size = [Link]();
int print_control_x = row_size;
int print_control_y = row_size;
for (out = 1; out <= row_size; out++) {
for (in = 1; in <= row_size * 2; in++) {
if (in == print_control_x || in == print_control_y) {
[Link]("*");
} else {
[Link](" ");
}
print_control_x--;
print_control_y++;
[Link]();
[Link]();
Pattern 14
/* V-pattern
* *
* *
* *
**
* */
package Patterns;
public class Star {
static void pattern(int n) {
int i, j;
for (i = n - 1; i >= 0; i--) {
for (j = n - 1; j > i; j--) {
[Link](" "); //Print Space
[Link]("*"); //Print star
for (j = 1; j < (i * 2); j++)
[Link](" ");//Print space
if (i >= 1)
[Link]("*");
[Link]("");//Enter newline
public static void main(String args[]) {
int n = 5;
pattern(n); //Pattern method call
}
Pattern 15
/*Rombus 15
*
**
* *
* *
* *
* *
* *
**
* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
[Link]("Number of rows: ");
int rows = extracted().nextInt();
int i;
//upper inverted V pattern
for (i = 1; i <= rows; i++) {
for (int j = rows; j > i; j--) {
[Link](" "); //Print spaces
[Link]("*"); //Print Spaces
for (int k = 1; k < 2 * (i - 1); k++) { /Logic for pattern
[Link](" "); //Print Spaces
if (i == 1) {
[Link](""); //Condition true, go to newline
} else {
[Link]("*"); //else print star
//Lower v pattern
for (i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
[Link](" "); //Print Spaces
}
[Link]("*");
for (int k = 1; k < 2 * (i - 1); k++) { Logic for pattern
[Link](" ");
if (i == 1)
[Link](""); //newline
else
[Link]("*"); //Print star
private static Scanner extracted() {
return new Scanner([Link]);
}
Pattern 16
/*Star Pattern 16
**
* *
* *
********* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int rows = [Link]();
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
[Link](" ");
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || i == rows || k == (2 * i - 1)) {
//Logic for printing Pattern
[Link]("*"); //Print Star
} else {
[Link](" "); //Print Spaces
}
}
[Link]("");
[Link]();
Pattern 17
/*Star Pattern 17
*********
* *
* *
**
* */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter the number of rows: ");
int rows = [Link](); //Row input
for (int i = rows; i >= 1; i--) {
for (int j = i; j < rows; j++) {
[Link](" "); //Print Spaces
for (int k = 1; k <= (2 * i - 1); k++) {
if (k == 1 || i == rows || k == (2 * i - 1)) { //logic to print Pattern
[Link]("*"); //Ture prints star
} else {
[Link](" "); //False prints spaces
[Link]("");
[Link]();
}
Pattern 18
/*Box 18
**********
* *
* *
* *
* *
* *
* *
* *
* *
********** */
package Patterns;
public class Star {
static void print_rectangle(int n, int m) {
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= m; j++) {
if (i == 1 || i == n || j == 1 || j == m) //Logic to print
[Link]("*"); //Tue?, print star
else
[Link](" "); //Tue?, print space
[Link]();
public static void main(String args[]) {
int rows = 10, columns = 10;
print_rectangle(rows, columns); //Method call
}
Pattern 19
12
123
1234
1 2 3 4 5 */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]); //Input
[Link]("Number of rows: ");
int rows = [Link]();
for (int i = 1; i <= rows; i++) {
for (int j = 1; j <= i; j++) {
[Link](j + " "); //Print j value and space
[Link]();
[Link]();
}
Floyd's Triangle Pattern
This pattern is created by printing a triangle of numbers, starting from 1 and
incrementing the value by 1 for each row. However, the numbers are printed
in a specific order, as shown in the example below:
Pattern 20
/*Number Pattern 20 (Floyd's Triangle)
1
23
456
7 8 9 10
11 12 13 14 15 */
package Patterns;
public class Star {
public static void main(String[] args) {
int i, j, k = 1;
for (i = 1; i <= 5; i++) {
for (j = 1; j < i + 1; j++) {
[Link](k++ + " "); /Floyd’s triangle logic(prints K value and
increments on every iteration)
[Link]();
}
Pascal's Triangle
It involves creating a triangular pattern of numbers that follow a specific
mathematical rule. This pattern can be created with a nested loop that
calculates the value of each number based on its position in the triangle.
Pattern 21
/*Number Pattern 21 (Pascal's Triangle)
1 1
1 2 1
1 3 3 1
1 4 6 4 1
1 5 10 10 5 1 */
package Patterns;
public class Star {
public static void main(String[] args) {
int x = 6;
for (int i = 0; i < x; i++) {
int num = 1;
[Link]("%" + (x - i) * 2 + "s", ""); //Pascals triangle logic
for (int j = 0; j <= i; j++) {
[Link]("%4d", num);
num = num * (i - j) / (j + 1);
[Link]();
}
}
It is another type of Java pattern program that involves printing numbers in a
specific sequence or arrangement. These programs can be used to create
tables, graphs, and other types of visual displays.
In Java, creating numeric patterns involves using loops for controlling the
number of rows and columns and the value of the numbers printed. The
program can be customized to create patterns, including multiplication
tables, Fibonacci sequences, and more complex designs.
Pattern 22
/*Number Pattern 22
10101
01010
10101
01010
10101 */
package Patterns;
import [Link];
public class Star {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Number of rows: ");
int rows = [Link](); //Input
for (int i = 1; i <= rows; i++) {
int num;
if (i % 2 == 0) {
num = 0;
for (int j = 1; j <= rows; j++) {
[Link](num);
num = (num == 0) ? 1 : 0;
} else {
num = 1;
for (int j = 1; j <= rows; j++) {
[Link](num);
num = (num == 0) ? 1 : 0;
[Link]();
[Link]();
}
Pattern 23
/*Ruby Pattern 23
BB
C C
D D
C C
BB
A */
package Patterns;
import [Link];
public class Ruby {
public static void main(String[] args) {
char[] alpha = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S',
'T', 'U', 'V', 'W', 'X', 'Y', 'Z' };
int digit = 0;
String[] ruby = new String[26];
[Link]("Input Uppercase alphabet between A to Z:");
Scanner reader = new Scanner([Link]);
try {
char aplhabet = [Link]("[A-Z]").charAt(0);
for (int i = 0; i < [Link]; i++) {
if (alpha[i] == aplhabet) {
digit = i;
break;
for (int i = 0; i <= digit; i++) {
ruby[i] = "";
for (int p = 0; p < digit - i; p++) {
ruby[i] += " ";
ruby[i] += alpha[i];
if (alpha[i] != 'A') {
for (int p = 0; p < 2 * i - 1; p++) {
ruby[i] += " ";
ruby[i] += alpha[i];
[Link](ruby[i]);
for (int i = digit - 1; i >= 0; i--) {
[Link](ruby[i]);
} catch (Exception e) { //Exception
[Link]();
} finally {
[Link]();
}
Pattern 24
/*Alphabet Pattern 24
A
AB
ABC
ABCD
ABCDE
A B C D E F */
package Patterns;
public class Alphabet {
public static void main(String[] args) {
int alphabet = 65; //ASCII value of “A”
for (int i = 0; i <= 5; i++) {
for (int j = 0; j <= i; j++) {
[Link]((char) (alphabet + j) + " "); //Logic to print Alphabet pattern
[Link]();
}
Pattern 25
/*Alphabet Pattern 25
AB
ABC
ABCD
A B C D E */
package Patterns;
public class Alphabet {
public static void main(String[] args) {
for (int i = 0; i <= 4; i++) {
int alphabet = 65; //ASCII value of “A”
for (int j = 4; j > i; j--) {
[Link](" "); //Print Space
for (int k = 0; k <= i; k++) {
[Link]((char) (alphabet + k) + " "); //Print Alphabet
[Link]();