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

181 Answers 181

2
\$\begingroup\$

Visual FoxPro - 77

Not really my favorite language, but makes golfing somewhat interesting. Here is my greetings to it:

a="Happy Birthday "
b="to You"
?a+b
?a+b
?a+"Dear "+_screen.Caption
?a+b

This abuses the fact that _screen.Caption (the main window title) initially contains Microsoft Visual FoxPro, handy here. If I'm allowed to call the language by its "friendly name", I can just replace that for "Fox" and save 10 characters.

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

Lua, 96 bytes

I was trying to be cool about this and make use of all the best Lua tricks I know but the guy who wrote a simple for loop with an if i==3 beat me by 20+ bytes >.<

Still, I think this is pretty cool in terms of Golfing:

a='Happy Birthday to you\ndear Lua\n'print(a:sub(0,-10):rep(3):sub(0,-8)..a:rep(2):sub(23,-10))

I was looking at recursively building the string a from a number using string.char() but I couldn't compress it anywhere near the length of a='Happy Birthday to you\ndear Lua\n'

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

PowerShell 62

0..3|%{"Happy Birthday "+("to You","Dear PowerShell")[$_-eq2]}
\$\endgroup\$
2
\$\begingroup\$

Delphi, 154 146 139

uses{$APPTYPE CONSOLE}SysUtils,StrUtils;var i:byte;begin
for i:=0to 3do WriteLn('Happy birthday '+IfThen(i=2,'dear Delphi','to you'))end.
\$\endgroup\$
  • \$\begingroup\$ Which Delphi version is this? I never met one with such Output parameter. And better choose another integer type, with a shorter name. For example Byte. \$\endgroup\$ – manatwork Oct 16 '14 at 15:17
  • \$\begingroup\$ An insignificant bug: the 'to you' and 'dear Delphi' should be reversed. \$\endgroup\$ – manatwork Oct 16 '14 at 15:27
  • 1
    \$\begingroup\$ Some minor shortening which work in FreePascal: move the directive between uses and the first unit name to be able to remove the space (1 char); remove unnecessary newline characters (3 chars); remove the ; before end (1 char); remove the space characters between numeric literals and the following keywords (2 chars). See whether any of those works in your Delphi too. \$\endgroup\$ – manatwork Oct 16 '14 at 15:34
  • \$\begingroup\$ @manatwork, thank you for your suggestions, it's Delphi XE2. \$\endgroup\$ – R-D Oct 17 '14 at 7:19
  • 1
    \$\begingroup\$ There's no need for Output,. \$\endgroup\$ – TLama Oct 20 '14 at 23:08
2
\$\begingroup\$

TeX - 70

\def\h{Happy Birthday }\def\y{to You}\h\y

\h\y

\h Dear TeX

\h\y\bye

Note that the line breaks are intentional and actually required.

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

LaTeX, 182

This isn't intended to be a serious competitive entry, but just to do something fun. Therefore, disregard the char count. I don't need any golfing tips for this, because it is intentionally ungolfed.

\documentclass{article}
\newcommand{\h}[1]{Happy Birthday to {#1}}
\newcommand{\g}{\h{You}}
\begin{document}
\begin{verse}
\g \\
\g \\
\h{\LaTeX} \\
\g \\
\end{verse}
\end{document}
\$\endgroup\$
2
\$\begingroup\$

C (74 characters)

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

Python 2 (72 characters)

for k in range(4):print"Happy birthday",k==2 and"Dear Python"or"to You"

Python 3 would need parentheses around the printed string, so two character more, so 74.

PHP (66 characters)

for($i=4;$i;)echo"Happy birthday ".($i---2?"to You\n":"Dear PHP\n");
\$\endgroup\$
  • \$\begingroup\$ In C, i---2 saves a character. Also {} after for isn't needed. \$\endgroup\$ – ugoren Oct 20 '14 at 9:32
2
\$\begingroup\$

Cardinal, 113 107 88 82 characters

I fixed the errors in the original interpreter, so the operators work as specified. This enabled me to shrink down the code by another 19 25 characters:

4th version:

v8888#;++"lanidraC raeD " <
8%+++>>"Happy Birthday"-jv^
>8888@^        ;"uoY oT "<

Output, using my interpreter fixes:

Executing program..

Happy Birthday To You
Happy Birthday To You
Happy Birthday Dear Cardinal
Happy Birthday To You

Execution complete.
>

Second version, saving 6 chars: I changed the o’s and O’s to v^<> because the available interpreter has some errors with the handling of the rotation operators o and O, j and J and some other errors. One has to use the wrong rotation operators to make it work, but I prefer using a version that works according to specification. The length of the code stays the same. This version works properly with the interpreter provided at the link given at the bottom of my comment: (107 characters)

%->"Happy Birthd"v
x ^D+;"uoY oT ya"<
   >"Happy Birthday D"v
v"ppaH";"lanidraC rae"<
>"y Birthday To You"@

Output, using the "original" faulty interpreter:

Executing program..

Happy Birthday To You
Happy Birthday To You
Happy Birthday Dear Cardinal
Happy Birthday To You

Execution complete.
>

You can test the program if you save it as txt file and drag it onto the interpreter exe for Windows.

http://esolangs.org/wiki/Cardinal

Here is the interpreter: http://floatation.webs.com/Cardinal.zip

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

CMD - 84 Bytes

cmd/v/c"set 1=to You&set 2=Dear CMD&for %a in (1,1,2,1)do @echo Happy Birthday !%a!"
\$\endgroup\$
2
\$\begingroup\$

jq: 54 52 characters

(49 characters code + 3 characters command line option.)

0,0,1,0|"Happy Birthday "+["to You","Dear jq"][.]

Sample run:

bash-4.3$ jq -n -r '0,0,1,0|"Happy Birthday "+["to You","Dear jq"][.]'
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear jq
Happy Birthday to You

On-line test (Passing -r through URL is not supported – check Raw Output yourself.)

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

Vim, 90 45 bytes

i
Happy Birthday to you
[ESC]
yypppPWWd$adear Vim

90 chars version:

i
Happy Birthday to you dear Vim
[ESC]
:%s/\(.* .* \)(.. .*\) \(.* .*\)/\1\2\r\1\2\r\1\3\r\1\2

I'm certain there is a quicker way but this is what I just came up with :)

Puts out

Happy Birthday to you
Happy Birthday to you
Happy Birthday dear Vim
Happy Birthday to you
\$\endgroup\$
2
\$\begingroup\$

Crayon, 48 46 bytes (non-competing)

"Happy Birthday to You
"4*q2Y15X"Dear¤Crayon"q

Try it online! Non-competing because Crayon is much newer than this challenge.

Explanation

Crayon's output is built on a 2-dimensional "canvas", which makes it ideal for ASCII art challenges. Text is drawn with a "crayon", which is a sort of cursor that moves around the canvas. Here's how this program works:

"Happy Birthday to You
"                        Push this string to the stack.
 4*                      Repeat the string 4 times.
   q                     Draw the string at the crayon (at (0,0) by default).
    2Y15X                Move the crayon to the "to" in the third line.
         "Dear¤Crayon"q  Push this string and draw it at the crayon.
                         Implicit: Output canvas

The ¤ represents byte \x1F; Crayon does not distinguish between the two. By default, a space does not overwrite the character underneath, so ¤ exists as a sort of "overwriting space".

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

Vim, 40 36 bytes

4OHappy Birthday to You<esc>k2bCDear Vim

4 bytes saved thanks to @DJMcMayhem by using O instead of i...\n and k2bC instead of kd2bC

This uses a completely different approach to the previous Vim answer and hence I posted it as a separate answer. Outputs a trailing newline, but can be removed at the cost of one extra byte by using Jk instead of k.

Since V is reverse compatible with V, you can Try it online!

Take that CJam!

\$\endgroup\$
  • \$\begingroup\$ This is the shortest competing solution, as far as I can tell. \$\endgroup\$ – primo Mar 12 '18 at 14:15
2
\$\begingroup\$

IDL (Interactive Data Language) – 47

Late entry (my first time here), but this looks like 3rd place after CJam and J!

Using IDL's Implied Print feature (since version 8.3), typing this at the command prompt:

'Happy Birthday '+[(u='to You'),u,'Dear IDL',u]

… will print the desired result to the console:

IDL> 'Happy Birthday '+[(u='to You'),u,'Dear IDL',u]
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear IDL
Happy Birthday to You

It works by creating (and implicitly printing) a four-element array, with 'Happy Birthday ' appended to each of the four desired endings, made more concise/cryptic by assigning a variable u the value 'to You' in the first instance, and reusing it twice later!

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

Brachylog, 52 bytes

"Happy Birthday to You"k₆,"Dear Brachylog"g,?,?↺,?ẉᵐ

Try it online!

-1 thanks to Fatalize.

\$\endgroup\$
  • \$\begingroup\$ You can do 52 bytes by replacing Z with ? and removing the first Z that unifies with the string. \$\endgroup\$ – Fatalize Aug 2 '17 at 14:29
  • \$\begingroup\$ @Fatalize Oh right the string unifies with ?. \$\endgroup\$ – Erik the Outgolfer Aug 2 '17 at 17:47
2
\$\begingroup\$

Unary, 2.715E2345 bytes

A string of zeroes with a length of 271544303753521374345784023617462447448766796991634195623271771343788064494033932467809637777433783261069835136468316635541569006957484008922172956907493623220358279584621153727394685316177875072717611870164011317998138133192412590581118711745080372132354520144049887264020357596958970682508119612069072184789433001139110110044141818883356102010044766108874743534376722033752443907309500308124800880956659496453762860029730933467190897780852535562502370486347488149358821225254933647032473098015846900520720300239993510123716590092279978630211422199772105659685365124211730629429856761187973769267382198582572968422104301126603859498501185917608874757741610893482041977003001381483481251619529628722427408338582339527157335234122942381275923949390605570564504822082772136057434464671123507495503027121245882553216512907124492451345153924551954324886539679044057026572440371334484657248811556813671966134409423451672608237773610848374597757496223902091127753821502096156978576733667670645804932685651998882744625271805968408192138003017383834286411465787825657955474390895833048578431020834761999769245058175985114327604667460675218915598066545717356728963407131382225834981689192053204022543453105950548359569307644293248265610511261123308873236361708287888512920927207140733264710153499926338085521461180344119631268486929440101264937117199882754841067458895376078972797675755102211602033168204711199744629239139853151734765325982327999734282103666604630769384354544377780974557091020653916122475973344426958326542939871981389127350485205414017350058299535652163085166091203298279054857159449355550278205308529995426116266317622349698148579685752377806473014344962213328458584663395562552762790574493037199848761427965675120816268240569132735553272845975553355819867289784427736974879735152861965319683108949477353673024614920892135291073944764379263817004591234093204928220689300382675729272978243082941205866127086587758323575891345926945843642967213885479373059430434109978135813970824413339027287055293233734939242557385167820253321966087885920624040254325137166864535273452145628538868866835395916784107310616214635120965693431403440614294637759561044455445706647271846433473546725069631283599186867863795122114084809385455487348989575611753135663244291563170055464760084089978670081473032469603515349502029160880861825869931824089974215244

\$\endgroup\$
  • 1
    \$\begingroup\$ I thought this was a good score till I saw the "E" \$\endgroup\$ – NoOneIsHere Sep 23 '17 at 20:45
2
\$\begingroup\$

Stax, 30 bytes

ï■σ∞úφ♂mê▌δ↓T┌C▒7w↓√éº┌│╦╫C∩█ï

Run and debug online!

Explanation

Uses ASCII representation to explain:

`=%5#`GG`!0 ZrO"`GdG}`I#aq5J^oD}(`pQ

                    }                   Define a subroutine after the bracket
                     `I#aq5J^oD}(`      Push "Happy Birthday "
                                  p     Pop the string and print without newline
                                   Q    Peek the stack and print with newline

`=%5#`                                  Push "to You"
      GG                                Call the subroutine twice
        `!0 ZrO"`                       Push "Dear Stax"
                 G                      Call the subroutine
                  d                     Pop and discard "Dear Stax"
                   G                    Call the subroutine again
\$\endgroup\$
  • \$\begingroup\$ I like your favorite language. Here's 28 for the lead. \$\endgroup\$ – recursive Mar 16 '18 at 21:12
2
\$\begingroup\$

R, 56 bytes

cat("",u<-"to You
",u,"Dear R
",u,sep="Happy Birthday ")

Try it online!

The old sep= trick.

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

Japt, 41 38 bytes

`Happy Bir»y  Y
`
³h58Ui` De Japt

Test it

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

PowerShell 79

$a = 'Happy Birthday '
$b = 'to You'
"$a$b`n$a$b`n$($a)Dear PowerShell`n$a$b"
\$\endgroup\$
1
\$\begingroup\$

Clojure : 87 chars

Golfed:

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

Ungolfed:

(let [ h "Happy Birthday "
       s (str h "to You\n")
       t (str h "Dear Clojure\n") ]
(print (str s s t s)))
\$\endgroup\$
  • 1
    \$\begingroup\$ Or 81 with the similar (let[h"Happy Birthday "s"to You\n"t"Dear Clojure\n"](map #(print h %)[s s t s])) \$\endgroup\$ – tjb1982 Oct 16 '14 at 13:53
1
\$\begingroup\$

C: 87

#define p(a)printf("Happy birthday %s\n",a?a:"to you");
main(){p(0)p(0)p("dear C")p(0)}

Demo

Compiles fine, but since it doesn't return, it might not exit correctly. If you want it to return 0, it'll cost another seven characters:

#define p(a)printf("Happy birthday %s\n",a?a:"to you");
main(){p(0)p(0)p("dear C")return!p(0)}

Demo

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

Burlesque - 55 Characters

"Happy Birthday to You"4.*g_15.+"Dear Burlesque".+2iauN

Test it here.

\$\endgroup\$
  • \$\begingroup\$ "Happy Birthday " "to You"4.*"Dear Burlesque"3ia?+uN would be a shorter version. \$\endgroup\$ – mroman Sep 25 '15 at 11:45
1
\$\begingroup\$

Python 85

[print('Happy Birthday %s'%('dear Python' if x==2 else 'to you')) for x in range(4)]
\$\endgroup\$
  • 2
    \$\begingroup\$ The if/else idiom can be generally shortened to a list selection: ['to you','dear Python'][x==2]. Instead of string formatting %, you can just use string concatenation. Take a look at some Python golf tips: codegolf.stackexchange.com/questions/54/… \$\endgroup\$ – xnor Oct 14 '14 at 7:05
1
\$\begingroup\$

Cobra - 78

class P
    def main
        print (t=(l='Happy Birthday ')+'to You\n')+t+l+'Dear Cobra\n'+t
\$\endgroup\$
1
\$\begingroup\$

Scala 89 (also it is not my favourite)

(1 to 4).map{i=>"Happy Birthday "+(if(i==3)"dear SCALA" else "to You")}.foreach{println}

Maybe it could be less, it is just a default example, so feel free to improve.

Regarding Java:

If ignoring the excess stuff it is also possible to do a "import static java.lang.System.*;" and then use out.println. So something like this is possible (87 chars):

int i=0;while(i<=3){out.println("Happy Birthday "+((i==2)?"dear Java":"to you"));i++;}

(also it is longer than the solution from Stretch Maniac - see https://codegolf.stackexchange.com/a/39768)

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

Befunge: 103

 >"yadhtriB yppaH"67+>:#,_1g:"0"-v
2^_@#-"0"g20p10-1,,,,,,," to You"_1+01p"egnufeB raeD ">:#,_"0"02p#
1
\$\endgroup\$
1
\$\begingroup\$

Railo CFML (79 characters)

for(i=1;i<5;i++){writeoutput("<br>Happy Birthday "&(i<>3?"to you":"dear CF"));}

Just having some fun creating a ridiculous mess:

  variables.endings = ["H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,t,o, ,y,o,u","H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,t,o, ,y,o,u","H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,d,e,a,r, ,C,o,l,d, ,F,u,s,i,o,n,n,n,n,n","H,a,p,p,y, ,B,i,r,t,h,d,a,y, ,t,o, ,y,o,u"];
  variables.iVerse = '';
  for (i = 1; i <= ArrayLen(variables.endings); i++) {
    variables.iLine = '';
    for (i2 = 1; i2 <= ListLen(variables.endings[i]); i2++) {
      iLine = iLine & ListGetAt(variables.endings[i],i2);
    }
    variables.iVerse = variables.iVerse & variables.iLine;
    if (i < ArrayLen(variables.endings)) {
      variables.iVerse = variables.iVerse & "<br />";
    }
  }

  writeOutput(variables.iVerse);
\$\endgroup\$
  • \$\begingroup\$ I know it's a little obvious what language this is, but please, describe what language you used and the character count of your code. Also, try to shorten it as much as you can. \$\endgroup\$ – g.carvalho97 Oct 13 '14 at 22:57
  • \$\begingroup\$ @g.carvalho97 I was having too much fun, sorry. I'll fix it. \$\endgroup\$ – Regular Joe Oct 13 '14 at 23:01
1
\$\begingroup\$

Bc: 62 characters

(This demonstrates the language's only golfing strength: string literals are displayed even without the print statement.)

while(i++<4){"Happy Birthday "
if(i-3)"To You
"else"Dear bc
"}

Sample run:

bash-4.3$ bc <<< 'while(i++<4){"Happy Birthday "
> if(i-3)"To You
> "else"Dear bc
> "}'
Happy Birthday To You
Happy Birthday To You
Happy Birthday Dear bc
Happy Birthday To You
\$\endgroup\$
1
\$\begingroup\$

Python 3 - 61

Based on @Falko's answer I made one for Python 3.3. I slightly improved his code by removing the space at the beginning of "Dear Python" and using the space between 'Birthday' and 'to'.

H="\nHappy Birthday to You"
print((H*3)[:60]+"Dear Python"+H)

Total number of characters is 60 61.(thanks for the recount @manatwork)

Edit:

Two slightly different approaches, all with the same number of characters:

T="\nHappy Birthday to You"
print(T*2+T[:-6]+"Dear Python"+T)

or

print(T*2+T[:16]+"Dear Python"+T)

BTW: I would like to post this as a comment, but I do not have the rep.

\$\endgroup\$
  • \$\begingroup\$ Sure about the 60 characters? Seems 61 to me. \$\endgroup\$ – manatwork Oct 14 '14 at 9:28
  • \$\begingroup\$ @manatwork: you are right. Somehow my character counter gave the wrong output. It is also logic that I loss one character compared with Falko, because I need two parentheses and win one character by changing the "Dear Python" phrase. \$\endgroup\$ – Hennep Oct 14 '14 at 9:33

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.