111
\$\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 '16 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 '16 at 12:58
  • 7
    \$\begingroup\$ @LeakyNun Leaderboard snippet please! \$\endgroup\$
    – anna328p
    Aug 4 '16 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 '16 at 9:14
  • 2
    \$\begingroup\$ you say trailing new lines are acceptable. Are leading newlines acceptable too? \$\endgroup\$ Feb 10 '17 at 2:34

375 Answers 375

1
9 10 11 12
13
0
\$\begingroup\$

A0A0, 145 bytes

A0A0
A0C3G1G1G1G1G1G1G1G1G1G1G1A0
A0P42P42P42P42P42P42P42P42P42P42G3P10A0
A0A1G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3A0
G-3
G-2G-2G-2G-2G-2G-2G-2G-2G-2

The top five lines are for the loop construction. A0A0 deletes instructions whenever it runs them, so those lines ensure that the loop keeps running. On line 3 are ten P42 instructions which print * (42 is the ascii code for an asterisk). The P10 instruction prints a new line.

The bottom line acts as a counter. For every execution of the loop it jumps to there and then jumps back to the loop. This consumes an instruction on that line. There are exactly nine instructions, so when it tries to go there when it has already printed ten lines the program will halt execution (since an empty line swill stop execution in A0A0).

\$\endgroup\$
0
\$\begingroup\$

Pascal (FPC), 57 bytes

var i:word;begin for i:=0to 9do writeln('**********')end.

Try it online!

This is probably the best one as I don't see how this can be improved in 9 characters or less which could be gained by removing *s.

\$\endgroup\$
0
\$\begingroup\$

Assembly (NASM, 32-bit, Linux), 122 bytes

mov ecx,10
n:push 0xa202a2a
push '****'
push '****'
loop n
mov edx,120
mov ecx,esp
mov ebx,1
mov eax,4
int 128
add esp,120

Try it Online!

\$\endgroup\$
2
  • \$\begingroup\$ If you're scoring by assembler code and not machine code, use some NASM magic \$\endgroup\$
    – EasyasPi
    Jan 22 at 20:56
  • \$\begingroup\$ @EasyasPi I try to make it portable \$\endgroup\$ Jan 25 at 15:32
0
\$\begingroup\$

Java 11, 48 chars

Method String.repeat​(int)

for(int i=0;i++<10;out.println("*".repeat(10)));

Try it online!


Java 5, 53 chars

Static imports

for(int i=0;i++<100;out.print("*"+(i%10>0?"":"\n")));

Try it online!


Java 3, 60 chars

Plain old Java

for(int i=0;i++<100;System.out.print("*"+(i%10>0?"":"\n")));

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Lua, 31 bytes

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

Try it online!

Not very impressive but I think simpler is shorter in this case.

\$\endgroup\$
0
\$\begingroup\$

x86_16 machine code - 37 bytes

B4 0E            MOV AH, 0EH
B9 000A          MOV CX, 10
8B D1            MOV DX, CX
8B D9            MOV BX, CX

             .LOOP:
B0 2A            MOV AL, "*"
CD 10            INT 10H
E3 02            JCXZ .NEWLINE
E2 F8            LOOP .LOOP

             .NEWLINE:
B0 0A            MOV AL, 10
CD 10            INT 10H
B0 0D            MOV AL, 13
CD 10            INT 10H

8B CB            MOV CX, BX
4A               DEC DX
74 02            JZ .EXIT
75 E9            JNZ .LOOP

             .EXIT:
B8 004C          MOV AX, 4C00H
CD 21            INT 21H

Running using DOSBox :

10-GRID Screenshoot

\$\endgroup\$
0
\$\begingroup\$

Java, 46 bytes

$->System.out.print("**********\n".repeat(10))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Twue, 31 bytes

a::~**********\n
::=
aaaaaaaaaa

Try it on the website!

Simply defines the replacement a which maps to printing 10 asterisks. The main body consists of 10 as. Trying a secondary replacement b::=aa or b::=aaaaa both yield 33 bytes, which is unfortunately longer.

Whereas in some Thue interpreters, outputting implicitly outputs a newline, that is not the case in Twue, which must manually insert the newline.

\$\endgroup\$
0
\$\begingroup\$

Ulpia, 41 bytes

"n*\n"for 0_?(0 0_1-=0_11%1<1+_->up)[hlt]

Explanation:

; counter == 'n' == 110 == 10 * 10 asterisks + 10 newlines and...
; ..."array" of characters to-be used, (starts at index = 1)
"n*\n"
for 0_ ?(  ; while the counter is not zero...
    0 (0_ 1 -) =  ; decrement it
    ; if counter is divisible by 11, the condition returns 1 and we get "\n"
    ; from the array, otherwise we get an "*"
    (((0_ 11 %) 1 <) 1 +) _ ->
up)  ; go up the nearest `for`
; when loop ends we halt
[hlt]
\$\endgroup\$
0
\$\begingroup\$

Python 3, 34 bytes

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

Try it online!

\$\endgroup\$
3
  • \$\begingroup\$ Welcome to Code Golf! I've edited your answer to make it a bit clearer and added a link to try it. There's a couple similar Python answers, but nice first answer! \$\endgroup\$ Oct 1 at 16:09
  • \$\begingroup\$ You can save some bytes by inlining the for block, so it can be one line like for i in range(10):print("*"*10) \$\endgroup\$
    – hyper-neutrino
    Oct 1 at 16:09
  • \$\begingroup\$ Also, make sure to check out our Tips for golfing in Python to see if there are any other ways to golf your code. \$\endgroup\$ Oct 1 at 16:10
0
\$\begingroup\$

Subleq (8 bit), 18 bytes

 2 -1 42
 1 17  0
15 -1 -9
15 17 12
 1  8  0
10 10 -9

Explanation

 2 -1 42 Print [2]                               // [2] = 42, ASCII(42) = "*"
 1 17  0 [17] = [17] - [1]  if [17] <= 0 goto 0  // Loop 10 times
15 -1 -9 Print [15]                              // [15] = 10 ASCII(10) = <line feed> 
15 17 12 [17] = [17] - [15] if [17] <= 0 goto 12 // Set [17] back to -9
 1  8  0 [8]  = [8]  - [1]  if [8]  <= 0 goto 0  // Loop 10 times
10 10 -9 [10] = [10] - [10] if [10] <= 0 Exit    // Exit program
\$\endgroup\$
0
\$\begingroup\$

SNOBOL4 (CSNOBOL4), 43 bytes

Outputs two trailing LFs.

 output =dupl(dupl('*',10) char(10),10)
end

Try it online!

\$\endgroup\$
0
\$\begingroup\$

jq, 16 bytes

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

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Python 3, 25 24 bytes

-1 byte thanks to @Jo King

*map(print,['*'*10]*10),

Try it online!

Unpacks the map object so it actually prints the output, instead of optimising it away.

\$\endgroup\$
2
  • \$\begingroup\$ You can actually do *map(...), instead of the brackets \$\endgroup\$
    – Jo King
    12 hours ago
  • \$\begingroup\$ In place tuple expansion. I like it. \$\endgroup\$ 12 hours ago
-2
\$\begingroup\$

HTML, 136 bytes

**********<br>**********<br>**********<br>**********<br>**********<br>**********<br>**********<br>**********<br>**********<br>**********
\$\endgroup\$
6
  • 2
    \$\begingroup\$ You can use <pre> \$\endgroup\$
    – Leaky Nun
    Aug 5 '16 at 16:45
  • 10
    \$\begingroup\$ This answer is valid and allowed, but it's also extremely boring and unimaginative. \$\endgroup\$
    – DJMcMayhem
    Aug 5 '16 at 17:08
  • 1
    \$\begingroup\$ Ok, Can I post it in Markdown? \$\endgroup\$
    – Skxrda
    Aug 5 '16 at 17:08
  • 3
    \$\begingroup\$ You can easily get this to 114 bytes with <pre> followed by the 109-byte text... \$\endgroup\$
    – Timtech
    Aug 27 '16 at 21:43
  • 2
    \$\begingroup\$ I was hoping to join in on this one but unfortunately do not have the reputation for here yet. I'm not sure if this is an applicable method, but using html/emmet you can get this down to 20 bytes. ({**********}+br)*10 Alternatively p{**********}*10 is 16 bytes. New user to this site, so I'm unsure if the tab character counts. \$\endgroup\$
    – N.J.Dawson
    Sep 1 '16 at 16:12
1
9 10 11 12
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.