Pattern programs are an excellent way to improve logic and loop concepts in programming languages like Java. Patterns can be simple, like triangles and squares, to more complex structures, like diamonds and Pascal’s triangles. In this article, we will explore the Pattern program in Java language.
Introduction to Pattern Programs in Java
Pattern programs in Java are generally solved using nested loops, most of which we used for loops because of their convenience. Sometimes, developers also use the while and do-while loop.
The Pattern program can be divided into 3 basic categories:
Star Patterns
Number Patterns
Character Patterns
Get curriculum highlights, career paths, industry insights and accelerate your technology journey.
Download brochure
Star Pattern Programs in Java
The start pattern generally prints the * (asterisk)in different shapes and forms. These patterns mostly consist of a mixture of squares, rectangles, and triangles. Let’s dive deep into the star pattern.
1. Triangle Star Pattern
The following program demonstrates the Triangle star pattern:
Program
// Java Program to print
// Triangular Pattern
import java.util.*;
class StarPattern {
public static void printPattern(int n)
{
int i, j;
// outer loop to handle rows
for (i = 0; i < n; i++) { // inner loop to print spaces. for (j = n – i; j > 1; j–) {
System.out.print(” “);
}
// inner loop to print stars.
for (j = 0; j <= i; j++) {
System.out.print(“* “);
}
// printing new line for each row
System.out.println();
}
}
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
2. Square Grid of Stars
The following program demonstrates the square grid:
Program
public class SquareGrid{
public static void main(String args[])
{
int n = 5 ;
// i loop : iterating n times
for (int i = 1; i <= n; i++)
{
// j loop : iterating n times
for (int j = 1; j <= n; j++)
{
// Printing the stars
System.out.print(“*”);
}
// Leaving the line after the ‘j’ loop
System.out.println();
}
}
}
Output
*****
*****
*****
*****
*****
3. Inverted Right Angled Triangle of * (left)
The following program demonstrates the Right Angled Triangle:
Program
public class RightAngled{
public static void main(String args[]){
int n = 10 ;
for (int i = 1; i <= n; i++)
{
// j loop : iterating (n-i+1) times
for (int j = 1; j <= n-i+1; j++)
{
// Printing the stars
System.out.print(“*”);
}
System.out.println();
}
}
}
The following program demonstrates the Inverted Valley:
Program
import java.util.*;
public class InvertedValley
{
public static void main(String args[]){
int n = 10 ;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= n – i + 1; j++)
{
System.out.print(“*”);
}
for (int j = 1; j <= i – 1; j++)
{
System.out.print(” ” + ” “);
}
for (int j = 1; j <= n – i + 1; j++)
{
System.out.print(“*”);
}
System.out.println();
}
}
}
The following program demonstrates the Diamond Star Pattern:
Program
import java.util.*;
public class DiamondStarPattern {
public static void printPattern(int n)
{
int i, j;
int num = 1;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n – i; j++) {
System.out.print(” “);
}
for (j = 1; j <= 2 * i – 1; j++) { System.out.print(“*”); } System.out.println(); } for (i = n-1; i >= 1; i–) {
for (j = 1; j <= n – i; j++) {
System.out.print(” “);
}
for (j = 1; j <= 2 * i – 1; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
The following program demonstrates the Hollow Diamond:
Program
import java.util.*;
public class HollowDiamond {
public static void printPattern(int n){
int i, j;
int num = 1;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n – i; j++) {
System.out.print(” “);
}
for (j = 1; j <= 2 * i – 1; j++) { if (j == 1 || j == 2*i-1) System.out.print(“*”); else System.out.print(” “); } System.out.println(); } for (i = n-1; i >= 1; i–) {
for (j = 1; j <= n – i; j++) {
System.out.print(” “);
}
for (j = 1; j <= 2 * i – 1; j++) {
if (j == 1 || j == 2*i-1)
System.out.print(“*”);
else
System.out.print(” “);
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
*
* *
* *
* *
* *
* *
* *
* *
* *
* *
*
9. Rhombus Pattern
The following program demonstrates the Rhombus Pattern:
Program
public class RhombusPattern {
public static void printPattern(int n)
{
int i, j;
int num = 1;
for (i = 1; i <= n; i++) {
for (j = 1; j <= n – i; j++) {
System.out.print(” “);
}
for (j = 1; j <= n; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
******
******
******
******
******
******
10. Hollow Hourglass Pattern
The following program demonstrates the Hollow Hourglass Pattern:
Program
// Java Program to print pattern
// Hollow Hourglass Pattern
import java.util.*;
public class HourGlass {
public static void printPattern(int n)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j < i; j++) {
System.out.print(” “);
}
for (j = i; j <= n; j++) { if(j==i||j==n||i==1) System.out.print(“* “); else System.out.print(” “); } System.out.println(); } for (i = n – 1; i >= 1; i–) {
for (j = 1; j < i; j++) {
System.out.print(” “);
}
for (j = i; j <= n; j++) {
if(j==i||j==n||i==1)
System.out.print(“* “);
else
System.out.print(” “);
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 10;
printPattern(n);
}
}
The following program demonstrates the K Pattern in Java programming language:
Program
// Java Program to print pattern
// Reverse Right Half Pyramid
import java.util.*;
public class K_Pattern {
public static void printPattern(int n)
{
int i, j;
for (i = n; i >= 1; i–) {
for (j = 1; j <= i; j++) {
System.out.print(“*”);
}
// printing new line for each row
System.out.println();
}
// outer loop to handle rows
for (i = 2; i <= n; i++) {
// inner loop to handle columns
for (j = 1; j <= i; j++) {
System.out.print(“*”);
}
// printing new line for each row
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
The following program demonstrates the Reverse Triangle Pattern:
Program
public class ReverseTriangle {
public static void printPattern(int n)
{
int i, j, k;
for (i = n; i >= 1; i–) {
// inner loop to print spaces.
for (j = i; j < n; j++) {
System.out.print(” “);
}
for (k = 1; k <= (2 * i – 1); k++) {
// printing stars.
if (k == 1 || i == n || k == (2 * i – 1)) {
System.out.print(“*”);
}
// printing spaces.
else {
System.out.print(” “);
}
}
System.out.println(“”);
}
}
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
***********
* *
* *
* *
* *
*
13. Triangle Star Pattern
The following program demonstrates the triangle star pattern:
Program
import java.util.*;
public class TriangleStarPattern {
public static void printPattern(int n)
{
int i, j;
for (i = 0; i < n; i++) { for (j = n – i; j > 1; j–) {
System.out.print(” “);
}
for (j = 0; j <= i; j++) {
System.out.print(“* “);
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
*
* *
* * *
* * * *
* * * * *
* * * * * *
14. Reverse Right Half Pyramid Pattern
The following program demonstrates the Right Half Pyramid Pattern:
Program
// This code written by Neeraj Kumar
import java.util.*;
public class ReverseRightHalf {
public static void printPattern(int n){
int i, j;
for (i = n; i >= 1; i–) {
for (j = 1; j <= i; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 10;
printPattern(n);
}
}
The following program demonstrates the Square Fill Pattern:
Program
import java.util.*;
public class SquareFillPattern {
public static void printPattern(int n){
int i, j;
for (i = 0; i <= n; i++) {
for (j = 0; j <= n; j++) {
System.out.print(“*”);
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 10;
printPattern(n);
}
}
In this section, we will see the Number patterns in Java language. We are given a pattern that has some number series in it, and we need to print it in a specific way. We have to replace * with the number. Let’s see one by one each number pattern.
Grid of numbers (Increasing row-wise)
The following program demonstrates the Grid of numbers:
Program
public class GridNumbers
{
public static void main(String args[])
{
int n = 5 ;
for (int i = 1; i <= n; i++)
{
for (int j = 1; j <= n; j++)
{
System.out.print(i);
}
System.out.println();
}
}
}
Output
11111
22222
33333
44444
55555
17. Right Angled Triangle (Increasing column-wise)
The following program demonstrates the Right Angled Triangle:
Program
import java.util.*;
public class RightAngledTriangle{
public static void main(String args[]) {
int n = 5 ;
for (int i = 1; i <= n; i++){
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println();
}
}
}
Output
1
12
123
1234
12345
18. Palindrome Triangle Pattern
The following program demonstrates the Palindrome Triangle Pattern in Java language:
Program
// Java Program to print pattern
// Palindrome triangle
import java.util.*;
public class PalindromeTriangle {
public static void printPattern(int n)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= 2 * (n – i); j++) { System.out.print(” “); } for (j = i; j >= 1; j–) {
System.out.print(j + ” “);
}
for (j = 2; j <= i; j++) {
System.out.print(j + ” “);
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
The following program demonstrates the Number-increasing Pyramid Pattern:
Program
// Java Program to print pattern
// Number-increasing pyramid
import java.util.*;
public class NumberIncreasing {
public static void printPattern(int n)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
System.out.print(j + ” “);
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
1 2 3 4 5 6
20. Number Changing Pyramid Pattern
The following program demonstrates the Pyramid Pattern:
Program
// Java Program to print pattern
// Number-changing pyramid
import java.util.*;
public class ChangingPyramid {
public static void printPattern(int n)
{
int i, j;
int num = 1;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
System.out.print(num + ” “);
num++;
}
System.out.println();
}
}
// Driver Function
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
The following program demonstrates the Zero-One Triangle Triangle:
Program
public class ZeroOne {
public static void printPattern(int n)
{
int i, j;
for (i = 1; i <= n; i++) {
for (j = 1; j <= i; j++) {
if ((i + j) % 2 == 0) {
System.out.print(1 + ” “);
}
else {
System.out.print(0 + ” “);
}
}
System.out.println();
}
}
public static void main(String args[])
{
int n = 6;
printPattern(n);
}
}
Output
1
0 1
1 0 1
0 1 0 1
1 0 1 0 1
0 1 0 1 0 1
22. Character Pattern Programs in Java
Character Pattern Programs in Java are similar to Java star pattern Programs. However, they have sequence-like Number Pattern Programs in the Java language.
A simple character can be printed in the alphabet in Uppercase. Just knowing the ASCII code of ‘A’ and iterating till ‘Z’.
ASCII codes:
A-Z (Upper case): 65 to 90
A-z (Lower case): 97 to 122.
‘’ (blank space):32
The following program demonstrates the convert ASCII to characters
Program
public class CharactersPrint {
public static void main(String args[])
{
for(int i = 0; i < 26; i++)
System.out.print((char)(i+65));
}
}
In this article, we have learned about the Java Patterns programs. Java Patterns programs are a great way to learn and practice. It helps developers to understand loops and nested loops. It also increases the “how to think logically to solve problems”. Understanding Java pattern problems is very essential for beginners or experienced programmers, practising pattern programs can improve your Java skills.
FAQs
What are Java Pattern Programs?
Java Pattern Programs are a set of programming problems that help developers familiarise themselves with loops and nested loops. These problems help the developer improve their understanding of control flow, loops and logical thinking.
What are some common Java Pattern Programs?
In Java, there are some common pattern problems, such as full pyramid, inverted pyramid, diamond pattern, and various alphabetic patterns. These problems can be created using nested loops and print statements to display the shape of the pattern and characters.
How can I get started with Java Pattern Programs?
To learn Java pattern programs. You can start with simple patterns and gradually increase the complexity. There are numerous online resources where you can learn Java Pattern Problems.
Can I use other characters besides “*” in pattern programs?
Yes, we can use any character or number to create patterns. Just replace the “*” in the code with your desired character or string.
Hero Vired is a leading LearnTech company dedicated to offering cutting-edge programs in collaboration with top-tier global institutions. As part of the esteemed Hero Group, we are committed to revolutionizing the skill development landscape in India. Our programs, delivered by industry experts, are designed to empower professionals and students with the skills they need to thrive in today’s competitive job market.