Triangle, NorthEast Learn programming Java

Lesson:

Basic Data Types


Exercise:

Triangle, NorthEast 22


Objetive:

Write a java program which asks for a width, and displays a triangle like this one:

Enter the desired width: 5

*****
_****
__***
___**
____*


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int width, height;
		int row, column;
		int max;

		System.out.print("Enter the desired width: ");
		height = Integer.parseInt(new Scanner(System.in).nextLine());
		width = 0;
		max = height;

		for (row = 0; row < height; row++)
		{
			for (column = 0; column < width; column++)
			{
				System.out.print(" ");
			}

			for (int asterisks = 0; asterisks < max; asterisks++)
			{
				System.out.print("*");
			}

			System.out.println();
			width++;
			max--;
		}
	}
}