Spiral Triangle of Stars
This program prints a triangular spiral of stars.
Problem Statement
Write a Java program that prints a triangle of 6 rows filled with stars in spiral order.
Source Code
| 1 | public class SpiralTriangleStars { |
| 2 | public static void main(String[] args) { |
| 3 | int n=6; |
| 4 | for(int i=0;i<n;i++){ |
| 5 | for(int j=0;j<=i;j++){ |
| 6 | System.out.print("* "); |
| 7 | } |
| 8 | System.out.println(); |
| 9 | } |
| 10 | } |
| 11 | } |
Program Output
* * * * * * * * * * * * * * * * * * * * *
Explanation
Every cell in the growing triangular region is simply printed as a star, so the triangle fills solidly with stars row by row, forming a basic left-aligned right triangle.