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

Write a program that uses nested loops to create the following output shape: 1.

ID: 3585654 • Letter: W

Question

Write a program that uses nested loops to create the following output shape: 1. 2. 3. 4. 5. Your program should be generalized to ask the user to input the number of output-lines. The hourglass shape shown above has nine (9) output lines. The input value provided by the user should always be a positive odd number or zero. Zero, an even number, or a negative number will cause the program to immediately exit. Note that the number of lines is also equal to the number of output stars (asterisks) on the first and last lines. Run your program for the input values-1, 1, 2, 7, 9, and 0 to demonstrate that it works properly The only print statements you are allowed to use are: System.out.print "Please enter an odd-number of output-lines (enter zero to exit the program): "; Systen.out-printf( “*" ); // this is a single asterisk System , out, printf( “ " ); // this is a single blank System . out.print f( “ " ); // this is a new line

Explanation / Answer

import java.util.Scanner;
import java.util.*;
import java.lang.*;

public class A
{
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
       
        //Taking rows value from the user
       
        System.out.println("Please enter the odd number of output lines and 0 for exit");
       
        int rows = sc.nextInt();
   if(rows<0||rows%2==0)
   {
       System.out.print(" Sorry! you have entered wrong choise.");  
       System.exit(0);
   }
   else
   {
       
        for (int i = 1; i <= rows; i++)
        {
            for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }
           
            
            for (int j = i; j <= rows; j++)
            {
                System.out.print(" *");
            }
           
            System.out.println();
        }
       
       for (int i = rows-1; i >= 1; i--)
        {
               
            for (int j = 1; j < i; j++)
            {
                System.out.print(" ");
            }
           
            for (int j = i; j <= rows; j++)
            {
                System.out.print(" *");
            }
           
            System.out.println();
        }
       
   }
         sc.close();
    }
}

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