Triángulo Aprende programación con ejercicios Java

Lección:

Tipos de datos básicos


Ejercicio:

Triángulo 76


Objetivo:

Escriba un programa en java que solicite un símbolo y un ancho, y muestre un triángulo de ese ancho, usando ese número para el símbolo interior, como en este ejemplo:

Introduzca un símbolo: 4
Introduzca el ancho deseado: 5

44444
4444
444
44
4


Código:

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--;
		}
	}
}