50.1k views
4 votes
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

User Seldridge
by
8.5k points

1 Answer

1 vote

Answer:

// here is code in java.

public class NAMES

{

// main method

public static void main(String[] args)

{

int n=4;

// print the upper half

for(int a=1;a<=n;a++)

{

for(int b=1;b<=n-a;b++)

{

// print the spaces

System.out.print(" ");

}

// print the * of upper half

for(int x=1;x<=a*2-1;x++)

{

// print the *

System.out.print("*");

}

// print newline

System.out.println();

}

// print the lower half

for(int y=n-1;y>0;y--)

{

for(int z=1;z<=n-y;z++)

{

// print the spaces

System.out.print(" ");

}

for(int m=1;m<=y*2-1;m++)

{

// print the *

System.out.print("*");

}

// print newline

System.out.println();

}

}

}

Step-by-step explanation:

Declare a variable "n" and initialize it with 4. First print the spaces (" ") of the upper half with the help of nested for loop.Then print the "*" of the upper half with for loop. Similarly print the lower half in revers order. This will print the required shape.

Output:

*

***

*****

*******

*****

***

*

User Tariqdaouda
by
9.1k points