Print a 10 by 10 grid of asterisks

  • Background

    This is a standard textbook example to demonstrate for loops.

    This is one of the first programs I learnt when I started learning programming ~10 years ago.


    You are to print this exact text:



    • You may have extra trailing newlines.

    • You may have extra trailing spaces (U+0020) at the end of each line, including the extra trailing newlines.


    This is . Shortest answer in bytes wins.

    I'm not that familiar with this. The chrome dev tools print repetition on one line. e.g, here it would print `(10)**********` is that acceptable, as we know it's a dev-tool 'optimisation'?

    @DylanMeeus "You are to print this **exact** text:"

    @DylanMeeus Since that is to do with the dev tools hiding repeated console outputs, and isn't native to JavaScript consoles as a whole and is not in the JavaScript spec - as well as the fact that feature can be turned off - i think it should be acceptable. Not all browsers will collapse it like that.

    @JamesTrotter oh I didn't notice that it can be turned off, cheers!

    @LeakyNun Leaderboard snippet please!

    One of the most interesting things about this challange is that depending on your language ********** can be shorter then a loop. Makes me wonder when it's better for a given language to switch between 1 or 2 loops.

    Do they have to be astricks? What about newlines?

    The shortest solution in JavaScript I can come up with to print to browser in JavaScript is `for(d=document.body,i=0;10>i++;d.innerHTML+='
    ')for(j=0;10>j++;)d.innerHTML+='*'`. I'd love to post this as an answer, but for some reason this question is "protected" and I don't have enough reputation here yet to post answers to "protected" questions.

    you say trailing new lines are acceptable. Are leading newlines acceptable too?

    Lua: `c="**********";print(c:gsub("*",c.."\n"))`

  • Brainfuck, 47 bytes


    Try it online!

    ++++++++++[->++++>+>+<<<]   set the tape to 40 10 10
    >++> set the tape to 42 10 10
    [-<..........>>.<] come on

    +1 for emoticon in the code `>.<`

    +1 for the fish bones `>++>`

  • C (gcc), 41 39 bytes


    what version of c?

    Add this info. I got that `i` is undcleared ;) So I can un downvote.

    looks good, good ol' ANSI C.

    `main(i){while(11-i++)puts("**********");}` is an alternative, same length.

    @YSC I saved two more bytes :)

    I like the clever use of the return from `puts()` (and of the initial value of `i`).

  • Bash + coreutils, 19 bytes

    I prefer to repeat stuff in Bash using 'yes'.

    yes **********|head

    I saved 2 bytes by @Neil's suggestion. But when the directory where you are running this command does not only contain files starting with a '.' dot you need to enclose the stars * with ".

    Bash + coreutils, 21 bytes

    yes "**********"|head

    Great idea to use `yes`. We usually label such solutions as “Bash + coreutils”.

    Nice that 10 lines coincidentally happens to be the default for `head`.

    Can you save two bytes by requiring that any files in the current directory must begin with a `.`?

    @Neil, is your comment ment for my answer? If so, I do not get it :)

    Normally `echo *` expands to all the files in the current directory that don't begin with a `.`; if there are no such files though, the `*` has no effect.

    I do not get it. `yes` just repeats the following text over and over. I do not use `echo` here... I just do not see what you mean.

    @CousinCocaine: His point was that you can get away with not quoting the `**********` command-line argument if the program is always run in a directory where no files match that pattern.

    @HenningMakholm Aha! I get it! So is it 'legal' to golf the "?

    You can also write `yes \**********|head` without the restriction on files.

    @FlorianF and what if the filename starts with an *?

    @CousinCocaine If a file starts with a * then it doesn't work. :-( So it is just a good compromise. Having a file that starts with * is much less likely than to sit in a non-empty directory.

    In programming, relying on 'less likely' is dangerous, but you are right, it also wont work with a file named **********

  • Vim, 13 8 bytes

    Saved 5 bytes thanks to @Lynn

    qqi*␛9.o␛[email protected] 


    10i*␛ insert 10 times *, and Y9p copy the line and paste it 9 times.

    `10i*♥Y9p` works.

    That's insane vi.

    I think you can count keystrokes instead of bytes for text editors, which means would be shorter.

    @addison `` is 1 byte

    Oh nevermind, I misread the answer. I see now

    Why ♥ and not ␛ ?

    Can you provide some explanation as to why this works? Great work (+1)

    I would have used `yy9p` myself, but nice job using capitals to save a character!

    @CL. Vim is love, Vim is life

  • Pyth, 6 bytes


    T is 10 in Pyth, Vab executes statement b a times, \* is the asterisk character constant, and multiplying (*) a string and an integer repeats that string n times. Pyth's implicit printing with V means 10 lines are printed.

  • Hexagony, 37 35 34 31

    10"+}(=${";<$<1}42/[email protected]_=<>\;>(_


       1 0 " +
    } ( = $ {
    " ; < $ < 1
    } 4 2 / . 0 @
    _ = < > \ ;
    > ( _ . .
    . . . .

    Try it online

    Basically just has two for loops counting down from ten to zero, printing out an asterisk on the inner loop, and a newline on the outer loop.


    This program consists of three main parts: initialisation of memory, a loop which prints ten asterisks and a loop which prints a newline. The loop which prints a newline also contains the loop which prints the asterisks.

    First, the code runs the totally linear memory initialisation. The code works out to be: 10"+}42. This sets the memory of the nearby edges to look like:

    10 \ / 10

    42 is the ASCII code for the asterisk character, and the two tens will be used as our loop counters. Of note is that the memory pointer is currently pointing away from the two tens, so moving backwards will put us on one of the tens.

    Next, we start the astersisk printing loop. Linearly, the code looks like: ;".._(. This prints out an asterisk, moves the memory pointer backwards and to the left and finally decrements the value there. After one iteration, the memory would look like:

    10 \ / 9

    Then we hit the loop condition: the bottom-leftmost >. If the edge we just decremented is still positive we bounce around and execute a { to move us back onto the 42. Then we hit a $ and return to the beginning of the printing loop, the ;, by skipping the <. If the value was zero, we head into the other loop.

    The outer loop begins by resetting the recently zeroed memory edge to ten (this is the 10 in the code, going southwest). Then, we print out this ten as an ASCII character, which is a newline. Next, we move onto the other memory edge and decrement it with {( and then execute what amounts to a bunch of noops: =${_=. Now, after one iteration of this loop, memory would look like:

     9 \ / 10

    This time, the memory is facing outwards from the edge storing a nine in the above diagram. Next we execute the < which acts as the loop condition for the outer loop. If the value was non-zero we bounce around off of some mirrors, then begin executing meaningful instructions again after entering the top of the hexagon at the " moving southwest. This causes us to move backwards and to the left, onto the 42 again, but facing inwards. Then the = flips our direction, resetting the state properly to begin the inner loop again. If the edge was set to zero, the instruction pointer goes on a little adventure which does nothing until it exits the program.

    The adventure begins by the instruction pointer venturing northeast, perilously disregarding the safety of the cardinal directions. It bravely ignores a mirror that is aligned with its diretion (/) and heroically leaps off of a trampoline ($) entirely evading the deadly trap of another, totally identical trampoline. Staring out at the emptiness of uninitialised hexagon edges, the pointer, without faltering for a moment, adds the two blank edges it faces together, setting the current edge to their sum: 0 (the edge was actually zero beforehand, but the pointer likes to believe this was pretty important). Since the edge is zero, the pointer makes a left turn at the fork in the road, walking into a mysterious forest (of hexagons). There, it finds itself disoriented, moving forwards and backwards and forwards, until it winds up at the same place in memory as it started. Thinking that the problem must be that the current edge was set to zero last time, the pointer bravely plants a 1 into the current edge. Then, the noble pointer investigates another path, one laid with... a trap! The current edge is decremented and set back to zero! The pointer, dazed by the shocking turn of events, stumbles back into the trap setting the current edge to negative one. Infuriated, the pointer attempts to return to the comparatively pleasant forest, only to notice that since the current edge is not positive, the paths have yet again shifted and the pointer finds itself walking into a cave. And by a cave, I mean the mouth of a giant hexagonal worm. Helpless, the pointer curses the sexinity with its dying breath. Also, the program ends.

    Golly, I hope the worm was OK with swallowing a pointer. Those things can *hurt*.

    +1 for writing the most interesting—the only—Hexagony explanation I've ever read. I felt so tense when the edge was decremented!

  • Emacs, 10 8 keystrokes

    F3 C-1 0 * ENTER F4 C-9 F4


    F3             Starts a macro recording
    C-1 0 * Apply 10 times command '*': prints 10 asterix'
    ENTER Insert new line
    F4 Stops the macro record
    C-9 F4 Apply 9 times the macro

    Thanks to Sean for saving two keystrokes, suggesting to replace C-udigit with C-digit.

    +1, I always upvote text editor answers (even though I'm more of a vim guy myself) :)

    If `C-u` counts as just one keystroke, then you can shave off two strokes by typing `C-1 C-0` (or `M-1 M-0`) instead of `C-u 1 0` and `C-9` instead of `C-u 9`.

    +1 because you had to suffer through using emacs to write this.

    Alternatively (saves nothing) the line repetition can be done inside the macro: `F3` `C-1` `0` `*` `ENTER` `C-1` `0` `F4`

    @JonathanCarroll yes it would save bytes if we were to print more than 10 lines ;)

    @DrMcMoylex Even is they're spam? (Not that this answer is, +1 for no vim)

  • Jelly, 7 bytes


    What's going on?

    ”*x⁵Ṅ9¡  - No arguments
    ”* - character literal, *
    x - multiply (dyadic operation)
    ⁵ - integer literal, 10 (we have now constructed the string '**********')
    Ṅ - Print & linefeed (monadic operation)
    9 - integer literal, 9
    ¡ - Repeat n times (n is 9 as the first Ṅ is not a repeat)

    Test it on tryitonline

    I really like the `Ṅ9¡`.

    List version: `”*ẋ⁵Wẋ⁵`.

    @EriktheGolfer you'd need a `Y` on the end to "print this exact text"

    @JonathanAllan It is a list on its own, though. It isn't meant to "print this exact text", but, if you want to work on it, you'll use it.

  • PowerShell, 14 12 bytes


    Constructs a string of asterisks of length 10 using string multiplication. Encapsulates that in parens and feeds that into the comma-operator to construct an array. We use array multiplication to construct a 10-element array consisting of that element (i.e., a 10-element array of asterisk strings). That's left on the pipeline, and output is implicit (since the default Write-Output for an array is newline-separated, we get that for free -- thanks to @Joey for the clarification).

    Older, 14 bytes


    Full program. Loops from 0 to 9 through a ForEach-Object loop |%{...}. Each iteration, we use string multiplication to create a length-10 string of *. Those resulting strings are left on the pipeline, and output at the end is implicit (since the default Write-Output for an array is newline-separated, we get that for free -- thanks to @Joey for the clarification).

    I like it, because powershell can get so verbose. Yet this is elegant and short.

    Well, technically the array is never passed through a `ToString`, it's unrolled and passed element by element to Write-Output. In contexts where the array is converted to a string, you get its elements space-separated.

    @Joey Ah, fair, that's a better way to put it. I'll edit the wording (and my answer template ;-)).

    I may be biased here because I'm involved with a PowerShell implementation so I had to learn a lot of what actually goes on inside the interpreter ;)

  • V, 7 bytes


    Try it online!

    About as straightforward as an answer can be.


    10      "10 times:
    é* "insert an asterisk
    10Ä "make 10 copies of the current line

    Non-competing version (5 bytes):



    10O     " Insert the following on the next ten lines:
    ± " 10 copies of
    * " an asterisk

    This didn't work when the challenge was posted because of a bug.

    Point of order: é and Ä are multibyte characters (at least in utf-8, as you have them here) so this program is 9 bytes long.

    @rob They are encoded in utf-8 here, because that's just the way the browser works. V uses "Latin1" encoding, where they are `E9` and `C4` respectively.

License under CC-BY-SA with attribution

Content dated before 7/24/2021 11:53 AM