Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Allow the user to input two values: a character to be used for printing an isosc

ID: 673143 • Letter: A

Question

Allow the user to input two values: a character to be used for printing an isosceles triangle and the size of the peak for the triangle. Test the input for valid characters. The size of the triangle should not be larger than 10. If an invalid entry is entered for size or if the value entered for size is larger than 10, use 3 as the default value. If an invalid entry is entered for the character, use an asterisk (*) as the default character. For example, if the user inputs # for the character and 6 for the peak, you should produce the following display: # ## ### #### ##### ###### ##### #### ### ## #

Explanation / Answer

import java.util.Scanner;

public class IsoscelesTriangle {
   public static void main(String args[]) {
       int n;
       String ch;
       Scanner scanner = new Scanner(System.in);

       System.out.println("Test 1 : ");
       System.out.print("Enter the number of rows: ");
       n = scanner.nextInt();
       System.out.print("Enter the Character: ");
       ch = scanner.next();
       generatePyramid(n, ch);
      
       System.out.println("Test 2 : ");
       System.out.print("Enter the number of rows: ");
       n = scanner.nextInt();
       System.out.print("Enter the Character: ");
       ch = scanner.next();
       generatePyramid(n, ch);
      
       System.out.println("Test 3 : ");
       System.out.print("Enter the number of rows: ");
       n = scanner.nextInt();
       System.out.print("Enter the Character: ");
       ch = scanner.next();
       generatePyramid(n, ch);

       System.out.println("Test 4 : ");
       System.out.print("Enter the number of rows: ");
       n = scanner.nextInt();
       System.out.print("Enter the Character: ");
       ch = scanner.next();
       generatePyramid(n, ch);

      
      
      
      
      
}
  
   public static void generatePyramid(int n,String ch){
  
      
      

       if (n >= 10 || n < 1) {
           n = 3;

       }
       if (!ch.matches("[^a-zA-Z0-9 ]")) {

           ch = "*";

       }

       for (int i = 1; i <= n; i++) {

           for (int j = 1; j <= i; j++) {
               System.out.print(ch);

           }
           System.out.println();
       }
       for (int i = 1; i <= n - 1; i++) {

           for (int j = n - i; j >= 1; j--) {
               System.out.print(ch);

           }
           System.out.println();
       }

output :

Test 1 :

Enter the number of rows: 10
Enter the Character: *
*
**
***
**
*
Test 2 :

Enter the number of rows: 15
Enter the Character: #
#
##
###
##
#
Test 3 :

Enter the number of rows: 5
Enter the Character: 8
*
**
***
****
*****
****
***
**
*
Test 4 :

Enter the number of rows: 12
Enter the Character: 7
*
**
***
**
*

   }

}
  

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote