Write code in JAVA
Write a program that will output a right triangle based on user specified input height (int) and specified input symbol (char).The first line will have one user-specified character, such as % or *. Each subsequent line will have one additional user-specified character until the number in the triangle's base reaches specified input height. Output a space after each user-specified character, including after the line's last user-specified character. Hint: Use a nested for loop.
Ex:If the input is:
Enter a character:
*
Enter triangle height:
8
the output is:
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
* * * * * * * *
//TrianglePattern.java import java.util.Scanner; public class TrianglePattern { public static void main(String[] args) { Scanner scan = new Scanner(System.in); char ch; int n; System.out.println("Enter a character:"); ch = scan.next().charAt(0); System.out.println("Enter triangle height:"); n = scan.nextInt(); System.out.println("the output is:"); for(int i = 0;i<n;i++){ for(int j = 0;j<=i;j++){ System.out.print(ch+" "); } System.out.println(); } } }
Get Answers For Free
Most questions answered within 1 hours.