Write an application called Trianlgle (That means it has a main method) to ask t
ID: 3925757 • Letter: W
Question
Write an application called Trianlgle (That means it has a main method) to ask the user for a positive integer. Keep asking until he correctly enters a positive integer. Do not throw an exception if the user enters a non-integer. Just consume the bad data and ask again.
Here is the prompt: System.out.print("How many rows in the triangle? ");
Print the integer.
Then using plus sign (+), display a filled isosceles triangle of the given number of rows. That means if the integer is 4, there will be four rows and the output will look like this - with the last row starting at the left edge:
Use nested loops
Explanation / Answer
Answer :
import java.io.*;
public class Triangle {
public static void main ( String[] args ){
BufferedReader br = new BufferedReader(new InputStreamReader( System.in ));
System.out.print("How many rows in the triangle? ");
int num=0;
num=Integer.parseInt( br.readLine() );
for(int i=1;i<=num;i++){
for(int j=1;j<num-(i-1);j++){
System.out.print(" ");
}
for(int k=1;k<=i;k++){
System.out.print("+");
for(int k1=1;k1<k;k1+=k){
System.out.print("+");
}
}
System.out.println();
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.