Triangle Learn programming Java

Lesson:

Basic Data Types


Exercise:

Triangle


Objetive:

Write a java program that prompts for a symbol and a width, and displays a triangle of that width, using that number for the inner symbol, as in this example:

Enter a symbol: 4
Enter the desired width: 5

44444
4444
444
44
4


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		System.out.print("Enter a number: ");
		int n = Integer.parseInt(new Scanner(System.in).nextLine());

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

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

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

Juan A. Ripoll - Systems Tutorials and Programming Courses ©  All rights reserved.  Legal Conditions.