127
\$\begingroup\$

Your favourite programming language has just had a birthday. Be nice and sing it the Happy Birthday song.

Of course you should accomplish this by writing a program in that language. The program takes no input, and writes the following text to the standard output or an arbitrary file:

Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear [your favourite programming language]
Happy Birthday to You

You should substitute the bracketed part (and omit the brackets).

This is a code golf — shortest code wins.

UPDATE

I'm glad that the question aroused great interest. Let me add some extra info about scoring. As stated originally, this question is a code golf, so the shortest code is going to win. The winner will be picked at the end of this week (19th October).

However, I'm also rewarding other witty submissions with up-votes (and I encourage everybody to do so as well). Therefore although this is a code-golf contest, not-so-short answers are also welcome.

Results

Congratulations to Optimizer, the winner of this contest with his 42 byte long, CJam submission.

Leaderboard

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

/* Configuration */

var QUESTION_ID = 39752; // 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\$
15
  • 3
    \$\begingroup\$ Can we count the name of the programming language as one byte because it would only seem fair to people coding in a long-winded language such as JavaScript vs. someone doing it in C. We are really looking for most creative logic right? \$\endgroup\$
    – MonkeyZeus
    Oct 13, 2014 at 18:49
  • 15
    \$\begingroup\$ Why all the upvotes for this despite all the downvotes for the Bonbon song? This is just as boring a challenge. \$\endgroup\$
    – xnor
    Oct 14, 2014 at 7:15
  • 10
    \$\begingroup\$ I should probably mention that the song is copyrighted and distributing these programs may cause a DMCA \$\endgroup\$ Oct 14, 2014 at 14:38
  • 12
    \$\begingroup\$ guys! Happy Birthday song is now in public domain!! \$\endgroup\$
    – Optimizer
    Sep 23, 2015 at 10:01
  • 5
    \$\begingroup\$ At least one of the answers prints a leading newline. Is that allowed? \$\endgroup\$
    – Dennis
    Jun 19, 2016 at 21:32

200 Answers 200

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

Barrel, 112 bytes

All this jumping eats up the bytes, I'm afraid.

→1→2→3→1→2→3→1→4→3→1→2'!'!¤'Happy birthday'↑¤' to you'↑¤','¶↑¤', dear Barrel'↑

Basically, it defines a bunch of functions whose purpose is to implicitly print strings and calls them in the appropriate order before exiting. The second ! (the first one is a string for ending the song) exits the program so that barrel doesn't continue executing the program. If the ! were not there, barrel would implicitly print 'Happy birthday' and then exit, seeing that the instruction doesn't have any more location pointers to pop, and therefore "pops" to the program that ran barrel.

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

tinylisp, 120 114 bytes

(load library
(d x(q(Happy Birthday 
(d y(join(concat x(q(to You)))spc
y 
y
(join(concat x(q(Dear tinylisp)))spc
y

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Refactoring a bit gives 101 bytes. \$\endgroup\$
    – DLosc
    Feb 27 at 3:03
0
\$\begingroup\$

JavaScript, 73

Run this in the console

for(h="",i=4;i;)h+='\nHappy Birthday '+(--i-1?'to You':'Dear JavaScript')
\$\endgroup\$
1
  • \$\begingroup\$ I think it would be easier to just declare the happy birthday 4 times than to loop it. \$\endgroup\$
    – Goose
    Dec 30, 2015 at 14:31
0
\$\begingroup\$

C++, 136

#include<iostream>
int main(){static int i=0;std::cout<<"Happy Birthday "<<(i==2?"Dear C++":"to You")<<std::endl;return ++i<4?main():0;}
\$\endgroup\$
0
\$\begingroup\$

B - 109

(caveat: arguably not valid B, difficult to say - works with this)

main()v(4,"Happy Birthday to You   HDear B");v(i,s){s[5]=i&5?'u':134744072;s[6]=&-247;i?puts(s)&v(i-1,s):0;}
\$\endgroup\$
0
\$\begingroup\$

EcmaScript 6 - 64 о_О

[1,1,0,1].map(i=>"Happy Birthday "+(i?"to you":"ES6")).join("\n")
\$\endgroup\$
4
  • \$\begingroup\$ Unfortunately, this does not completely follow the spec: you need to have "dear" before the language name, you have to write out "JavaScript", as per the other JS answers, and you have to provide a way of outputting the result so it can be a stand-alone program (console.log in this case.) \$\endgroup\$ Oct 15, 2014 at 23:07
  • \$\begingroup\$ Than that's the fix (83): alert([1,1,0,1].map(i=>"Happy Birthday "+(i?"to you":"Dear EcmaScript")).join("\n")) \$\endgroup\$
    – Qwertiy
    Oct 15, 2014 at 23:28
  • \$\begingroup\$ By the way, solution codegolf.stackexchange.com/a/39770/32091 does not do any output and uses latest evaluation output in console like mine. \$\endgroup\$
    – Qwertiy
    Oct 15, 2014 at 23:31
  • \$\begingroup\$ True, but that one should also have its own method of output. \$\endgroup\$ Oct 15, 2014 at 23:45
0
\$\begingroup\$

PowerShell 92 (fixed)

$a="happy birthday to you";$a;$a;write-host -no  $a.TrimEnd('to you');"y dear powershell" ;$a

this one is longer than the other PS solution but is unique and actually out puts the correct string

OLD -Powershell- - 62

$a="happy birthday to you";$a;$a;write "$a dear powershell";$a
\$\endgroup\$
1
  • 1
    \$\begingroup\$ It looks to me like that will print the third line as "happy birthday to you dear powershell", which is not correct. \$\endgroup\$ Oct 17, 2014 at 22:45
0
\$\begingroup\$

><> 76 Bytes

Since it wasn't here already...

0>"Happy Birthda"{1+:}3-?vv
v^?=1l<;?=5}:{a"uoY oT y"<>"y Dear ><>"a
>{{o} ^
\$\endgroup\$
0
\$\begingroup\$

JS 70 Bytes

alert([c=(a="Happy Birthday")+" to You",c,a+" Dear JS",c].join("\n"));
\$\endgroup\$
0
\$\begingroup\$

Swift, 68 bytes

(1...4).map{print("Happy Birthday "+($0==3 ?"dear Swift":"to You"))}

also differently:

print({$0+$1+$0+$1+$0+"dear Swift"+$0+$1}("\nHappy Birthday ","to You"))

with 71 bytes

\$\endgroup\$
1
  • \$\begingroup\$ Another 70-byte alternative: for i in 1...4{print("Happy Birthday "+(i==3 ?"Dear Swift":"to You"))} \$\endgroup\$
    – Mr. Xcoder
    Aug 3, 2017 at 6:22
0
\$\begingroup\$

HPPPL (HP Prime Programming Language), 88

n:=char(10);a:="Happy Birthday ";b:="To You";c:="Dear HPPPL";d:=a+b+n;print(d+d+a+c+n+d)

Result:

Happy Birthday To You HPPPL

HPPPL is the programming language for the HP Prime color graphing calculator/CAS. An emulator is available at the HP website.

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

Stringy, 78 bytes

(Happy birthday );a to you;p;p;^ to you!dear Stringy;p;^ dear Stringy!to you;p
\$\endgroup\$
0
\$\begingroup\$

Mouse-2002, 70 bytes

Mouse isn't very skilled at . :(

$H"Happy Birthday "@
$Y"to you!"@
#H;#Y;#H;#Y;#H;"Dear Mouse!"#H;#Y;$

The exclamation points print newlines, not themselves, and the $H and $Y are functions, not variables, because poor Mouse can't comprehend strings.

MUSYS, MUsic-SYStem, Mouse's predecessor, 93 bytes

"Happy Birthday to you!Happy Birthday to you!Happy birthday dear MUSYS!Happy Birthday to you"
\$\endgroup\$
0
\$\begingroup\$

Staq, 67 66 chars

{h"Happy Birthday "}{T"To You"}{D"Dear Staq"}{N&iiqi,;}hTNhTNhDNhT

Result:

Executing D:\codegolf\Happy Birthday Staq.staq

Happy Birthday To You
Happy Birthday To You
Happy Birthday Dear Staq
Happy Birthday To You

Execution complete.
>
\$\endgroup\$
0
\$\begingroup\$

JavaScript, 70 Bytes

a="Happy Birthday ";b=a+"to You\n";c=a+b;d=c+c+a+"Dear JavaScript\n"+c

Best answer JavaScript answer so far!

a="Happy Birthday ";
b=a+"to You\n";
c=a+b;                         // Create string "Happy Birthday to You\n"
d=c+c+a+"Dear JavaScript\n"+c  // Create song

Implementation:

a="Happy Birthday ";b=a+"to You\n";c=a+b;d=c+c+a+"Dear JavaScript\n"+c
alert(d);
console.log(d);
\$\endgroup\$
1
  • 1
    \$\begingroup\$ You're not allowed to leave output in a variable. You have to write a full program which prints it or a function which returns it. \$\endgroup\$
    – FlipTack
    Jan 11, 2017 at 17:04
0
\$\begingroup\$

Edited Processing.js 80 bytes

var a="\nhappy birthday to you";print(a+a+"\nHappy birthday processing.js"+a);

Processing JS is a language that is fun but I am lazy and use an edited version that also is ungolfable. Try it online Println was used so you don't have to open the console :P

Khanacademy version

\$\endgroup\$
1
  • \$\begingroup\$ Ah, it's the Khan Academy version. Perhaps you should mention that somewhere (also, I believe the source code is here) \$\endgroup\$ Mar 6, 2017 at 23:33
0
\$\begingroup\$

ZPL (Zebra Programming Language), 112 bytes

This is a just for fun answer.

^XA^FB137,4^FDHappy Birthday to You\&Happy Birthday to You\&Happy Birthday Dear ZPL\&Happy Birthday to You^FS^XZ

Try It Online

Explanation:

^XA                                # Start Format
    ^FB                            # Field Block
        137,                       # Width of text block line (in dots)
        4                          # Maximum number of lines in text block
    ^FD                            # Field Data
        Happy Birthday to You      # Hardcoded String
        \&                         # New Line (as allowed by the ^FB command)
        Happy Birthday to You      # Hardcoded String
        \&                         # New Line (as allowed by the ^FB command)
        Happy Birthday Dear ZPL    # Hardcoded String
        \&                         # New Line (as allowed by the ^FB command)
        Happy Birthday to You      # Hardcoded String
    ^FS                            # Field Separator
^XZ                                # End Format

Note: The Zebra Programming Language is a printer control language that is not Turing complete.

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

Sprects, 49 bytes

.12121Dear Sprects\n12.1Happy Birthday .2to You\n

Try it here

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

C, 74 bytes

i=5;main(){while(--i)printf("Happy Birthday %s\n",i-2?"to You":"Dear C");}

Detailed

void main()
{
    int i = 5;

    while (--i > 0)
    {
        printf("Happy Birthday %s\n",
        i!=2 ? "to You" : "Dear C" );
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ You're supposed to write a program, not a function. Change f() to main() and it should be OK (but one character longer than V-X's answer). \$\endgroup\$
    – r3mainer
    Apr 19, 2017 at 11:44
  • \$\begingroup\$ @squeamishossifrage fixed. \$\endgroup\$
    – Khaled.K
    Apr 19, 2017 at 12:48
0
\$\begingroup\$

C++, 92 bytes

#include<iostream> 
int main(int c){for(c=4;c--;)std::cout<<"Happy Birthday "<<(c-1?"To You":"Dear C++")<<"\n";}

Try it online

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

Batch, 72 bytes

@SET h=@ECHO Happy Birthday 
%h%to You
%h%to You
%h%Dear Batch
%h%to You
\$\endgroup\$
0
\$\begingroup\$

Fourier, 52 bytes

|`Happy Birthday `|A|`to You
`|BABABA`Dear Fourier
`AB

Try it online!

Nothing particularly special except that it demonstrates that you can have newlines in string prints.

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

Braingolf, 69 bytes

"Happy Birthday to You
"VR{.M}v&,6>[$_]"dear Braingolf
"R!&@!&@v&@c&@

Try it online!

Explanation

"Happy Birthday to You
"

Pushes "Happy Birthday to You\n" to the stack

VR{.M}

Creates a 2nd stack and duplicates the contents of the first stack to it, in reverse order

v&,

Switches to the 2nd stack and flips the entire stack

6>[$_]

Drops the last 7 items from the 2nd stack

"dear Braingolf
"

Pushes "dear Braingolf\n" to the 2nd stack

R!&@!&@

Prints the contents of the first stack twice

v&@

Prints the contents of the 2nd stack

c&@

Prints the contents of the first stack

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

Cubically, 813 bytes

I guess this belongs here now too! Loops haven't been added to the language yet, so this is the best I can manage.

+53@6:5+2/1+55@6:4/1+552@66+1@6:5/1+3@6/1+52@6:5+1/1+551@6+1@6:5+3/1+552@6:5/1+551@6:1/1+551@6:5+2/1+55@6:4/1+553@6:5/1+3@6:5+3/1+552@6:3/1+552@6:5/1+3@6:5+3/1+54@6:3/1+552@6:5+53@6:1/1+1@6:5+3@6:5+2/1+55@6:4/1+552@66+1@6:5/1+3@6/1+52@6:5+1/1+551@6+1@6:5+3/1+552@6:5/1+551@6:1/1+551@6:5+2/1+55@6:4/1+553@6:5/1+3@6:5+3/1+552@6:3/1+552@6:5/1+3@6:5+3/1+54@6:3/1+552@6:5+53@6:1/1+1@6:5+3@6:5+2/1+55@6:4/1+552@66+1@6:5/1+3@6/1+52@6:5+1/1+551@6+1@6:5+3/1+552@6:5/1+551@6:1/1+551@6:5+2/1+55@6:4/1+553@6:5/1+3@6+4@6:2/1+551@6:5+2/1+55@6:5+1/1+552@6:5/1+3@6:4/1+52@6:5+53@6:5+3/1+55@6:5+1/1+551@6:5+51@6:5+2/1+55@6:5+52@66:4/1+553@6:1/1+1@6:5+3@6:5+2/1+55@6:4/1+552@66+1@6:5/1+3@6/1+52@6:5+1/1+551@6+1@6:5+3/1+552@6:5/1+551@6:1/1+551@6:5+2/1+55@6:4/1+553@6:5/1+3@6:5+3/1+552@6:3/1+552@6:5/1+3@6:5+3/1+54@6:3/1+552@6:5+53@6

Try It Online!

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

Pyth, 49 bytes

Since the other Pyth answer is quite outdated and non-competing has been removed per meta consensus, here is a newer answer:

K"Happy Birthday "J+K"to You\n"+*J2+K"Dear Pyth"J

Try it online!

Pyth, 50 bytes

K+"Happy Birthday to You"b+++KK++<K15"Dear Pyth"bK

Try it online!

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

Golfscript, 56 bytes

Thanks, professorfish!

"Happy Birthday ":h"to You\n"+:s s h"dear Golfscript\n"s

Output:

Happy Birthday to You
Happy Birthday to You
Happy Birthday dear Golfscript
Happy Birthday to You
\$\endgroup\$
2
  • 1
    \$\begingroup\$ You don't need to concatenate them; they'll all be printed anyway \$\endgroup\$
    – user16402
    Oct 13, 2014 at 19:17
  • 1
    \$\begingroup\$ and you seem to do "Happy Birthday ":h;h - why do you need to pop the value and then push it again? "Happy Birthday ":h will have the same effect AFAIK. same for :s;s \$\endgroup\$
    – user16402
    Oct 13, 2014 at 19:32
0
\$\begingroup\$

SOGL V0.12, 29 bytes

‰║M─╬RS‘ūΖTtŗQQ"8Ν¼ģ^ļ&NZøe‘O

Try it Here!

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

Canvas, 43 bytes

3[to You¶}Dear Canvas¶)«∑Happy Birthday 4*×

Try it online!

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

3[to You¶}                                      Push the string "to You\n" to the stack three times
            Dear Canvas¶                          Push the string "Dear Canvas\n" to the stack
                        )                        Compress the stack to an array
                          «                       Shift the array once to the left
                           ∑                      Join all strings in the array to one string
                            Happy Birthday 4*   Push the string "Happy Birthday " repeated four times vertically to the stack
                                               ×  Concatenate the second object in the stack to the first object in the stack
                                                  Print the object at the top of the stack (implicit)
\$\endgroup\$
1
  • \$\begingroup\$ 40 bytes \$\endgroup\$
    – dzaima
    Jul 30, 2018 at 16:43
0
\$\begingroup\$

Add++, 55 bytes

L,"Happy Birthday "dddB]"To You"ddV"Dear Add++"GBCBcB+n

Try it online!

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

Python 2, 82 Bytes

h="Happy Birthday";y='to You';d="Dear Python";print h,y,'\n',h,y,'\n',h,d,'\n',h,y

Still getting my head around slicing...

\$\endgroup\$
1
3 4 5
6
7

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.