Answer:
import java.util.Scanner;
public class num4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int size;
do{
System.out.println("Enter size of triangle");
size = in.nextInt();
}while(size<=1 || size>=50);
for(int i = 1; i <= size; ++i) {
for(int j = 1; j <= i; ++j) {
System.out.print("* ");
}
System.out.println();
}
System.out.println();
//Second triangle bottom up
for(int i = size; i >= 1; --i) {
for(int j = 1; j <= i-1; ++j) {
System.out.print("* ");
}
System.out.println();
}
}
}
Step-by-step explanation:
- Use Scanner class to receive user value for size
- Use a do......while loop to ensure user entered a valid range (1-50)
- Use nested loops to print the first triangle from i = 1
- Print also the second triangle from i = size downwards