2.4: Star Pattern Write a program that displays the following pattern: * *** ***
ID: 3787992 • Letter: 2
Question
2.4: Star Pattern Write a program that displays the following pattern: * *** ***** ******* ***** *** *
Output . Seven lines of output as follows: The first consists of 3 spaces followed by a star. The second line consists of 2 spaces followed by a 3 stars. The third consists of one space followed by 5 stars, and the fourth consists just of 7 stars. The fifth line is identical to third, th sixth to the second and the seventh to the first.
CLASS NAMES. Your program class should be called StarPattern
Explanation / Answer
class StarPattern
{
public static void main (String[] args)
{
int x = 1;
int increment_by = 2;
while (x > 0) {
// print spaces
int spaces = 4 - x / 2;
for (int i = 1; i <= spaces; i++) System.out.print(" ");
// print asterisks
for (int i = 1; i <= x; i++) System.out.print("*");
// end line
System.out.println();
// change width
if (x == 7) increment_by = -2; // reverse
x += increment_by;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.