In this tutorial, we will write a number pattern program in java. The patterns are half pyramid patterns with different combinations of numbers such as in increasing order, decreasing order, or more.
Before that, you may go through the following topic in java.
Java Program to print Half Pyramid using Numbers
The programs below take the user input for the number of rows to be printed for the half pyramid. Now let us go through the java program to print pyramid pattern of numbers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | import java.util.Scanner; public class Main { public static void main(String[] args) { int rows; Scanner sc = new Scanner(System.in); System.out.print("Enter the no. of rows: "); rows = sc.nextInt(); for (int i = 1; i <= rows; ++i) { for (int j = 1; j <= i; ++j) { System.out.print(j + " "); } System.out.println(); } } } |
Output:
1 2 3 4 5 6 | Enter the no. of rows: 5 1 1 2 1 2 3 1 2 3 4 1 2 3 4 5 |