80
\$\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.

Leaderboard

Here is a Stack Snippet to generate both a regular leaderboard and an overview of winners by language.

/* Configuration */

var QUESTION_ID = 88653; // Obtain this from the url
// It will be like https://XYZ.stackexchange.com/questions/QUESTION_ID/... on any question page
var ANSWER_FILTER = "!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe";
var COMMENT_FILTER = "!)Q2B_A2kjfAiU78X(md6BoYk";
var OVERRIDE_USER = 48934; // This should be the user ID of the challenge author.

/* App */

var answers = [], answers_hash, answer_ids, answer_page = 1, more_answers = true, comment_page;

function answersUrl(index) {
  return "https://api.stackexchange.com/2.2/questions/" +  QUESTION_ID + "/answers?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + ANSWER_FILTER;
}

function commentUrl(index, answers) {
  return "https://api.stackexchange.com/2.2/answers/" + answers.join(';') + "/comments?page=" + index + "&pagesize=100&order=desc&sort=creation&site=codegolf&filter=" + COMMENT_FILTER;
}

function getAnswers() {
  jQuery.ajax({
    url: answersUrl(answer_page++),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      answers.push.apply(answers, data.items);
      answers_hash = [];
      answer_ids = [];
      data.items.forEach(function(a) {
        a.comments = [];
        var id = +a.share_link.match(/\d+/);
        answer_ids.push(id);
        answers_hash[id] = a;
      });
      if (!data.has_more) more_answers = false;
      comment_page = 1;
      getComments();
    }
  });
}

function getComments() {
  jQuery.ajax({
    url: commentUrl(comment_page++, answer_ids),
    method: "get",
    dataType: "jsonp",
    crossDomain: true,
    success: function (data) {
      data.items.forEach(function(c) {
        if (c.owner.user_id === OVERRIDE_USER)
          answers_hash[c.post_id].comments.push(c);
      });
      if (data.has_more) getComments();
      else if (more_answers) getAnswers();
      else process();
    }
  });  
}

getAnswers();

var SCORE_REG = /<h\d>\s*([^\n,]*[^\s,]),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/;

var OVERRIDE_REG = /^Override\s*header:\s*/i;

function getAuthorName(a) {
  return a.owner.display_name;
}

function process() {
  var valid = [];
  
  answers.forEach(function(a) {
    var body = a.body;
    a.comments.forEach(function(c) {
      if(OVERRIDE_REG.test(c.body))
        body = '<h1>' + c.body.replace(OVERRIDE_REG, '') + '</h1>';
    });
    
    var match = body.match(SCORE_REG);
    if (match)
      valid.push({
        user: getAuthorName(a),
        size: +match[2],
        language: match[1],
        link: a.share_link,
      });
    
  });
  
  valid.sort(function (a, b) {
    var aB = a.size,
        bB = b.size;
    return aB - bB
  });

  var languages = {};
  var place = 1;
  var lastSize = null;
  var lastPlace = 1;
  valid.forEach(function (a) {
    if (a.size != lastSize)
      lastPlace = place;
    lastSize = a.size;
    ++place;
    
    var answer = jQuery("#answer-template").html();
    answer = answer.replace("{{PLACE}}", lastPlace + ".")
                   .replace("{{NAME}}", a.user)
                   .replace("{{LANGUAGE}}", a.language)
                   .replace("{{SIZE}}", a.size)
                   .replace("{{LINK}}", a.link);
    answer = jQuery(answer);
    jQuery("#answers").append(answer);

    var lang = a.language;
    if (/<a/.test(lang)) lang = jQuery(lang).text();
    
    languages[lang] = languages[lang] || {lang: a.language, user: a.user, size: a.size, link: a.link};
  });

  var langs = [];
  for (var lang in languages)
    if (languages.hasOwnProperty(lang))
      langs.push(languages[lang]);

  langs.sort(function (a, b) {
    if (a.lang > b.lang) return 1;
    if (a.lang < b.lang) return -1;
    return 0;
  });

  for (var i = 0; i < langs.length; ++i)
  {
    var language = jQuery("#language-template").html();
    var lang = langs[i];
    language = language.replace("{{LANGUAGE}}", lang.lang)
                       .replace("{{NAME}}", lang.user)
                       .replace("{{SIZE}}", lang.size)
                       .replace("{{LINK}}", lang.link);
    language = jQuery(language);
    jQuery("#languages").append(language);
  }

}
body { text-align: left !important}

#answer-list {
  padding: 10px;
  width: 290px;
  float: left;
}

#language-list {
  padding: 10px;
  width: 290px;
  float: left;
}

table thead {
  font-weight: bold;
}

table td {
  padding: 5px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b">
<div id="answer-list">
  <h2>Leaderboard</h2>
  <table class="answer-list">
    <thead>
      <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr>
    </thead>
    <tbody id="answers">

    </tbody>
  </table>
</div>
<div id="language-list">
  <h2>Winners by Language</h2>
  <table class="language-list">
    <thead>
      <tr><td>Language</td><td>User</td><td>Score</td></tr>
    </thead>
    <tbody id="languages">

    </tbody>
  </table>
</div>
<table style="display: none">
  <tbody id="answer-template">
    <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>
<table style="display: none">
  <tbody id="language-template">
    <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr>
  </tbody>
</table>

\$\endgroup\$
  • \$\begingroup\$ 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'? \$\endgroup\$ – Dylan Meeus Aug 4 '16 at 12:37
  • 2
    \$\begingroup\$ @DylanMeeus "You are to print this exact text:" \$\endgroup\$ – Leaky Nun Aug 4 '16 at 12:56
  • 12
    \$\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\$ – Trotski94 Aug 4 '16 at 12:58
  • 6
    \$\begingroup\$ @LeakyNun Leaderboard snippet please! \$\endgroup\$ – dkudriavtsev Aug 4 '16 at 22:08
  • 1
    \$\begingroup\$ you say trailing new lines are acceptable. Are leading newlines acceptable too? \$\endgroup\$ – Albert Renshaw Feb 10 '17 at 2:34

264 Answers 264

1
\$\begingroup\$

Swift, 35 Bytes

for _ in 0...9{print("**********")}
\$\endgroup\$
1
\$\begingroup\$

Brachylog, 15 bytes

9ye,"*":9jw@Nw\

Try it online!

Explanation

This exploits backtracking to loop; we create ten useless choice points at the beginning so that it writes ten times the string "**********\n".

9ye,         Pick a number between 0 and 9 (there are thus 10 choice points here)
"*":9j       Concatenate "*" 9 times to itself
w@Nw         Write that string and write a line break
\            False (trigger backtracking: pich another one of the ten numbers)
\$\endgroup\$
1
\$\begingroup\$

MarioLANG, 101 99 95 bytes

Saved 2 bytes thanks to Martin Ender.

Try it Online!

+
++++)+<......(<
++====".======"
+>)+(()>...(.
+====(-=====) -
+++++[!))++ )[!
======#=======#

The left half sets the tape to 10 40 10

The second half increases the 40 to 42, then prints the 42 (*) 10 times, and a newline, and repeats that 10 times.

\$\endgroup\$
  • \$\begingroup\$ Link? – – – – – \$\endgroup\$ – Leaky Nun Aug 5 '16 at 17:01
  • \$\begingroup\$ @LeakyNun Sorry I completely forgot. Edited it in. \$\endgroup\$ – Business Cat Aug 5 '16 at 17:06
1
\$\begingroup\$

Convex, 10 bytes

A'**{A*N}%

Try it online!

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

Grocery List, 72 bytes

G

number ten
v
z
v
P
s
u
c
l
number ten
l
b
c
p
u
n
s
e
x
u
c
p
b
n
s
e

Try it online

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

Awk, 37 35 characters

BEGIN{for(OFS="*";++i<NF=11;)print}

Thanks to:

  • Cabbie407 for combining the OFS and loop-based solutions (-2 characters)

Sample run:

bash-4.3$ awk 'BEGIN{for(OFS="*";++i<NF=11;)print}'
**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
\$\endgroup\$
  • 1
    \$\begingroup\$ I managed to shorten it by 2 bytes by combining both approaches BEGIN{for(OFS="*";++i<NF=11;)print} \$\endgroup\$ – Cabbie407 Aug 12 '16 at 19:13
  • \$\begingroup\$ Wow! Great catch, @Cabbie407. Thank you. \$\endgroup\$ – manatwork Aug 13 '16 at 10:12
1
\$\begingroup\$

LaTeX, 122 110 105 bytes

-12 bytes thanks to @Leaky Nun
-5 bytes by changing package & document type

Or, if I'm allowed to skip the document class definition & setup, and just count the package import and for-loop code: 55 bytes.

I still need to check if there's another package that has a shorter loop syntax, but for now, this simply uses pgffor (from tikz) and a foreach:

\documentclass{book}\usepackage{tikz}\begin{document}\foreach\n in{0,...,9}{**********\par}\end{document}

Ungolfed:

\documentclass{book}
\usepackage{tikz}
\begin{document}
\foreach\n in{0,...,9}{**********\par}
\end{document}

Output (w/ free page number :) ): enter image description here

\$\endgroup\$
  • 1
    \$\begingroup\$ ********** is shorter than \foreach\n in{0,...,9}{*} \$\endgroup\$ – Leaky Nun Aug 14 '16 at 13:54
  • \$\begingroup\$ Good point! Not sure how I missed that... Must've been too focused on getting the nested loop going :) Thanks! \$\endgroup\$ – MH. Aug 14 '16 at 14:10
  • \$\begingroup\$ \\ is shorter than \par \$\endgroup\$ – Leaky Nun Aug 14 '16 at 14:11
  • \$\begingroup\$ True, but then the result is no longer a proper grid as the first line will be indented. \$\endgroup\$ – MH. Aug 14 '16 at 14:27
1
\$\begingroup\$

Fortran, 68 51 49 bytes

Skimmed off 17 bytes, because who needs indents and spaces?

-2 bytes thanks to @Joffan

As a tribute to senior and the fact there was no Fortran answer yet. Well, now I know why: this is a pretty hard language to golf! Just one or two indents already take up more bytes than the shortest answer here :)).

program T
do i=0,9
print*,('*',j=0,9)
end do
end

Try it! (ideone)

I tried golfing it down by introducing a nested 'implied' do loop, but the newline character wasn't available until Fortran 2003. Also, it's insanely verbose...

\$\endgroup\$
  • \$\begingroup\$ Loops using 0,9 limits skim off another couple? And make the positive case for not using '**********' :-) \$\endgroup\$ – Joffan Aug 19 '16 at 20:14
  • \$\begingroup\$ Thanks! Not sure how I didn't think of that. Must've had a temporary brain meltdown :/ \$\endgroup\$ – MH. Aug 19 '16 at 21:00
1
\$\begingroup\$

GolfScript, 27 bytes

0{"**********\n"\1+.10<}do;

Try it online!

Explanation

0              # Push 0 onto the stack: counter
{
"**********\n" # Push 10 '*' and a newline character
\              # Moves the counter onto the stack
1+             # Increments the counter
.10<           # Check if it's smaller than 10
}do;           # Repeat until counter equals 10
\$\endgroup\$
  • 1
    \$\begingroup\$ You know, you can just do this \$\endgroup\$ – Leaky Nun Aug 28 '16 at 19:26
  • \$\begingroup\$ I'm a newbie, I didn't know, thank you! \$\endgroup\$ – FedeWar Aug 28 '16 at 19:27
  • \$\begingroup\$ Another byte saved \$\endgroup\$ – Leaky Nun Aug 28 '16 at 19:28
  • \$\begingroup\$ 12 bytes now \$\endgroup\$ – Leaky Nun Aug 28 '16 at 19:29
  • \$\begingroup\$ Wow, I still have a lot to learn. \$\endgroup\$ – FedeWar Aug 28 '16 at 19:30
1
\$\begingroup\$

S.I.L.O.S, 41 bytes

Never used S.I.L.O.S before, can probably be golfed more.

EDIT: Looks like there was already a S.I.L.O.S answer, but it didn't show up on the leaderboard. But I'll keep this answer because it's shorter.

a=10
lblb
printLine **********
a-1
if a b

Try it online!

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

BrainFuck, 68 Bytes

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

BrainFuck is fun, this is me practicing my skills.

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

Japt, 9 bytes

Ao ç*pA)·

Test it online! This is, in fact, competing.

How it works

Ao ç*pA)·  // Implicit: A = 10
Ao         // Create the range [0..10).
   ç*pA)   // Fill with "*".repeat(A).
        ·  // Join with newlines. Implicitly print.
\$\endgroup\$
  • \$\begingroup\$ Can be 7 bytes these days! \$\endgroup\$ – Shaggy Feb 2 '18 at 17:45
1
\$\begingroup\$

Carrot, 10 9 bytes, non-competing

*^*9^
^*9

Try it online! (copy & paste only)

*^                //pushes "*" to the stack
*9                //add 9 more copies of itself
                  //stack = "**********"
^\n^              //append a newline to it
*9                //add 9 more copies of the resulting string
                  //implicit output
\$\endgroup\$
1
\$\begingroup\$

tinylisp, 62 bytes

(d A(q((n *)(i n(i(disp *)0(A(s n 1)*))*))))
(A 9(q **********

Try it online!

We define a function A with two parameters: n is the number of iterations, and * is a row of asterisks (it will always be ten of them, but it saves bytes to pass that as an argument). If n is truthy (nonzero), we display the asterisk row (with trailing newline) and recurse with n minus 1. If n is falsey (zero), we simply return the asterisk row.

Calling the function with n = 9 results in 9 disp calls; the return value of the function is then displayed, giving us our 10th row.

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

Syms 1.3, 17 bytes (noncompeting)

{*}10;*{
}~+10;*>

Explanation:

{*}10;*{\n}~+10;*>
{*}                 Pushes "*"
   10;*             Repeats it 10 times.
       {\n}         Pushes \n.
           ~+       Adds it to the end of "*"*10.
             10;*   Repeats it 10 times.
                 >  Outputs.
\$\endgroup\$
  • \$\begingroup\$ 2 bytes can be saved with 1.4's single char string $. \$\endgroup\$ – CalculatorFeline Jun 22 '17 at 15:32
1
\$\begingroup\$

LibreLogo, 21 bytes

Code:

print(9*'*'+'*\n')*10

Result:

enter image description here

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

MATLAB / Octave, 23 bytes

Code:

m=[eye(10) ''];m(:)='*'

Try It Online!

Result:

**********
**********
**********
**********
**********
**********
**********
**********
**********
**********
\$\endgroup\$
  • \$\begingroup\$ You can replace '*' by 42 to save a byte. Or use [ones(10)+41 ''] \$\endgroup\$ – Luis Mendo Jan 8 '18 at 16:26
1
\$\begingroup\$

R 34 bytes

While not being elegant at all, it makes what expected in not that many bytes...

cat(rep("**********",10),sep="\n")

\$\endgroup\$
  • 2
    \$\begingroup\$ write(matrix('*',10,10),'',10,,'') is the same number of bytes with a slightly different approach. \$\endgroup\$ – Giuseppe Jun 7 '17 at 19:28
1
\$\begingroup\$

Triangular (non-competing) , 28 bytes

A\(A@1].p-p..pA@...](/*76-1<

Try it online!

Expanded version:

       A 
      \ ( 
     A @ 1 
    ] . p - 
   p . . p A 
  @ . . . ] ( 
 / * 7 6 - 1 < 

Commands executed, excluding directional commands:

A(1-A(1-67*@p]A@pp]
\$\endgroup\$
1
\$\begingroup\$

Java 8, 73 62 bytes

o is a String passed to a Consumer

o->System.out.print("**********".replace("*", "**********\n"))
\$\endgroup\$
  • \$\begingroup\$ o->System.out.print("**********".replace("*", "**********\n")) \$\endgroup\$ – Nevay Sep 28 '17 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\$

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\$
  • \$\begingroup\$ I believe lambda body is enough - 33 bytes \$\endgroup\$ – mazzy Jul 2 '18 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\$

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.