Number Pattern in Java: Explanation & Practice
Print a number pattern
Problem summary
Print a right-angle triangle pattern of numbers with n rows.
Starter code
public class Main {
public static void main(String[] args) {
int n = 4; // Test case 1
// Print pattern:
// 1
// 12
// 123
// 1234
}
}Expected output and test cases
- 4-row pattern
1 12 123 1234
- 3-row pattern
1 12 123
- 5-row pattern
1 12 123 1234 12345
Hints
- Outer loop controls rows (1 to n)
- Inner loop prints numbers (1 to row number)
- Print newline after each row
Related Control Flow exercises
Practice all Control Flow exercises · Run this idea in the Java compiler