×

Number Pattern Programs in Java

Number Pattern Programs in Java: Number pattern programs are part of pattern programs. In the previous section, we have learned the approach to print the pattern program in Java. To print the number program in Java we will follow the same approach with slight changes.

Right Triangle Number Pattern-1

Filename: NumberPatternExample1.java

public class NumericPatternExample1
{             
public static void main(String argvs[])
{
               int row = 5; //There will be 5 rows in the output
               int r, c; 
               for(r = 0; r < row; r++) //The loop handles rows
               {
                               for(c = 0; c <= r; c++) //  The loop handles columns
                               {      
                                              System.out.print(c + 1 + " "); // Displaying numbers
                               }   
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

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

Right Triangle Number Pattern-2

Filename: NumberPatternExample2.java

public class NumericPatternExample2
{             
public static void main(String[] args)
{                
int r, c, no = 1, row = 5;                
for (r = 1; r <= row; r++) // To handle rows               
{                               
for (c = 1; c < r + 1; c++) // To handle columns                             
   {                                   
  System.out.print(no++ + " "); // Printing numbers in ascending order               
                 }            
                    System.out.println(); // throws the cursor in the next line at the end of each row               
}
}
}

Output:

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

Right Triangle Number Pattern-3 Filename:

NumberPatternExample3.java

public class NumericPatternExample3
{             
public static void main(String argvs[])
-r {               
int row = 5; //There will be 5 rows in the output              
  int r, c;               
  for(r = 0; r < row; r++) //The loop handles rows              
  {                              
  for(c = 0; c <= r; c++) //  The loop handles columns         
  {                                                 
    System.out.print(r + 1 + " "); // Displaying numbers         
      }           
    System.out.println(); // throws the cursor in the next line at the end of each row    
  }
}
}

Output:

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

Right Triangle Number Pattern-4

Filename: NumberPatternExample4.java

public class NumericPatternExample4
{             
public static void main(String argvs[])
{
               int row = 5; //There will be 5 rows in the output
               int r, c; 
               for(r = row; r > 0; r--) //The loop handles rows
               {
                               for(c = row; c >= r; c--) //  The loop handles columns
                               {      
                                              System.out.print(c + " "); // Displaying numbers
                               }   
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

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

Right Triangle Number Pattern-5

Filename: NumberPatternExample5.java

public class NumericPatternExample5
{             
public static void main(String argvs[])
{
               int row = 5; //There will be 5 rows in the output
               int r, c; 
               for(r = 0; r < row; r++) //The loop handles rows
               {
                               for(c = r; c >= 0; c--) //  The loop handles columns
                               {      
                                              System.out.print(c + 1 + " "); // Displaying numbers
                               }   
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

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

Right Triangle Number Pattern-6

Filename: NumberPatternExample6.java

public class NumericPatternExample6
{             
public static void main(String argvs[])
{
               int row = 5; //There will be 5 rows in the output
               int r, c; 
               for(r = row; r > 0; r--) //The loop handles rows
               {
                               for(c = r; c <= row; c++) //  The loop handles columns
                               {      
                                              System.out.print(c + " "); // Displaying numbers
                               }   
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

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

Pascal Triangle Number Pattern

Filename: PascalTriangleNumberPattern.java

public class PascalTriangleNumberPattern
{             
public static void main(String argvs[])
{
               int row = 6; // There will be 6 rows in the output
               int r, c; 
               for(r = 0; r < row; r++) // The loop handles rows
               {
                               int no = 1; // Every row starts with number 1
                               // Ensuring different indentation for different rows
                               System.out.printf("%" + 2 * (row - r) + "s", "");
                               for(c = 0; c <= r; c++) // This loop handles columns
                               {     
                                              // Printing in C-language style. %4d means we are printing an
                                              // integer and there will be some inter-space between two
                                              // integers.
                                              System.out.printf("%4d", no);
                                              // calculating next number
                                              no = no * (r - c) / (c + 1);
                               }   
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

             1
           1   1
         1   2   1
       1   3   3   1
     1   4   6   4   1
   1   5  10  10   5  1

Diamond Number Pattern

Filename: DiamondNumberPattern.java

public class DiamondNumberPattern
{             
public static void main(String argvs[])
{
               int row = 5; // The first half the diamond contains 5 rows
               // Printing the first half of the diamond
               for (int r = 1; r <= row; r++) // Handling rows in the first half
               {
                               int number = row;
                               // Handling indentation in the first half of the diamond
                               for (int ind = 1; ind <= number - r; ind++)
                               {
                                              System.out.print(" ");
                               }
                               // Every row contains number from decreasing then increasing order
                               // The first for-loop handles the numbers in decreasing order
                               for (int dec = r; dec >= 1; dec--)
                               {
                                              System.out.print(dec);
                               }
                               // The second for-loop handles the numbers in increasing order
                               for (int inc = 2; inc <= r; inc++)
                               {
                                              System.out.print(inc);
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
               // Printing the second half of the diamond
               for (int r = row - 1; r >= 1; r--) // Handling rows in the second half
               {
                               int number = row - 1;
                               // Handling indentation in the second half of the diamond
                               for (int ind = 0; ind <= number - r; ind++)
                               {
                                              System.out.print(" ");
                               }
                               // Similar to the first half of the diamond.
                               // The first loop handles the numbers in the decreasing order
                               for (int dec = r; dec >= 1; dec--)
                               {
                                              System.out.print(dec);
                               }
                               // The second loop handles the numbers in the increasing order
                               for (int inc = 2; inc <= r; inc++)
                               {
                                              System.out.print(inc);
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

        1
      212
    32123
  4321234
543212345
  4321234
    32123
      212
        1

Hour-Glass Number Pattern

Filename: HourGlassNumberPattern.java

public class HourGlassNumberPattern
{             
public static void main(String argvs[])
{
               int row = 8; // The first half the hour-glass contains 8 rows
               // Printing the first half of the hour-glass
               for (int r = 1; r <= row; r++) // Handling rows in the first half
                {
                               // Handling indentation in the first half of the hour-glass
                               for (int ind = 1; ind < r; ind++)
                               {
                                              System.out.print(" ");
                               }
                               // This loop prints the numbers in each row
                               for (int no = r; no <= row; no++)
                               {
                                              System.out.print(no + " ");
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
               // Printing the second half of the hour-glass
                for (int r = row - 1; r >= 1; r--)
                {
                               // Handling indentation in the second half of the hour-glass
                               for (int ind = 1; ind < r; ind++)
                               {
                                              System.out.print(" ");
                               }
                               // This loop prints the numbers in each row
                               for (int no = r; no <= row; no++)
                               {
                                              System.out.print(no +" ");
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
                }
}
}

Output:

1 2 3 4 5 6 7 8
  2 3 4 5 6 7 8
    3 4 5 6 7 8
     4 5 6 7 8
       5 6 7 8
         6 7 8
           7 8
             8
           7 8
         6 7 8
       5 6 7 8
     4 5 6 7 8
   3 4 5 6 7 8
  2 3 4 5 6 7 8
1 2 3 4 5 6 7 8

K-Shaped Number Pattern

Filename: KShapedNumberPattern.java

public class KShapedNumberPattern
{             
public static void main(String argvs[])
{
               int row = 8; // The first half the letter K contains 8 rows
               // Printing the first half of the letter K
               for (int r = 0; r < row; r++) // Handling rows in the first half
                {            
                               //This loop prints the numbers in each row
                               for (int no = row; no > r; no--)
                               {
                                              System.out.print(no + " ");
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
               // Printing the second half of the letter K
                for (int r = 2; r <= row; r++)
                {          
                               // This loop prints the numbers in each row
                               for (int no = row; no > row - r; no--)
                               {
                                              System.out.print(no +" ");
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
                }
}
}

Output:

8 7 6 5 4 3 2 1
8 7 6 5 4 3 2
8 7 6 5 4 3
8 7 6 5 4
8 7 6 5
8 7 6
8 7
8
8 7
8 7 6
8 7 6 5
8 7 6 5 4
8 7 6 5 4 3
8 7 6 5 4 3 2
8 7 6 5 4 3 2 1

Binary Number Pattern-1 (Rectangular Shaped)

Filename: BinaryNumberPattern1.java

public class NumericPatternExample10
{             
public static void main(String argvs[])
{       
               int rows = 6; // The output will contain 6 rows  
               int columns = 6; // The output will contain 6 columns
               for (int r = 1; r <= rows; r++) // Handling rows
               {
                               int no = 0;
                               if(r % 2 == 0)
                               {
                                              no = 1;
                                              for (int c = 1; c <= columns; c++) // Handling columns
                                              {
                                                             System.out.print(no + " ");
                                                             no = (no == 0) ? 1 : 0; // Toggling between 0 & 1
                                              }
                               }  
                               else
                               {
                                              for (int c = 1; c <= columns; c++) // Handling columns
                                              {
                                                             System.out.print(no + " ");
                                                             no = (no == 0) ? 1 : 0; // Toggling between 0 & 1
                                              }
                               }     
                               System.out.println(); // Ensuring line break at the end of each row
               }
}
}

Output:

0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0

Binary Number Pattern-2 (Right Triangle)

Filename: BinaryNumberPattern2.java

public class BinaryNumberPattern2
{             
public static void main(String argvs[])
{       
               int rows = 6; // The output will contain 6 rows   
               for (int r = 1; r <= rows; r++) // Handling rows
               {
                               for (int c = 1; c <= r; c++) // Handling columns
                               {
                                              if(c % 2 == 0) // For even columns we print 0
                                              {
                                                             System.out.print(0 + " ");
                                              }
                                              else // For odd columns we print 1
                                              {
                                                             System.out.print(1 + " ");
                                              }
                               }
                               System.out.println(); // throws the cursor in the next line at the end of each row
               }
}
}

Output:

1
1 0
1 0 1
1 0 1 0
1 0 1 0 1
1 0 1 0 1 0

Related Topics

How to convert double to String in Java

How to Convert double to String in Java It is used when we want to convert double primitive to String type. There are two methods to convert double to String. Using String.valueOf()...

2 minutes read.

Shift right zero Fill Operator in Java and Operator Shifting

Left shift operator ( << ) : The left shift operator is an operator which performs its action at the bits level of a binary Operator. That means when you perform...

4 minutes read.

Race Condition in Java

Java is a multi-threaded programming language, race conditions are more likely to arise. Mostly because data can change when multiple threads visit the same resource simultaneously. Race conditions are concurrency...

3 minutes read.

Difference between Constructor and Method in Java

What is Constructor? In Constructor, we will discuss constructors and also will discuss default constructors and finally, we will discuss overloading constructors. A constructor is a method that is used to...

13 minutes read.

Java Localization

Internationalization is the process of creating a software application that can be translated into different languages and regions without modifying the application. Creating a locale-specific application raises the cost of...

3 minutes read.

FizzBuzz Program in Java

FizzBuzz is a well-known children's game. This game helps children learn division. The FizzBuzz game is becoming a popular programming question, appearing frequently in Core Java interviews. This section will...

3 minutes read.

Fork Join in Java

Multithreaded processors are being introduced in new computer systems today. The operation is faster due to multicore CPUs. Therefore, it becomes essential for a programmer to leverage multithreaded processors effectively...

4 minutes read.

Java Switch string

A multi-way branch statement is the switch statement. It offers a simple method for allocating execution to various code sections according to the expression's value. Primitive data types, including bytes,...

3 minutes read.

Java Maven Silicon

Maven is a build automation tool that is mostly used for Java applications. It allows you to manage the build, reporting, and documentation of a project from the central location. Maven provides...

4 minutes read.

Java Garbage Collection

Java Garbage Collection In Java, unreferenced objects are treated like garbage. The process of reclaiming the unused memory during runtime automatically is known as the Java Garbage Collection.In other words, the...

4 minutes read.

String Pool in Java

String Pool in Java: String is one of the most important discussed topics in Java. There are a lot of concepts related to the String and one of them is...

5 minutes read.

How to Convert int to char in Java

To convert a higher data type to lower data type, we need to do typecasting. Casting is also required when we want to convert ASCII value into character. It is...

3 minutes read.

Java String lastIndexOf() method

Java String lastIndexOf() method returns last index of character or substring in a String. Syntax: Method Description int lastIndexOf(int ch)It returns last index position for the given char valueint lastIndexOf(int ch, int...

2 minutes read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Java String Inbuilt functions

There are different types of inbuilt functions available in the Java String class, all of them are listed below. Char chat At (int index)It outputs the character indicated by the index....

5 minutes read.

Array and String with Examples in Java

Array in Java: An array in Java is a group of variables with similar types that have a common name. The arrays used in Java differ from those used in C/C++. Key...

6 minutes read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java Program to remove duplicate characters in a string

In strings, we can find many characters present one or more times in a string; accessing a particular character is difficult due to repetition. Duplicate characters will present in the...

5 minutes read.

How to create a mirror image of a 2D array in Java

Problem Statement We have provided a list of m x n. here m indicates rows, and n indicates columns). Printing the fresh matrices should result in a mirror reflection of an...

2 minutes read.

Automorphic Number Program in Java

We will learn about automorphic numbers through examples in this article, and we'll also make Java programmes that can determine whether a specific number was automorphic or not. What is an...

3 minutes read.