// This program demonstrates nested for loops
// Get an integer number from the user, 
// draw a triangle of stars


// need this import for reading data:
import java.io.*;

public class Triangles {

    public static void main (String [] args)  throws IOException {
	// Create a buffered reader
	// need only one buffered reader for all your reading
	BufferedReader in = new BufferedReader(
			new InputStreamReader(System.in));
	// string for storing a line of input
	String line;

	// Read one integer
	// prompt the user:
        System.out.print("Please enter an integer: ");
	// read a line of input:
	line = in.readLine();
	// extract the integer from the entered data:
	int n = Integer.parseInt(line);

	// lower right
	for (int i = 0; i < n; ++ i) {
	    for (int j = 0; j < n - i - 1; ++ j)
		System.out.print(" ");
	    for (int j = n - i - 1; j < n; ++ j)
		System.out.print("*");
	    System.out.println(); // go to new line
	}

	// upper right:
	for (int i = 0; i < n; ++ i) {
	    for (int j = n - i; j < n; ++j)
		System.out.print(" ");
	    for (int j = i; j < n; ++j)
		System.out.print("*");
	    System.out.println(); // go to new line
	}

	/*	
	// upper left:
	for (int i = 0; i < n; ++ i) {
	    for (int j = 0; j < n - i; ++ j)
		System.out.print("*");
	    System.out.println();
	}

	// lower left:
	for (int i = 0; i < n; ++ i) {
	    for (int j = n - i - 1; j < n; ++ j)
		System.out.print("*");
	    System.out.println();
	}
	*/
    }
}

This is an example from CSci 1211 course.

The views and opinions expressed in this page are strictly those of the page author. The contents of this page have not been reviewed or approved by the University of Minnesota.