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

413 Answers 413

1
3 4
5
6 7
14
2
\$\begingroup\$

Notepad++, ??? keystrokes

I have no idea how to score this, but I decided to give it a shot since there's an answer on Notepad.

Here's the sequence:

* * * * * CTRL (hold) A D D D D D D D D D D

\$\endgroup\$
2
  • \$\begingroup\$ If codegolf.stackexchange.com/a/88747/14732 is valid (and has upvotes), this is more than perfectly valid. \$\endgroup\$ Commented Aug 4, 2016 at 20:27
  • 2
    \$\begingroup\$ @Mego As long as that one exists, this one will exist as well \$\endgroup\$ Commented Aug 5, 2016 at 7:47
2
\$\begingroup\$

Bash (pure), 30 bytes

printf '%.s**********
' {0..9}

Test it on Ideone.

How it works

Before calling the shell built-in printf, Bash expands the glob {0..9} to 0 1 2 3 4 5 6 7 8 9.

The format string

%.s**********
 

specifies a string whose first 0 characters are included in the output (%.s), followed by ten asterisks and a linefeed. printf repeats the format string as many times as needed to consume all arguments. Since .%s is an empty string, this results in the desired output.

\$\endgroup\$
2
\$\begingroup\$

Clojure, 38 bytes

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

Simple enough

\$\endgroup\$
2
  • \$\begingroup\$ I'm afraid, that should be 38 bytes. Currently it prints only 8 x 8. \$\endgroup\$
    – manatwork
    Commented Aug 5, 2016 at 13:17
  • \$\begingroup\$ You are completely right. I keep posting wrong cases this whole day. \$\endgroup\$
    – Michael M
    Commented Aug 5, 2016 at 13:52
2
\$\begingroup\$

Underload, 22 bytes

(*****):*(
)*:*::*:**S
\$\endgroup\$
0
2
\$\begingroup\$

Scheme, 51 Bytes

(map (lambda (n)(display "**********\n"))(iota 10))
\$\endgroup\$
2
\$\begingroup\$

Ruby, 26 20 bytes

10.times{puts'*'*10}

Try it online!

Explanation

Runs 10 times; each time, it prints * (10 times), with an automatic line break at the end.

Kudos to OrangeFlash81 for saving me 6 bytes.

\$\endgroup\$
1
  • \$\begingroup\$ I think 10.times{puts'*'*10} works as well and is shorter. \$\endgroup\$ Commented Aug 7, 2016 at 8:26
2
\$\begingroup\$

Xtend, 33 bytes

[for(_:0..9)print('**********
')]

... a Java dialect; [] is a lambda definition

\$\endgroup\$
2
\$\begingroup\$

TI-Basic, 21 bytes

For(I,0,9
Disp "**********
End
\$\endgroup\$
2
\$\begingroup\$

Deadfish ~, 24 bytes

{{iiii}ii{c}{ddd}ddc{d}}

how it works:

{foo} does foo exactly ten times. first, it starts a ten loop, in this loop, it sets the counter to 42, prints it ten times (as char (asterisk)), decrements it to 10, prints it (newline), subtracts 10, then loops again, doing this 10 times.

\$\endgroup\$
1
  • \$\begingroup\$ 23 bytes by setting the counter to 10 initially \$\endgroup\$
    – ovs
    Commented Sep 22, 2021 at 7:50
2
\$\begingroup\$

GNU sed, 27 bytes

s/^/**********/;h;H;G;H;g;G

To add to the diversity of languages used, I present a sed solution. The code works as follows, indicating the number of sets of 10 asterisks in pattern space and then in hold space:

s/^/**********/        # 10 asterisks are added to pattern space      (1 0)
h                      # overwrites hold space with pattern space     (1 1)
H                      # appends pattern space to hold space          (1 2)
G                      # appends hold space to pattern space          (3 2)
H                      #                                              (3 5)
g                      # overwrites pattern space with hold space     (5 5)
G                      # after G, pattern space is printed on exit   (10 5)
\$\endgroup\$
2
\$\begingroup\$

BrainFuck, 68 Bytes

++[>+++++<-]++[>>>+++++<<<-]++++++[>>+++++++<<-]>[>..........>.<<-]

BrainFuck is fun, this is me practicing my skills.

\$\endgroup\$
2
\$\begingroup\$

SmileBASIC, 19 bytes

?("*"*10+" "*40)*10

SB has a 50 character wide console, so printing 40 spaces after the 10 asterisks will bring the cursor to the beginning of the next line

\$\endgroup\$
2
\$\begingroup\$

Google Sheets, 31 23 bytes

Code:

In cell A1 (12 bytes):

=REPT(B1,10)

In cell B1 (11 bytes):

**********        # Contains New Line
                  # <--

Result:

enter image description here

Previous Attempt #1 (31 bytes):

=REPT(REPT("*",10)&CHAR(10),10)

Previous Attempt #2 (also 31 bytes):

=REPT("**********"&CHAR(10),10)
\$\endgroup\$
2
\$\begingroup\$

S.I.L.O.S 45 bytes

All whitespace necessary due to my poor interpreter design.

a = 10
lbla
printLine **********
a - 1
if a a

Try it Online!

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

Lua, 34 bytes

for i=1,10 do print"**********"end
\$\endgroup\$
2
\$\begingroup\$

Befunge-93 (PyFunge), 28 bytes

#<90v0-1_@#:,
#^_ >1+"*",:9`

Try it online!

\$\endgroup\$
2
\$\begingroup\$

Javascript (REPL), 27 bytes, Old is 32 bytes. Thanks to @Peter

('**********\n').repeat(10)
\$\endgroup\$
4
  • 1
    \$\begingroup\$ Welcome to PPCG! Please note that we count REPL submissions differently from full-program solutions, so this should be marked as "Javascript (REPL)". \$\endgroup\$
    – user45941
    Commented Feb 13, 2018 at 2:32
  • \$\begingroup\$ @Mego Thank you, I will remember this! \$\endgroup\$
    – NTCG
    Commented Feb 13, 2018 at 2:49
  • \$\begingroup\$ ('**********\n').repeat(10) is shorter :) \$\endgroup\$
    – Peter
    Commented Feb 13, 2018 at 21:27
  • \$\begingroup\$ Welcome to PPCG :) Note that this solution already exists: codegolf.stackexchange.com/a/88660/58974. However, you can remove the brackets around the string and use a template literal with a literal newline to save 3 bytes: tio.run/##y0osSyxOLsosKNHNy09J/Z@cn1ecn5Oql5OfrvE/… \$\endgroup\$
    – Shaggy
    Commented Feb 15, 2018 at 11:54
2
\$\begingroup\$

Kotlin (script), 32 bytes

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

Try it online!

Run this locally as a standalone program by

  • installing Kotlin
  • saving the source to a .kts file
  • run with kotlinc -script myfile.kts

This does not seem to work on TIO, for whatever reason, which is why I had to include the header/footer.

\$\endgroup\$
3
  • \$\begingroup\$ You could run this as a standalone program using the -script command line switch on kotlin. This doesn't work in TIO/kotlinc though. \$\endgroup\$
    – snail_
    Commented May 25, 2018 at 19:01
  • \$\begingroup\$ I wasn't aware this is possible. If you want to link the script version on TIO, you could do it this way. \$\endgroup\$
    – ovs
    Commented May 25, 2018 at 19:35
  • \$\begingroup\$ {print("**********\n".repeat(10))} \$\endgroup\$
    – mazzy
    Commented Jul 2, 2018 at 8:18
2
\$\begingroup\$

Kotlin, 62 bytes

fun main(a:Array<String>){for(i in 0..9)println("**********")}

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ your golfed version 37 bytes: {for(i in 0..9)println("**********")}. -3 byte version {print("**********\n".repeat(10))} ))) \$\endgroup\$
    – mazzy
    Commented Jul 2, 2018 at 8:15
2
\$\begingroup\$

05AB1E, 9, 8 bytes

Tƒ,TF'*+

my first golfing answer so any tips are welcome

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ That's a grid of 10x9, not 10x10 \$\endgroup\$
    – mbomb007
    Commented Jul 2, 2018 at 14:34
  • \$\begingroup\$ didn't notice that, fixed it @mbomb007 \$\endgroup\$
    – Tomb
    Commented Jul 2, 2018 at 14:41
2
\$\begingroup\$

Java (OpenJDK 8), 61 bytes

static{for(int i=0;i++<10;)System.out.println("**********");}

Try it online!

\$\endgroup\$
2
\$\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\$
2
\$\begingroup\$

MathGolf, 6 bytes

⌂♀*♂/n

Try it online!

Explanation

⌂       Push "*"
 ♀*     Multiply by 100
   ♂/   Divide into chunks of 10
     n  Join with newline

With an output control flag this could be 5 bytes, but I don't have that yet.

\$\endgroup\$
2
  • \$\begingroup\$ Out of curiosity, why is there a single-byte builtin to push the * character? Or are there builtins for *, +, -, and / and some kind of eval functionality for a mathematical expression in string format? \$\endgroup\$ Commented Sep 20, 2018 at 15:26
  • 2
    \$\begingroup\$ When I made the language, I wanted one single printable character to satisfy printing challenges where any character is valid. Luckily enough, I chose the asterisk! I also have a space character as a single command. \$\endgroup\$
    – maxb
    Commented Sep 20, 2018 at 19:01
2
\$\begingroup\$

Shakespeare Programming Language, 387 bytes

,.Ajax,.Ford,.Act I:.Scene I:.[Exeunt][Enter Ajax and Ford]Ford:You be twice the sum ofa cat twice twice the sum ofa cat a big big cat.Speak thy.Speak thy.Speak thy.Speak thy.Speak thy.Speak thy.Speak thy.Speak thy.Speak thy.Speak thy.You be twice the sum ofa cat a big big cat.Speak thy.Ajax:You be the sum ofyou a cat.Be you nicer the sum ofa cat a big big big cat?If notlet usScene I.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ 352 bytes \$\endgroup\$
    – Jo King
    Commented Sep 27, 2018 at 17:24
2
\$\begingroup\$

Stax, 4 bytes

╪n│╔

Run and debug it

\$\endgroup\$
2
\$\begingroup\$

R 33 bytes

cat(rep('**********',10),sep='
')

Try it online

I believe this is shorter than the other R answers on this question.

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I believe this is shorter than yours, but Mamie's answer on the first page (sorted by oldest) is something like 26 bytes if the comments are to be believed. \$\endgroup\$
    – Giuseppe
    Commented Feb 19, 2019 at 22:14
  • \$\begingroup\$ Ah, I'd missed that one, thanks @Giuseppe. \$\endgroup\$
    – CT Hall
    Commented Feb 19, 2019 at 22:31
2
\$\begingroup\$

Gol><>, 9 bytes

aFa:R`*|H

There is a trailing newline, but the specs say it is okay. Below is a version that doesn't have a trailing new line, but adds on 2 extra bytes.

aFaR`*a|~rH

Try it online!

\$\endgroup\$
2
\$\begingroup\$

C# (.NET Core), 53 bytes

Brand new to code golf, is this type of format allowed to be used (Usage of the header section / footer section)?

for(int i=0;i<10;i++){Console.Write("**********\n");}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Welcome to PPCG, and Great First Answer \$\endgroup\$ Commented Feb 19, 2019 at 22:46
  • \$\begingroup\$ You can get it down to 49 bytes by moving the i++ into the conditional expression, and moving the block body into the increment statement: for(int i=0;i++<10;Console.Write("**********\n")); (try it online) \$\endgroup\$
    – mgthomas99
    Commented Feb 20, 2019 at 14:47
  • 1
    \$\begingroup\$ This is not valid since it's neither a function (lambdas count) nor is it a full program. With a few adjustments though, 42 \$\endgroup\$
    – ASCII-only
    Commented Feb 20, 2019 at 16:29
  • \$\begingroup\$ @mgthomas99 btw that's 50 bytes, and it makes no difference whether the write is inside or outside \$\endgroup\$
    – ASCII-only
    Commented Feb 20, 2019 at 16:30
2
\$\begingroup\$

Perl 5, 18 Bytes

say'*'x10for 1..10

Requires the -E flags, as in:

perl -E "say'*'x10for 1..10"
\$\endgroup\$
2
\$\begingroup\$

Forth (gforth), 36 34 bytes

: f 9 for ." **********" cr next ;

Try it online!

-2 bytes thanks to @bubbler

Explanation

 10 0            \ put 10 and 0 on the stack
 do              \ begin a loop from 0 to 10 (0 inclusive 10 exclusive)
 ." **********"  \ print the string literal ********** (the initial space is required because forth words are space separated, and ." is not an exception)
 cr              \ print out a new line character
 loop            \ end the loop
\$\endgroup\$
1
  • \$\begingroup\$ Using for..next loop is 2 bytes shorter: Try it online! \$\endgroup\$
    – Bubbler
    Commented Oct 12, 2019 at 8:13
1
3 4
5
6 7
14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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