Introduction
Number pattern programs are another popular topic for learning and practicing loops and nested loops in Java. These programs are commonly asked in coding interviews and are great for beginners to understand how loops and control structures work in Java. In this post, we'll cover various number patterns, from simple to complex, along with detailed explanations and Java code examples.
Check out 15 Java Star Pattern Programs.
1. Right-Angled Triangle Number Pattern
A right-angled triangle where numbers increment in each row, starting from 1.
Example:
1
12
123
1234
12345
Java Program:
public class RightAngledNumberTriangle {
public static void main(String[] args) {
int rows = 5; // Number of rows
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print numbers from 1 to i
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints numbers from 1 to the current row number
i
.
2. Inverted Right-Angled Triangle Number Pattern
This pattern prints an inverted right-angled triangle where the numbers start from the largest and decrease with each row.
Example:
12345
1234
123
12
1
Java Program:
public class InvertedRightAngledNumberTriangle {
public static void main(String[] args) {
int rows = 5; // Number of rows
// Outer loop for rows
for (int i = rows; i >= 1; i--) {
// Inner loop to print numbers from 1 to i
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The outer loop starts from the total number of rows and decreases.
- The inner loop prints numbers up to the current row value.
3. Pyramid Number Pattern
A pyramid pattern where numbers increment as the rows increase, and the numbers are centered.
Example:
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
Java Program:
public class PyramidNumberPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Print leading spaces for centering
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print numbers in each row
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The first inner loop prints spaces to center the numbers.
- The second inner loop prints the numbers, starting from 1 and increasing up to the current row number.
4. Inverted Pyramid Number Pattern
An inverted pyramid number pattern where numbers start at the top and decrease as you move down the rows.
Example:
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
Java Program:
public class InvertedPyramidNumberPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows
// Outer loop for rows
for (int i = rows; i >= 1; i--) {
// Print leading spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print numbers in each row
for (int j = 1; j <= i; j++) {
System.out.print(j + " ");
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The first inner loop prints spaces for alignment.
- The second inner loop prints numbers in decreasing rows.
5. Floyd’s Triangle
Floyd's Triangle is a right-angled triangle formed by filling rows with consecutive numbers, starting from 1.
Example:
1
2 3
4 5 6
7 8 9 10
11 12 13 14 15
Java Program:
public class FloydsTriangle {
public static void main(String[] args) {
int rows = 5; // Number of rows
int num = 1; // Starting number
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print numbers in each row
for (int j = 1; j <= i; j++) {
System.out.print(num + " ");
num++; // Increment the number
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The outer loop controls the number of rows.
- The inner loop prints consecutive numbers for each row.
6. Number Diamond Pattern
A diamond-shaped pattern of numbers that expands in the upper half and contracts in the lower half.
Example:
1
121
12321
1234321
123454321
1234321
12321
121
1
Java Program:
public class NumberDiamondPattern {
public static void main(String[] args) {
int rows = 5; // Number of rows
// Print the upper half of the diamond
for (int i = 1; i <= rows; i++) {
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
// Print the lower half of the diamond
for (int i = rows - 1; i >= 1; i--) {
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println();
}
}
}
Explanation:
- The first set of loops prints the upper half of the diamond.
- The second set of loops prints the lower half of the diamond.
7. Pascal’s Triangle
Pascal’s Triangle is a triangle where each number is the sum of the two numbers directly above it.
Example:
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
Java Program:
public class PascalsTriangle {
public static void main(String[] args) {
int rows = 5;
for (int i = 0; i < rows; i++) {
// Print leading spaces
for (int j = 0; j < rows - i; j++) {
System.out.print(" ");
}
int number = 1;
// Print numbers in Pascal's triangle
for (int j = 0; j <= i; j++) {
System.out.print(number + " ");
number = number * (i - j) / (j + 1);
}
System.out.println();
}
}
}
Explanation:
- The outer loop controls the rows.
- The inner loop calculates the numbers using the formula for binomial coefficients.
8. Number Palindrome Pyramid
This pattern prints a pyramid with numbers, forming a palindromic sequence in each row.
Example:
1
121
12321
1234321
123454321
Java Program:
public class NumberPalindromePyramid {
public static void main(String[] args) {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Print leading spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print increasing numbers
for (int j = 1; j <= i; j++) {
System.out.print(j);
}
// Print decreasing numbers
for (int j = i - 1; j >= 1; j--) {
System.out.print(j);
}
System.out.println(); // Move to the next
line
}
}
}
Explanation:
- The first inner loop prints leading spaces to center the numbers.
- The second inner loop prints increasing numbers, while the third inner loop prints decreasing numbers to form the palindrome.
9. Number Square Pattern
A square pattern where each row contains the same number as the row number.
Example:
11111
22222
33333
44444
55555
Java Program:
public class NumberSquarePattern {
public static void main(String[] args) {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print the row number
for (int j = 1; j <= rows; j++) {
System.out.print(i);
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The outer loop controls the rows.
- The inner loop prints the row number
i
repeatedly.
10. Repeated Number Triangle Pattern
A triangle pattern where each row contains repeated numbers starting from 1.
Example:
1
22
333
4444
55555
Java Program:
public class RepeatedNumberTriangle {
public static void main(String[] args) {
int rows = 5;
// Outer loop for rows
for (int i = 1; i <= rows; i++) {
// Inner loop to print the row number repeatedly
for (int j = 1; j <= i; j++) {
System.out.print(i);
}
System.out.println(); // Move to the next line
}
}
}
Explanation:
- The outer loop controls the rows.
- The inner loop prints the row number
i
multiple times.
Conclusion
These 10 Java number pattern programs cover various patterns such as triangles, pyramids, diamonds, and more. By practicing these patterns, you can improve your understanding of loops and nested loops in Java, as well as develop problem-solving skills related to pattern printing.
Comments
Post a Comment
Leave Comment