Number Pattern Spiral in Java: Explanation & Practice
Print numbers in a specific pattern
Problem summary
Print numbers 1 to n² in a triangular increasing pattern.
Starter code
public class Main {
public static void main(String[] args) {
int n = 4; // Test case 1
// Print pattern:
// 1
// 2 3
// 4 5 6
// 7 8 9 10
}
}Expected output and test cases
- 4-row number triangle
1 2 3 4 5 6 7 8 9 10
- 3-row number triangle
1 2 3 4 5 6
Hints
- Keep a counter starting at 1
- Row i has i numbers
- Increment counter after each print
Related Control Flow exercises
Practice all Control Flow exercises · Run this idea in the Java compiler