Please enter a positive integer 11235 8 13 21 F 821 3 Write

Please enter a positive integer: 11235 8 13 21 F 8-21 3. Write a program to ask the user to enter the size of a triangle (an integer from 1 to 50). Display the triangle by writing lines of asterisks. The first line will have one asterisk, the next two, and so on, with each line having one more asterisk than the previous line, up to the number entered by the user. On the next line write one fewer asterisk and continue by decreasing the number of asterisks by 1 for each successive line until only one asterisk in displayed. (Hint: Use nested for loops; the outside loop controls the number of lines to write, and the inside loop controls the number of asterisks to display on a line. Complete the program by solving the upper triangle followed by the lower triangle.) Please enter a positive integer: Please enter a positive integer: Please enter a positive integer: 844 1844 8344 4484 244 ea ta

Solution

Triangle.java

import java.util.Scanner;

public class Triangle {

   public static void main(String[] args) {

       int num;

       // Scanner class object is sued top read the inputs entered by the user
       Scanner kb = new Scanner(System.in);
      
       //This loop continues to execute until the user enters a valid number
       while (true) {
           // Getting the number entered by the user
           System.out.print(\"Please Enter a positive number :\");
           num = kb.nextInt();
          
           /* Checking whether the number is with in range or not
           * If nor prompt the user to enter again
           */
           if (num < 1 || num > 50) {
               //Displaying the error message
               System.out.println(\"Invalid Number.Number must be between 1-50 (inclusive)\ \");
               continue;
           } else
               break;
       }

       //Displaying the required pattern of upper triangle
       for (int i = 1; i <= num; i++) {
           for (int j = 1; j <= i; j++) {
               System.out.print(\'*\');
           }
           System.out.println();
       }
       //Displaying the lower triangle
       for (int i = num - 1; i > 0; i--) {
           for (int j = 1; j <= i; j++) {
               System.out.print(\'*\');
           }
           System.out.println();
       }
   }

}

____________________________

Output1:

Please Enter a positive number :1
*

____________________________

Output2:

Please Enter a positive number :2
*
**
*

________________________

Output3:

Please Enter a positive number :5
*
**
***
****
*****
****
***
**
*

______Thank You

 Please enter a positive integer: 11235 8 13 21 F 8-21 3. Write a program to ask the user to enter the size of a triangle (an integer from 1 to 50). Display the
 Please enter a positive integer: 11235 8 13 21 F 8-21 3. Write a program to ask the user to enter the size of a triangle (an integer from 1 to 50). Display the

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site