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