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

\$\endgroup\$
  • 2
    \$\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 '14 at 18:49
  • 14
    \$\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 '14 at 7:15
  • 10
    \$\begingroup\$ I should probably mention that the song is copyrighted and distributing these programs may cause a DMCA \$\endgroup\$ – ratchet freak Oct 14 '14 at 14:38
  • 12
    \$\begingroup\$ guys! Happy Birthday song is now in public domain!! \$\endgroup\$ – Optimizer Sep 23 '15 at 10:01
  • 4
    \$\begingroup\$ At least one of the answers prints a leading newline. Is that allowed? \$\endgroup\$ – Dennis Jun 19 '16 at 21:32

177 Answers 177

1
\$\begingroup\$

Pascal (83) in maXbox

  const H='Happy Birthday to You';
  Speak(H+H+DelString(H,'to You')+'Dear Pascal'+H);

See more: https://github.com/maxkleiner/maXbox3/releases

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

Mathematica (65)

Print["Happy Birthday "<>#]&/@{s="to you",s,"dear Mathematica",s}
\$\endgroup\$
  • \$\begingroup\$ Welcome to PPCG! As this is a code golf challenge, please add the byte count of your code next to the language name. \$\endgroup\$ – NinjaBearMonkey Oct 16 '14 at 3:57
1
\$\begingroup\$

VBScript (75)

h="Happy Birthday "
l=vbLF
t="to You"&l
x=h&t
WScript.Echo x&x&h&"Dear VBS"&l&x

69 bytes if replacing WScript.Echo with MsgBox would be allowed.

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

Rexx: 87

p='a'x;h='Happy Birthday';i='to you';ooray='dear Rexx'p;!=h i;say h i p   h i p   h ooray !

Output:

Happy Birthday to you 
 Happy Birthday to you 
 Happy Birthday dear Rexx
 Happy Birthday to you
\$\endgroup\$
1
\$\begingroup\$

C# (Full program) - 162

namespace O{class P{static void Main(string[] a){for(int i=0;i<4;)System.Console.Write("Happy Birthday {0}\n",++i==3?"Dear god why do I golf in C#?":"to you");}}}
\$\endgroup\$
  • 1
    \$\begingroup\$ "Dear god why do I golf in C#?" -- Try vba, it's worse. \$\endgroup\$ – RubberDuck Oct 16 '14 at 20:32
1
\$\begingroup\$

Falcon: 61

for i in[:4]:>"Happy Birthday "+(i==2?"Dear Falcon":"to You")
\$\endgroup\$
1
\$\begingroup\$

Javascript, 82

Yet another different js entry

a='Happy Birthday %s';b='to You\n';console.log(a+a+a+a,b,b,'Dear Javascript\n',b)
\$\endgroup\$
1
\$\begingroup\$

J Script(63)

alert((a=(x="\nHappy Birthday ")+"to You")+a+x+"Dear JScript"+a)

Why J Script and not Java Script? -3 bytes:)

\$\endgroup\$
  • \$\begingroup\$ I don't see why this has been downvoted... \$\endgroup\$ – Beta Decay Oct 14 '14 at 18:06
  • 1
    \$\begingroup\$ @BetaDecay coz JS is not the language name. \$\endgroup\$ – Optimizer Oct 16 '14 at 19:13
  • \$\begingroup\$ @Optimizer Ah, I see now... \$\endgroup\$ – Beta Decay Oct 16 '14 at 19:18
  • \$\begingroup\$ @Optimizer now it is the language name. \$\endgroup\$ – MegaTom Oct 16 '14 at 22:37
1
\$\begingroup\$

Clojure -- 79

(map #(println"Happy Birthday"%)(assoc(vec(repeat 4"to You"))2"Dear Clojure"))
\$\endgroup\$
1
\$\begingroup\$

PHP - 87 80 78 chars

<? $h="<br>Happy birthday ";$t="to you";$d="dear PHP";echo "$h$t$h$t$h$d$h$t";

I don't think that PHP is a good language for golfing, but I'm still practicing. And I love PHP :)

For the command line (strip off the short tags and replace the <br> with \n) 73 chars:

$h="\nHappy birthday ";$t="to you";$d="dear PHP";echo "$h$t$h$t$h$d$h$t";
\$\endgroup\$
  • 1
    \$\begingroup\$ I think you can skip the <? ?> because you can run it with -r on the command line. \$\endgroup\$ – chilemagic Oct 13 '14 at 20:28
  • 2
    \$\begingroup\$ Even on a webpage, you don't need the closing ?>, two more chars saved. \$\endgroup\$ – OlivierTheOlive Oct 17 '14 at 15:55
  • 1
    \$\begingroup\$ @OlivierTheOlive Thanks. My PHP is a little rusty, I've not used it for a long time. \$\endgroup\$ – g.carvalho97 Oct 17 '14 at 18:16
1
\$\begingroup\$

C# (83)

using System;

for(int i=0;i<4;i++)Console.WriteLine("Happy Birthday "+(i!=2?"to You":"Dear C#"));

//Happy Birthday To You
//Happy Birthday To You
//Happy Birthday Dear C#
//Happy Birthday To You

Equally short (83 Bytes of code) is:

int i=0;while(i<4)Console.WriteLine("Happy Birthday "+(i++!=2?"to You":"Dear C#"));
\$\endgroup\$
1
\$\begingroup\$

Clojure (104 76 characters)

(let[h "Happy Birthday" t "to You\n"](print h t h t h "Dear Clojure\n" h t))

which prints

Happy Birthday to You
 Happy Birthday to You
 Happy Birthday Dear Clojure
 Happy Birthday to You
\$\endgroup\$
1
\$\begingroup\$

Rant, 58 54 chars

[r:4]{Happy Birthday [alt:[nth:3;Dear Rant];to You]\N}

Online Version

How it Works:

  • [r:4] - Sets repetitions of next block to 4.
  • [alt: ... ; ...] - Executes alternate code if a primary pattern prints nothing.
    • [nth:3;Dear Rant] - Prints "Dear Rant" on the third iteration.
    • If the nth condition is not satisfied, the alt function prints to You.
\$\endgroup\$
1
\$\begingroup\$

Intersystems Cache M, (71)

S X="Happy Birthday "
S Y="to You"
S Z="Dear M"
W X,Y,!,X,Y,!,X,Z,!,X,Y

Sample run...

USER>S X="Happy Birthday "

USER>S Y="to You"

USER>S Z="Dear M"

USER>W X,Y,!,X,Y,!,X,Z,!,X,Y
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear M
Happy Birthday to You
\$\endgroup\$
1
\$\begingroup\$

Delphi - 124 122 115

Another Delphi XE2 try, this time without a loop.

{$APPTYPE CONSOLE}const l=#13#10;y='to you'+l;h='Happy birthday ';d='dear Delphi'+l;begin
Write(h+y+h+y+h+d+h+y)end.
\$\endgroup\$
  • \$\begingroup\$ Write is enough there as the "Ln" part you output literally. \$\endgroup\$ – manatwork Oct 17 '14 at 9:08
  • \$\begingroup\$ And again you're right :) \$\endgroup\$ – R-D Oct 17 '14 at 9:20
1
\$\begingroup\$

Befunge, 90

Try it out here or here

4v>$"uoY ot"     v
v>0" yadhtriB yppaH" >:#,_$
< |-2\+49\0:
 v>"egnufeB raeD">:#,_$1-:!#@_
\$\endgroup\$
  • \$\begingroup\$ If I recollect correctly \r is the Mac OS linebreak; \r\n is the Windows linebreak; and \n is the *nix linebreak. \$\endgroup\$ – Bob Jarvis Oct 22 '14 at 2:52
  • \$\begingroup\$ Oops, yes I meant Mac (prior to OS X anyway). Apparently any of these line breaks should work according to the Funge-98 specs. I just checked the interpreter at compileonline.com and it seems happy to accept \r so I'll go with that. \$\endgroup\$ – squeamish ossifrage Oct 22 '14 at 8:11
1
\$\begingroup\$

C++, 107

Nice:

#include <iostream>
int main(){[](){}();//<-Good Luck Charm
    for (auto b : {false, false, true, false})
        std::cout << "Happy Birthday " << (b ? "Dear C++" : "To You") << '\n';
}

Golfed:

#include <iostream>
int main(){for(int b:"\0\0?")std::cout<<"Happy Birthday "<<(b?"Dear C++":"To You")<<'\n';}
\$\endgroup\$
  • \$\begingroup\$ How should I compile this? gcc without any switch says: “nwp.cpp:2:22: error: range-based ‘for’ loops are not allowed in C++98 mode”. \$\endgroup\$ – manatwork Oct 22 '14 at 17:50
  • \$\begingroup\$ @manatwork -std=c++11 should do the trick. \$\endgroup\$ – nwp Oct 22 '14 at 18:04
  • \$\begingroup\$ Nope. :( pastebin.com/eZUpZXve BTW, its gcc 4.8.2 on Linux. As neither the ungolfed version compiles, I suppose on my side is broken something… \$\endgroup\$ – manatwork Oct 22 '14 at 18:19
  • \$\begingroup\$ @manatwork Looks like gcc does not find the standard library. I don't know how exactly to fix that. You can semi-run the code at ideone. \$\endgroup\$ – nwp Oct 22 '14 at 18:49
  • \$\begingroup\$ @manatwork Now I remember. Ignore the standard library comment. Instead of gcc use g++ to compile. \$\endgroup\$ – nwp Oct 22 '14 at 23:40
1
\$\begingroup\$

sh - 59

echo "${h=Happy Birthday }${y=to You}
$h$y
${h}Dear sh
$h$y

I thought this solution would be worth sharing too, even though there's a shorter one.

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

K, 45 bytes

No one has sung Happy Birthday for everyone's favorite in-memory columnar database language? There, there:

`0:4 21#(57#"Happy Birthday to You"),"Dear K"

Very similar to the APL approach by @Moris Zucca- repeat the string Happy Birthday to You slightly more than two times and then glue on the Dear K. Reshaping into a 4x21 matrix repeats the initial line. In K, sending text to stdout requires the 0: IO verb. Without it (for 3 fewer characters), we would print:

  4 21#(57#"Happy Birthday to You"),"Dear K"
("Happy Birthday to You"
 "Happy Birthday to You"
 "Happy Birthday Dear K"
 "Happy Birthday to You")

An alternate 47 character solution I considered was:

`0:("Happy Birthday ",("to You";"Dear K")2=)'!4

This is about as straightforward as it gets- map a function over the sequence 0 1 2 3 given by !4, select the second half of the verse based on whether this index is equal to 2, glue on the start of the verse and then print the whole list out.

\$\endgroup\$
  • 2
    \$\begingroup\$ There's no in-memory columnar database language I like better. \$\endgroup\$ – Dennis Jun 28 '15 at 20:56
1
\$\begingroup\$

JMP/JSL, 67

Like the Mathematica example by David Carraher, this will 'sing' (...read) the Happy Birthday message to you out loud.

speak(x="\!nHappy Birthday";y=x||" to You";y||y||x||" Dear JSL"||y)

Output:

Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear JSL
Happy Birthday to You

For 7 less bytes you will have the Happy Birthday message printed for you in the log

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

SAS, 66 bytes

data;a='Happy Birthday';b=a||' to You';put b/b/a 'Dear SAS'/b;run;
\$\endgroup\$
1
\$\begingroup\$

Lua - 72 bytes

Straightforward, but none of the other Lua programs work this way.

a,b,c='Happy Birthday ','to You\n','Dear Lua\n'io.write(a,b,a,b,a,c,a,b)
\$\endgroup\$
1
\$\begingroup\$

><>, 157

v
\0&
v                                   <
\&:&2(?v
       >"Happy Birthday to you"a&1+&^
\"Happy Birthday dear ><>"a\
v  a"uoy ot yadhtriB yppaH"/
r
>l?!;o

Big, but I wanted it to be different from the Befunge answer.

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

G*, 76 bytes

G* is a golfing language I made but it seems it failed miserably :P (Note that this isn't even close to my favorite language but I wanted to give it a try at code golf.)

l 2 p Happy birthday to you;p Happy birthday dear G*;p Happy birthday to you

Here's another (longer) one:

1=Happy birthday to you;@ 1;@ 1;p Happy birthday dear G*;p Happy birthday to you
\$\endgroup\$
1
\$\begingroup\$

C#, 91 bytes

var s="Happy Birthday to You\n";System.Console.Write(s+s+s.Substring(0, 15)+"Dear C#\n"+s);
\$\endgroup\$
1
\$\begingroup\$

beeswax, 66 chars

Non-competing answer; 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\$

Milky Way 1.6.5, 56 bytes

I"Happy Birthday ":"Dear Milky Way"+;"to You"+::>>J§{!}

Explanation

I                                                        ` empty the stack
 "Happy Birthday "                                       ` push the string
                  :"Dear Milky Way"+                     ` make the 3rd line
                                    ;"to You"+::         ` make the 1st, 2nd and 4th lines
                                                >>       ` put the lines in order
                                                  J§{!}  ` print each line

Usage

$ ./mw <path-to-code>
\$\endgroup\$
1
\$\begingroup\$

Golisp, 102 100 bytes (Non-competing)

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

Non-competing since the language was posted after the creation of this challenge.

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

Jelly, 31 bytes (non-competing)

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

Try it online!

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

Pushy, 44 bytes

Pushy doesn't actually have a string type. However, it has the command ", which interprets the current stack as a character list and prints it.

`Happy Birthday `V`to You`""F`dear Pushy`"F"

It's quite simple:

`Happy Birthday `      % Push chars
V                      % Copy into stack 2
`to You`               % Push chars
""                     % Print stack 1 twice ("Happy Birthday to You")
F                      % Go to stack 2
`dear Pushy`           % Push chars
"                      % Print stack ("Happy Birthday dear Pushy")
F"                     % Print other stack again to complete the song.

Basically, it uses the two stacks to construct the two needed strings, and prints them as it goes along. Here's the output:

$ pushy happybday.pshy

Happy Birthday to You
Happy Birthday to You
Happy Birthday dear Pushy
Happy Birthday to You
\$\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.