Triangle V2 Learn programming Java

Lesson:

Arrays, Structures and Strings


Exercise:

Triangle V2


Objetive:

Write a java program to ask the user for his/her name and display a triangle with it, starting with 1 letter and growing until it has the full length:

Enter your name: Juan
J
Ju
Jua
Juan


Code:

import java.util.*;
public class Main
{
	public static void main(String[] args)
	{
		int n, ancho, alto;
		int row, column;

		System.out.print("Introduce un número: ");
		n = Integer.parseInt(new Scanner(System.in).nextLine());

		System.out.print("Introduce la anchura: ");
		ancho = Integer.parseInt(new Scanner(System.in).nextLine());
		alto = ancho;

		for (row = 0; row < alto; row++)
		{
			for (column = 0; column < ancho; column++)
			{
				System.out.print(n);
			}

			System.out.println();

			ancho--;
		}
	}
}

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