113
\$\begingroup\$

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.

Task

You are to print this exact text:

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Specs

  • 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.

Scoring

This is . Shortest answer in bytes wins.

\$\endgroup\$
10
  • 3
    \$\begingroup\$ @DylanMeeus "You are to print this exact text:" \$\endgroup\$
    – Leaky Nun
    Aug 4, 2016 at 12:56
  • 14
    \$\begingroup\$ @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. \$\endgroup\$
    – James T
    Aug 4, 2016 at 12:58
  • 8
    \$\begingroup\$ @LeakyNun Leaderboard snippet please! \$\endgroup\$
    – anna328p
    Aug 4, 2016 at 22:08
  • 4
    \$\begingroup\$ 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. \$\endgroup\$
    – dwana
    Aug 5, 2016 at 9:14
  • 3
    \$\begingroup\$ you say trailing new lines are acceptable. Are leading newlines acceptable too? \$\endgroup\$ Feb 10, 2017 at 2:34

384 Answers 384

1
6 7
8
9 10
13
1
\$\begingroup\$

q/kdb+, 19 13 bytes

Solution:

-1(2#10)#"*";

Example:

q)-1(2#10)#"*";
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

Explanation:

-1(2#10)#"*"; / solution
         "*"  / the asterisk
        #     / take or reshape
  (2#10)      / list (10;10), 10 rows, 10 columns
-1           ; / print to stdout
\$\endgroup\$
1
\$\begingroup\$

Pyth, 6 bytes

Bet there's going to be a shorter Charcoal answer

VT*T"*

Explanation:

VT     Ten times
 *T"*  Output ten asterisks followed by a newline

Try it online!

\$\endgroup\$
2
  • \$\begingroup\$ ---nope :|--- yep \$\endgroup\$
    – ASCII-only
    Sep 7, 2017 at 1:55
  • \$\begingroup\$ @ASCII-only why am i alive \$\endgroup\$
    – Stan Strum
    Sep 7, 2017 at 2:11
1
\$\begingroup\$

Java 8, 73 62 bytes

o is a String passed to a Consumer

o->System.out.print("**********".replace("*", "**********\n"))
\$\endgroup\$
1
  • \$\begingroup\$ o->System.out.print("**********".replace("*", "**********\n")) \$\endgroup\$
    – Nevay
    Sep 28, 2017 at 14:19
1
\$\begingroup\$

R: 33 bytes

write.matrix(matrix("*",10,10))

This does assume that you've loaded the MASS library.

\$\endgroup\$
1
\$\begingroup\$

Powershell, 78 bytes

$h=@();$v=@();1..10|% $_{$h+="*"};$i=0;Do{$v+=$h-join'';$i++}while($i-ne10);$v

Code explode:

$numbers = 1..10
$horArray = @()
$verArray = @()

Foreach ($number in $numbers){
    $horArray += "*"
}

$i = 0
Do {
    $verArray += $horArray -join ''
    $i++
}
while ($i -ne 10)
$verArray
\$\endgroup\$
1
\$\begingroup\$

Symbolic Python, 41 bytes

_=-~(_==_)
_=_**-~_+_
_=("*"*_+"""
""")*_

Try it online!

Explanation

  • _=-~(_==_) sets the value of _ to True, which is interchangeable with 1
  • _=_**-~_+_ performs 2**3+2, resulting in 10, and stores this in _
  • "*"*_ constructs a row, and +"""\n""" appends a newline.
  • This is multiplied by _, 10, to create the square.
  • At the end of execution, the value left in _ is printed.
\$\endgroup\$
1
\$\begingroup\$

Befunge, 33 bytes

55+:v
_v#:<,"*"-1
^_1-:!#@_55+:,1

Try It Online

How it works

55+:v
....<... Initialises the stack with two 10s as counters.
........ The first is for the outer loop, and the second is the inner

.....
_v#:<... Checks whether the inner counter is 0
._......

.....           If so, decrement the outer counter
_..........       If the outer counter is 0, exit the program
^_1-:!#@_55+:,1   Else print a newline and reset the outer loop counter

.....
.....,"*"-1 Else, decrement the inner counter and print an *
........... And run the inner loop again
\$\endgroup\$
1
\$\begingroup\$

Befunge-98, 19 15 bytes

a1_@#:-,ka:k8*'

Try It Online

First time using 98 instead of 93. Both the k and ' commands are immensely useful.

\$\endgroup\$
1
\$\begingroup\$

Kotlin, 41 bytes

Submission

fun f()=(0..9).map{println("**********")}

Test

fun f()=(0..9).map{println("**********")}

fun main(args: Array<String>) {
  f()
}

TryItOnline

Link

\$\endgroup\$
1
  • \$\begingroup\$ I believe lambda body is enough - 33 bytes \$\endgroup\$
    – mazzy
    Jul 2, 2018 at 8:38
1
\$\begingroup\$

Thue, 30 bytes

%::=~**********
::=
%%%%%%%%%%

Try it online!


Thue, 53 bytes

%::=,*,*
>*::=>[
>,::=>]]
[::=~
]::=~*****
::=
>%%%%%

Uses the convention that "when a string is sent to the output stream, no newline is printed at the end, except if the string is empty, in which case a newline is all that is printed." This is not followed by the TIO interpreter.

\$\endgroup\$
1
\$\begingroup\$

Io, 32 bytes

10repeat("*"repeated(10)println)

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Julia, 23 bytes

print(("*"^10*"\n")^10)

Explanation:

print(                ) # Print
      (           )     # A string consisting of 
       "*"^10           # "*" duplicated 10 times
             *"\n"      # then concatenated to a newline
                   ^10  # which is then duplicated 10 times
\$\endgroup\$
1
\$\begingroup\$

Canvas, 5 bytes

*A×A*

Try it online!

With notes (not valid code, as Canvas does not support comments):

          Print the following (implicit)
*         The string "*"
 A×      Duplicated 10 times horizontally
    A*  Duplicated 10 times vertically
\$\endgroup\$
1
\$\begingroup\$

Japt -R, 5 bytes

AÆAç*

Try it online!

Unpacked & How it works

Ao@Aç*

Ao@    Map range(10) with a function that returns...
Aç*      "*" repeated 10 times.
       `-R` flag joins the result array with "\n"
       Implicit output
\$\endgroup\$
1
\$\begingroup\$

Japt -R, 5 bytes

AÆAî*

Test it

\$\endgroup\$
1
\$\begingroup\$

Tcl, 25 bytes

time {puts **********} 10

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 2, 20 bytes

print("*"*10+"\n")*10

I didn't know you could remove the space between print and the string.

\$\endgroup\$
1
\$\begingroup\$

Q'Nial7, 15 bytes

10 10reshape'*'               #create atom '*', reshape to 10 by 10 matrix
\$\endgroup\$
1
\$\begingroup\$

Haskell, 34 bytes

putStr$[0..9]>>([0..9]>>"*")++"\n"

λ> putStr$[0..9]>>([0..9]>>"*")++"\n"
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
\$\endgroup\$
5
  • \$\begingroup\$ Since the question stipulates you have to print the text your likely need a main=. I'd also point out that "*********\n" is shorter than ([0..9]>>"*")++"\n". \$\endgroup\$
    – Wheat Wizard
    Jul 2, 2018 at 0:21
  • 1
    \$\begingroup\$ @CatWizard No need for main. Your proposal is already taken. \$\endgroup\$ Jul 2, 2018 at 0:24
  • \$\begingroup\$ @CatWizard I am running GHCi on my terminal. Isn't that allowed? \$\endgroup\$ Jul 2, 2018 at 0:39
  • \$\begingroup\$ GHCi is allowed. It is generally considered a different language from Haskell. If other people are not using main and no one has done anything I think you will fine though. \$\endgroup\$
    – Wheat Wizard
    Jul 2, 2018 at 0:42
  • \$\begingroup\$ @CatWizard I added a demo so that readers know this is to be used in an interpreter. \$\endgroup\$ Jul 2, 2018 at 0:46
1
\$\begingroup\$

Z80Golf, 14 bytes

00000000: 0a4f 3e2a 46ff 10fd 7eff 0d20 f576       .O>*F...~.. .v

Try it online!

Disassembly

start:
  ld a, (bc)  ; 0a
  ld c, a     ; 4f
loop1:
  ld a, '*'   ; 3e 2a
  ld b, (hl)  ; 46
loop2:
  rst $38     ; ff
  djnz loop2  ; 10 fd
  ld a, (hl)  ; 7e
  rst $38     ; ff
  dec c       ; 0d
  jr nz, loop1  ; 20 f5
  halt        ; 76

Golfing nested loops

Since a is the parameter to putchar, I used b and c for loop counters. The loop constructs are

  ld b, ... ; 1 or 2 bytes
loop:
  (loop content)
  djnz loop ; 2 bytes

and

  ld c, ... ; 1 or 2 bytes
loop:
  (loop content)
  dec c     ; 1 bytes
  jr nz, loop ; 2 bytes

This combination of 2-level nested loops is optimal in Z80Golf.

Reusing the first instruction as data

The first byte 0a is located at memory address $0000. Since hl is always zero in this program, we can use the one-byte instruction ld r, (hl) whenever we need the constant 0a, instead of the two-byte ld r, $0a.

In this particular challenge, the byte $0a has three uses: the outer loop, the inner loop, and the newline character. The byte as an instruction is not particularly useful, so we save two bytes here.

\$\endgroup\$
1
\$\begingroup\$

Matlab, 17 bytes

@()repmat('*',10)

Try it Online

\$\endgroup\$
1
\$\begingroup\$

ORK, 185 bytes

When this program starts:
I have a scribe called W
W is to write "**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********\n**********"

Try it online!

ORK does everything (even I/O and basic math) with objects. As far as this question goes, looping does not have to be one of them.

\$\endgroup\$
1
\$\begingroup\$

q, 12 bytes

10 10#get"*" / a 10x10 matrix
\$\endgroup\$
1
  • \$\begingroup\$ 10 10#(*) should have the same effect. But I'm not sure if spaces are allowed between asterisks. \$\endgroup\$
    – ngn
    Sep 28, 2018 at 1:24
1
\$\begingroup\$

MBASIC, 32 bytes

1 WIDTH 10:PRINT STRING$(100,42)

RUN
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********

No loop. Just printing 100 asterisks into a 10 character wide terminal to force line wraps.

There's no MBASIC on TIO, so I've linked the reference manual.

\$\endgroup\$
1
\$\begingroup\$

Premier, 8 bytes

'*U*N+U*

Try it online!

The boring way.

Explanation

'*U*N+U*     full program
'*           push "*"
  U          push 10
   *         repeat
    N+       append newline
      U      push 10
       *     repeat

Premier, 14 bytes

QnN
**********

Try it online! This second approach uses Premier's implicit data iteration. It iterates over each character in the data (the second line), and executes the program (the first line) for each character. The data QnN pushes the data (Q), outputs it (n), then pushes a newline (N), to be implicitly outputted. This iterates 10 times, printing a 10x10 grid of asterisks.

\$\endgroup\$
1
\$\begingroup\$

Excel VBA, 11 Bytes

Anonymous VBE immediate window function that outputs a 10x10 grid of * to the ActiveSheet object

[A1:J10]="*
\$\endgroup\$
1
\$\begingroup\$

Perl 5, 66 bytes

$x = 1x10;$x =~ s/\d/*/g;for($i = 1; $i <=10; $i++){print "$x\n";}

Try it online!

\$\endgroup\$
1
  • 2
    \$\begingroup\$ You can remove literally all the whitespace \$\endgroup\$
    – Jo King
    Feb 19, 2019 at 23:59
1
\$\begingroup\$

MathGolf, 7 bytes

♂⌂*n+♂*

Explanation

♂⌂*     Push asterisk multiplied by 10("**********")
   n    Append a newline
    +   Concatenate the two strings
     ♂* Multiply the string by 10
implicit output

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Python 3, 23 bytes

print(10*(10*"*"+"\n"))

Try it online!

In this case, the obvious answer is best. I tried some other ways, but all were equal or longer in length.
Let me know if you have any answers to add.

23 bytes (alternative)

print(10*f"{10*'*'}\n")

24 bytes

print(10*"**********\n")

25 bytes

exec(10*"print(10*'*');")

29 bytes

print("\n".join(10*[10*"*"]))

32 bytes

for _ in range(10):print(10*"*")

43 bytes

print(*(10*"*"for _ in range(10)),sep="\n")
\$\endgroup\$
1
\$\begingroup\$

PHP, 26 bytes

Just used a compressed string. Has raw output of gzdeflate and contains unprintable binary data, so the code itself and direct TIO link cannot be posted here, but here is a hex dump of it:

000000  3c 3f 3d 67 7a 69 6e 66 6c 61 74 65 28 22 d3 d2  <?=gzinflate("..
000010  82 01 2e 2d 9a 33 01 22 29 3b                    ...-.3.");

Save it as a binary file then execute it like this: php asterisks.php

TIO which uses bash to run this file: Try it online!

\$\endgroup\$
1
6 7
8
9 10
13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.