Number Pattern Program in Java

Since they provide a great brainstorming session to evaluate a person’s uniqueness and creativity, number patterns are becoming more and more common as interview questions for freshers. Making a matrix out of the pattern code and then looking at the pattern in the matrix is the best method to tackle a pattern problem.

Number Pattern

Java’s conditional loops and syntax may be used with Number Patterns to create a variety of patterns, including star patterns, character patterns, and others. Let’s look at how to interpret a pattern before going on to the Java Number Pattern Program.
The condition of loops and the condition of the print statement make up the first and second groups, respectively, of the pattern program in Java.

for(condition of i loop){
  for(condition of j loop){
print(...);
  }
}

We can form the conditions of the loops as follows:
The outer loop will begin at i = 1 if the number of elements in the rows increases; otherwise, it will begin at i = row or i = x, where x may be any integer.
If the elements are increasing in each row, the inner loop will begin at j = 1, otherwise, it will begin at j = row, j = i or j = x, where x can be any integer.
The above trick works on most of the patterns, but we should always cross-verify the conditions of the loop. And for the condition of the print statement, let’s understand it with an example.
Consider the pattern for number n = 5

To understand this pattern, convert the pattern into a 2-D Matrix and mention the row numbers and column numbers.

Explanation:
The outer loop will begin from i = 1 to i = n because there are more elements in the rows. There are i elements per row, therefore the j loop runs from j = 1 to j = i.
But if we pay close attention, we are publishing the row number in each print statement (By observing the 2-D Matrix). Thus, the 2-D matrix helps in our evaluation of the print statement’s status.

Top Number Pattern Programs in Java

Pattern Example-1
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5

class Main {
  public static void main(String args[]) {
    int n = 5;
    //Loop to iterate over each row
    for (int i = 1; i <= n; i++) {
      //Loop to iterate over each column of the ith row
      for (int j = 1; j <= i; j++) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}

Output

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 

Explanation
The i loop runs from i = 1 to i = n since there are more items in each row.
The elements in each row are arranged in ascending order, thus the j loop runs from j = 1 to j = i. Additionally, we print the column number or j in each printing process.

Pattern Example – 2

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
class Main {

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

    for (int i = n - 1; i >= 1; i--) {
    
      for (int j = 1; j <= i; j++) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}

Output

1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 

Explanation:
Pattern Example 2 explains the upper triangle. You can see that there are less elements in the rows of the Lower Triangle, therefore the i loop is running from i = n-1 to i = 1.
Since the components in each row are arranged in ascending order, the j loop runs from j = 1 to j = i.
As a result, we display the pattern for n rows in the first section, followed by the pattern for n-1 rows in decreasing sequence from n-1 to 1.

Pattern Example – 3

5 4 3 2 1
4 3 2 1
3 2 1
2 1
1
class Main {

  public static void main(String args[]) {
    int n = 5;

    for (int i = n; i >= 1; i--) {
      for (int j = i; j >= 1; j--) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}

Output

5 4 3 2 1 
4 3 2 1 
3 2 1 
2 1 
1 

Explanation
As we can see, there are fewer elements in the rows. As a result, the i loop executes from i = n to i = 1.
Additionally, the components in each row are arranged in decreasing order, thus the j loop extends from j = i to j = 1.

Pattern program – 4

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5
class Main {

  public static void main(String args[]) {
    int n = 5;

    for (int i = n; i >= 1; i--) {
    
      for (int j = 1; j <= i; j++) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
    for (int i = 2; i <= n; i++) {
 
      for (int j = 1; j <= i; j++) {
        System.out.print(j + " ");
      }
      System.out.println();
    }
  }
}
 

Output

1 2 3 4 5 
1 2 3 4 
1 2 3 
1 2 
1 
1 2 
1 2 3 
1 2 3 4 
1 2 3 4 5

Explanation:
Since there are fewer elements in the rows in the top triangle, the i loop begins at i=n and ends at i=1.
The components in each row are arranged in ascending order, thus the j loop runs from j = 1 to j = i.
Since there are more items in the rows of the bottom triangle, the i loop begins at i = 2 and continues until i = n.
Since the components in each row are arranged in ascending order, the j loop begins with j = 1 and ends with j = i.

Pattern program – 5

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
class Main {

  public static void main(String args[]) {
    int n = 5;

    int counter = 1;
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= i; j++) {
        System.out.print(counter + " ");
        counter++;
      }
      System.out.println();
    }
  }
}

Output

1
2 3
4 5 6
7 8 9 10
11 12 13 14 15

Explanation
The i loop runs from i = 1 to i = n since there are more items in each row.
However, as you can see, there are I elements in the ith row, thus the j loop runs i times, with each iteration outputting the value of the counter.
We add one after printing the counter’s value to make it higher.

Conclusion:
We’ve already seen some Java number programs that print various number patterns. How to print the number pyramid pattern with the given number of rows as well as the inverted hollow number pyramid pattern. includes formulae to aid in step-by-step learning and comprehension.

Other Java Programs

Java Program to Add Two Numbers
Java Program to Check Prime Number
Java Program to Check Whether a Number is a Palindrome or Not
Java Program to Find the Factorial of a Number
Java Program to Reverse a Number
Java Program to search an element in a Linked List
Program to convert ArrayList to LinkedList in Java
Java Program to Reverse a linked list
Java Program to search an element in a Linked List
Anagram Program in Java
Inheritance Program in Java
Even Odd Program in Java
Hello World Program in Java
If else Program in Java
Binary Search Program in Java
Linear Search Program in Java
Menu Driven Program in Java
Package Program in Java
Leap Year Program in Java
Array Programs in Java
Linked List Program in Java
String Programs in Java
Star Program in Java

Leave a Reply

Your email address will not be published. Required fields are marked *