• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Devaka Cooray
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • paul wheaton
  • Paul Clapham
Saloon Keepers:
  • Piet Souris
Bartenders:

Need Logic/Java Code for below pattern

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need to understand the logic for the attached Star pattern in the image.

I could not really understand how the Spacing logic for first row as well as the only 3 astericks in last row is acheived.

Appreciate if anyone can provide complete code of java (using FOR loops - to print spaces and astercisk for each row)
1.jpg
Star pattern
Star pattern
 
Bartender
Posts: 11195
90
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What have you tried so far? What output did it produce?
 
selby Graham
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
output for code below that i tried:
   *    *
 *   *  *   *
*       **       *

 
Carey Brown
Bartender
Posts: 11195
90
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Saloon Keeper
Posts: 5788
221
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to avoid all these nasty space-jugglings :

Form a variable s = String[rows][cols] with cols = 4 * rows - 3 and rows = say 10;
Fill the array with "   " (or " ".repeat(3));
initialise:
currentRow = rows - 1;
deltax = 1;
And then use one loop:
from col = 0 to cols;
   set s[currentRow][col] to " * ";
   test: if currentRow == 0 or currentRow == rows - 1 then deltax = -deltax;
   currentrow += deltax;

Finally print s.
 
Piet Souris
Saloon Keeper
Posts: 5788
221
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
and selby,

welcome to the Ranch!

 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic