Question-   write a program that displays the following pattern

Solution-

Question:- 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

 

Complete solution below:-



public class StarShapePattern {

//the main method

                        public static void main(String[] aug) {

                        final int rowVal = 7;

                        int spacesVal = rowVal/2;

                        int  starsNum= 1;

                        //this loop works on the rows of pattern.

                        for(int j = 0; j < rowVal; j ++){

                                    //this loop works on the one row how spaces are there many patterns.

                        for(int k = 0; k < spacesVal; k ++){

                        System.out.print("");

                        }

                        //this loop works on the one row how stars are there many patterns.

                        for(int k = 0; k < starsNum; k ++){

                        System.out.print("*");

                        }

                        //after every row complete there should be a new line.

                        System.out.println();

                        if(j< rowVal/2){

                        spacesVal--;

                        starsNum+= 2;

                        }

                        else{

                        spacesVal++;

                        starsNum-= 2;

                        }

                        }

                        }

                       

}

Our Happy Testimonials

They are fast in responding to homework questions. they have the best technical writers. Thanks for helping me with my programming doubts.

studentDyana
5  stars image

I contact to disklib for homework, they help me out, despite there was some technical issue they gone through extra mile for me and provide me good quality work in first priority. 100% recommended.

studying on laptopAsif Waheed
5  stars image

Desklib's study resources are best & unique. Their study database is easy to access and easy to use.
100 % recommended.

library and studentsMike Taylor
5  stars image