×

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

Java Integer lowestOneBit()

The lowestOneBit () method of Java Integer class returns an int value with at most a single one-bit, in the position of the lowest-order one-bit in the specified int value.  Syntax public...

2 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Java 9 Tutorial

The Java 9 is most popular release of java programming to make it platform modular. It is used to improve JDK and java implementation security. It provides JAVA SE and...

3 minutes read.

How to Convert long to int in Java

How to Convert long to int in Java When we assign a larger type value to a variable of smaller type, then we need to perform explicit casting for the conversion....

2 minutes read.

Java Math log1p() Method

The log1p() method of Math class returns the natural logarithmic sum for the specified double argument and 1. Its value is much closer to result of ln(1 + x). Syntax: public static...

2 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Java Integer equals() method

The equals() method of Integer class compares the given object to the specified object. Syntax public boolean equals(Object obj) Parameters The parameter ‘obj’ represents the object to be compared with. Overrides The equals() method overrides equals...

1 minute read.

3N+1 problem program in Java

The 3N+1 problem is a hypothesis in the field of abstract mathematics (not yet proven). Collectively known as the Collatz problem. This tutorial will describe about the 3N+1 problem and...

3 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

How to enable java in chrome

The Java module is huge for the Java Runtime Environment (JRE). It permits a program to work with the Java stage to run Java applets. Essentially, each of the undertakings connect...

3 minutes read.

Java Flags Enum

In a programming language, enumerations represent a group of named constants.For instance; the four suits in a deck of playing cards could be the enumerators Club, Diamond, Heart, and Spade,...

3 minutes read.

Java Integer divideUnsigned() method

The divideUnsigned() method of Integer class returns the unsigned quotient by dividing the first argument by the second argument. Syntax public static int divideUnsigned (int dividend , int divisor) Parameters The parameter ‘dividend’ represents...

1 minute read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Java Transient Keyword

An object in Java can be turned into a stream of bytes using serialization. The data of the instance and the kind of data saved in that instance are both...

3 minutes read.

Console Errors in Java

An unlawful motion taken through the person that reasons this system to act abnormally is amistake until this system is compiled or run; maximum programming mistakes pass unnoticed.The software is...

3 minutes read.

Java Integer longValue() method

The longValue()  method of Java Integer class returns a long value for this Integer after a widening primitive conversion. Syntax public long longValue() Parameters NA Specified by This method is specified by longValue in class Number Return...

1 minute read.

Java Plot

Java Plot is a phrase in Java that is mostly used for plotting coordinates on a cartesian plane. Plotting graphs in Java is accomplished through the use of various core...

3 minutes read.

Java String substring() method

Java String substring() method returns a part of the String. Syntax: public String substring(int startIndex) public String substring(int startIndex, int endIndex) Parameters: startIndex : starting index endIndex : ending index Returns: It returns a specified String. Throws: StringIndexOutOfBoundsException if start...

2 minutes read.