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

Complete the following code segment using only one loop so that itwill print the

ID: 3614739 • Letter: C

Question

Complete the following code segment using only one loop so that itwill print the following pattern (shown for n = 4):

****
***
**
*

There are no spaces on the first line of output. You may assumethat the user types a number between 1 and 50. Use the variablenames provided.

Scanner scan = new Scanner(System.in);
System.out.println(“Enter the number of lines”);
int n = scan.nextInt( ), i;
String spaces = “”;
String stars =“**************************************************”;

Explanation / Answer

I am not sure what you are meant to do with the spacesvariable. You never said anything about it. Here is everything else though. stjohn@beatific ~/programming/java $ java Stars Enter the number of lines 4 **** *** ** * stjohn@beatific ~/programming/java $ cat Stars.java import java.util.Scanner; public class Stars {         public static voidmain(String[] args)         {                Scanner scan = new Scanner(System.in);                System.out.println("Enter the number of lines");                int n = scan.nextInt(), i;                String spaces = "";                String stars ="**************************************************";                for (i=n; i>0; i--)                {                        System.out.println(stars.substring(0,i));                }         } } stjohn@beatific ~/programming/java $

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