130
\$\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\$
16
  • 4
    \$\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
  • 16
    \$\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
  • 13
    \$\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

208 Answers 208

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

Quipu, 123 115 bytes

'H'D't[][][]
'a'e'o/\/\/\
'p'a' 2&1&2&
'p'r'Y[][][]
'y' 'o/\/\/\
' 'Q'u
'B'u\n
'i'i/\
'r'p
't'u
'h\n
'd
'a
'y
' 
/\

Try it online!

Explanation

Each pair of columns in a Quipu program is a "thread." Execution proceeds from the top of each thread to the bottom, left to right from one thread to the next. Each thread also stores a value, which can be referenced by other threads.

  • Thread 0 outputs Happy Birthday with a trailing space and sets its value to that string.
  • Thread 1 sets its value to Dear Quipu with a trailing newline.
  • Thread 2 outputs to You with a trailing newline and sets its value to that string.
  • Thread 3 outputs the value of thread 0, then outputs the value of thread 2.
  • Thread 4 outputs the value of thread 0, then outputs the value of thread 1.
  • Thread 5 outputs the value of thread 0, then outputs the value of thread 2.
\$\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, 2022 at 3:03
1
\$\begingroup\$

Jelly, 31 bytes

“уẉ%VİỊ½żƒ»µ,;“©ʋ⁶ẓsŀKɗʠØ»,⁸¤Y

Try it online!

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

Golisp, 102 100 bytes

EDIT: Removed 2 bytes by changing the last writeln to write

write@*["Happy Birthday to You\n"2]writeln@"Happy Birthday Dear Golisp"write@"Happy Birthday to You"

Ungolfed & commented version:

write @ *["Happy Birthday to You\n" 2] # Call write with "Happy Birthday to You\n" repeated 2 times (f@x == f[x]) #
writeln @ "Happy Birthday Dear Golisp" # Write "Happy Birthday Dear Golisp" to stdout #
write @ "Happy Birthday to You" # Write "Happy Birthday to You" to stdout #
\$\endgroup\$
1
\$\begingroup\$

beeswax, 66 chars

I created beeswax in December 2015. This solution is just for fun.

>`y Dear beeswax`5Np
pb"M`adhtriB yppaH`<3~4_
>`y To You`N~L;~   d

You can clone the beeswax interpreter, language specifications and instructions from my GitHub repository.

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

Zsh, 51 bytes

set Happy\ Birthday to You
<<.
$@
$@
$1 Dear zsh
$@

Attempt This Online!

Beats vanilla shell by one byte, despite having a one-byte longer name!

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

Uiua, 45 bytes SBCS

⍥(&p⊂"Happy Birthday ")4.,"Dear Uiua""to You"

Try it online!

Explanation

⍥(&p⊂"Happy Birthday ")4.,"Dear Uiua""to You"
                                     "to You" # push string "to You" to stack
                          "Dead Uiua"         # push string "Dear Uiua" to stack
                         ,                    # duplicate 2nd-to-top stack element
                        .                     # duplicate top stack element
⍥(                    )4                      # repeat this function 4 times:
     "Happy Birthday "                        # push string "Happy Birthday " to stack
    ⊂                                         # join end-to-end
  &p                                          # print to stdout with newline
\$\endgroup\$
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\$

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
1
3 4 5
6
7

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.