×

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 Import Keyword

Java's import keyword used in the code that follows the import statement, a Java class is declared. Once a Java class is declared, it is possible to use the class...

6 minutes read.

Inheritance Program in Java

Inheritance Program in Java Inheritance is one of the important pillars of Object-Oriented Programming that facilitates parent-child relationships in programming. Using inheritance, we can create a new class with the help...

7 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

Java String Interview Questions

Java String Interview Questions String is the most common and important discussed topics in a Java interview. In Java interview interviewer generally asked three to four questions based on String concept....

16 minutes read.

How to add double quotes in a string in Java

Strings are indicated using double-quotes. Double quotes are not printed; the values inside the double quotes are printed. There are different methods for adding double quotes to the string, such...

2 minutes read.

How to check version of java in Linux

Java is one of the most famous and thoroughly utilized programming tongues from one side of the world to the other. On the off chance that you are a Java...

2 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

Java Math abs() Method

The abs() method of Math class returns the absolute value of the argument where the argument can be int, double, float, long. Syntax public static int abs(int a) public static float abs(float a) public...

2 minutes read.

Object class in Java

In Java, a class is a file containing the Java byte code. It can essentially specifically be executed on the JVM (Java Virtual Machine), fairly significant. The JVM mostly generally...

6 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Java Subtract Days from Current Date

Dealing with date and time in Java is not a particularly challenging operation because Java has an API for date and time that simplifies duties for developers. There are two...

3 minutes read.

Intersection Point of two linked list in Java

In this article, you will be very well acknowledged about how to achieve the intersection point of two linked list in Java. There are several approaches to obtain. Surely each...

8 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.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Computing Digit Sum of all Numbers from 1 to n in Java

In this tutorial, we will discuss various methods to compute the sum of digits of all numbers beginning from 1 to n through a Java program. We will achieve it through...

7 minutes read.

PriorityQueue in Java

PriorityQueue in Java A PriorityQueue is a member of the Java Collection Framework and is used when the values are needed to be processed based on priority. Priority queue operates similar to...

8 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Java Sort String

In this article, you will be acknowledged about how to sort a string in Java. Introduction Firstly, let us revise what is string Strings are collections of characters that are frequently used in...

4 minutes read.

What is advance Java?

The definition of advance inside the dictionary refers to a forward motion, development, or progress, and the definition of enhancing is something that improves things. To become experts in that...

3 minutes read.