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

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\$
  • \$\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\$ – squeamish ossifrage Apr 19 '17 at 11:44
  • \$\begingroup\$ @squeamishossifrage fixed. \$\endgroup\$ – Khaled.K Apr 19 '17 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\$

tcl, 69 68 65

puts "[set t [set h Happy\ Birthday]\ To\ You]
$t
$h Dear tcl
$t"

demo

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

VBA, 58 Bytes

Anonymous VBE immediate window function that takes no input and makes VBA sing its own happy birthday song, alone.

h="Happy Birthday ":t="to You":?h;t:?h;t:?h"Dear VBA":?h;t
\$\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\$

AWK, 70 bytes

BEGIN{for(x=-3;x<1;)print"Happy Birthday "(s=++x?"to You":"Dear AWK")}

Try it online!

I didn't see an AWK solution, so here we go. :)

\$\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\$
  • 1
    \$\begingroup\$ You don't need to concatenate them; they'll all be printed anyway \$\endgroup\$ – user16402 Oct 13 '14 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 '14 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\$
  • \$\begingroup\$ 40 bytes \$\endgroup\$ – dzaima Jul 30 '18 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\$
0
\$\begingroup\$

Attache, 54 bytes

Print=>4&("Happy Birthday "+2&"To You"'"Dear Attache")

Try it online!

Explanation

2&"To You"'"Dear Attache"
2&"To You"                  resizes "To You" into a 2-length array
          '"Dear Attache"   concatenate with "Dear Attache"

                            result: ["To You", "To You", "Dear Attache"]


"Happy Birthday "+_         Adds this string to each element
                            result: ["Happy Birthday To You", "Happy Birthday To You", "Happy Birthday Dear Attache"]

4&(_)                       Resizes the array to size 4
                            result: ["Happy Birthday To You", "Happy Birthday To You", "Happy Birthday Dear Attache", "Happy Birthday To You"]

Print=>_                    Prints each member on its own line
\$\endgroup\$
0
\$\begingroup\$

jamal, 57 characters

{@for _(///dear jamal/0)Happy Birthday {@if/_/_/to You}
}

Sample run:

bash-4.4$ jamal.pl birthday.jam 
Happy Birthday to You
Happy Birthday to You
Happy Birthday dear jamal
Happy Birthday to You
\$\endgroup\$
0
\$\begingroup\$

C\C++ Preprocessor, 244 224 bytes

Live in C

Live in C++

#define A "to"
#define B "You"
#define C "C"
#ifdef __cplusplus
#define C "C++"
#endif
#define D "Dear"
#define E "Happy Birthday"
#define H printf("%s %s %s \n",E,A,B)
int main(){H;H;printf("%s %s %s \n",E,D,C);H;return 0;}
\$\endgroup\$
  • \$\begingroup\$ You don't need the "return 0"... Also, too many whitespaces. Why do you split E and F? ... \$\endgroup\$ – user202729 Jun 26 '18 at 8:23
  • \$\begingroup\$ @user202729 just made E and F the same thing, return is needed for it to work in C++ \$\endgroup\$ – Khaled.K Jun 26 '18 at 17:54
0
\$\begingroup\$

Charcoal, 56 bytes

≔Happy Birthdayη≔ to YouτP⁺ητ↓P⁺ητ↓P⁺η”↶ G↗5▶h…≡-⌕”↓P⁺ητ

Try it online! (Link to the verbose version.)

It wasn't fair that Charcoal still hadn't its own birthday party...

\$\endgroup\$
  • \$\begingroup\$ 47 \$\endgroup\$ – ASCII-only Aug 6 '18 at 6:23
0
\$\begingroup\$

Noether, 54 bytes

"Happy Birthday "~s"to You"+~mP?P?sP"Dear Noether"P?mP

Try it online!

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

MBASIC, 93 bytes

1 FOR I=1 TO 4:PRINT"Happy Birthday ";:IF I=3 THEN PRINT"Dear MBASIC"ELSE PRINT"to You"
2 NEXT
\$\endgroup\$
0
\$\begingroup\$

MathGolf, 45 bytes

"Happy Birthday to You"·E<"Dear MathGolf"+\]n

Try it online!

\$\endgroup\$
  • \$\begingroup\$ wow this challenge is 4 years old now \$\endgroup\$ – qazwsx Oct 6 '18 at 1:43
  • \$\begingroup\$ I was surprised that there weren't any answers from some newer golfing languages, but I like these simple challenges. With some string compression this answer could be shorter, but I haven't implemented that yet. \$\endgroup\$ – maxb Oct 6 '18 at 7:02
0
\$\begingroup\$

A0A0 - 309 bytes

A0A0
A0C3G1G1G1G1G1G1G1G1G1G1G1G1G1G1G1A0
A0P72P97P112P112P121P32P66P105P114P116P104P100P97P121P32G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3A0
G-3
G1G4G7G4
A0A0
A0C3G1G1G1G1G1G1G1A0
A0P116P111P32P89P111P117P10G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3A0
G-3
G-8G-8
P68P65
P101P48
P97P65
P114P48
P32P10
G-5G-14

Explanation:

A0A0
A0C3G1G1G1G1G1G1G1G1G1G1G1G1G1G1G1A0
A0P72P97P112P112P121P32P66P105P114P116P104P100P97P121P32G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3G-3A0
G-3

Creates a loop which prints "Happy Birthday " and jumps every iteration.

G1G4G7G4

The positions the code should jump to each iteration.

A0A0
A0C3G1G1G1G1G1G1G1A0
A0P116P111P32P89P111P117P10G3A0
A0A1G-3G-3G-3G-3G-3G-3G-3A0
G-3

Creates a loop which will print "to you" and jumps out of the loop after each iteration.

G-8G-8

More positions to jump to, but this time for the second loop (also acts as a sort of counter, adding an additional G-8 would print "Happy Birthday " one more time).

P68P65
P101P48
P97P65
P114P48
P32P10
G-5G-14

Prints "Dear A0A0" and jumps back to the first loop.

Test it here.

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

Cubically, 251 bytes

⇒:4+@:5+2/1+55@+2S-0@@S'+1@f2/1+52@F+5@F'+1@:4/1+6255@U-1@U'/6+1*@S-0@S'+1/1*@f2
⇒:5/1+3@
⇒/6+6366@/4+552@f2/67+55@/3+552@B+1@B'/6+1@
f1f3f1f3f1//3+6635@/3+551@:5+2/1+55@/3+6552@f2+/3+165@-6+355@/6++++55@/3+6551@:1+55@:5+2/1+55@:2+55@@/6+61*@/6+1@f1f3

Try it online!

Explanation:

First, second and third lines are functions.
First line prints "Happy Birthday " (note the trailing space)
Second line prints a space (to golf it down)
Third line prints "to You\n"

Fourth line is the program. It simply calls functions and prints "Dear Cubically\n" on the third line.

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

C (gcc) and aplay, 360, 175, 167, 151 bytes

Generates the contents of a WAV file onto stdout, singing the melody of Happy Birthday. The output can then be piped to, for example, aplay to listen to it.

Including -lm causes a +4 to score.

Also, thanks to ceilingcat for golfing a couple dozen bytes.

c,i;main(t,T){for(;i<25;i++)for(t=T="$$(((0$$(((0$$((((($$(((0"[i]-32<<9;t--;)putchar(c=sin(exp("!!#!&%!!#!(&!!-*&%#++*&(&"[i]/17.-2)*t)*9*t/T+9);}

Try it online! (won't produce sound, duh...)

Now available on Clyp for listening. Amplified for convenience, may be loud and/or pop on some devices.

Try it offline!

Compile and listen with gcc -w src.c -lm && ./a.out | aplay

Degolf

c,i;
main(t,T) {
    for(;i<25;i++) // 25 notes of the song in the loop, 25 notes of the song
        for(t=T="$$(((0$$(((0$$((((($$(((0"[i]-32<<9; // Select number of samples
                t--;)
            // See below, this has been mutilated quite a bit to golf it.
            // Assign to c for implicit cast to int.
            // A small shortcut is made by only dampening the sine-part of the wave.
            putchar(c=sin(exp("!!#!&%!!#!(&!!-*&%#++*&(&"[i]/17.-2)*t)*9*t/T+9);
}

A sine wave is defined as \$s(t)=A\cdot\sin 2\pi t\$. A sine wave with frequency \$f\$ can thus be expressed as \$s(ft)\$. Now, in this case \$\{t\in\mathbb N \mid0\leq t \leq T \}\$ and \$T:=n\cdot2^{11}\$, so we have to divide \$t\$ with 2048, in order to make the signal function work. Our function is now \$s(2^{-11}\cdot ft)\$.

Since listing the frequencies would take a significant amount of bytes, I have encoded the frequencies as halfsteps relative to A4, represented by 40 or ( in the string. The frequency is thus obtained \$f=440Hz \cdot 2^{c-40\over12}\$. Finally, putting it all together, adding in some dampening and ensuring outputs greater than zero, we get:

$$ f(t)= \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({880 \pi t \cdot 2^{c-40\over12}\cdot2^{-11}} \Big)\Bigg) $$

$$ f(t)= \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({880 \pi t \cdot \exp\Big({{c\ln 2-40\ln 2\over12} - 11 \ln 2}}\Big) \Big)\Bigg) $$

$$ f(t)= \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({ t \cdot \exp\Big({{c\ln 2-40\ln 2\over12} - 11 \ln 2 + \ln 880 \pi}}\Big) \Big)\Bigg) $$

$$ f(t)\approx \Bigg({T - t\over T}\Bigg) \Bigg({A\over2}+{A\over2}\sin \Big({t \cdot \exp \Big({c\over17}-2\Big)} \Big)\Bigg) $$

And finally, we select \${A\over2}=9\$ to conserve bytes.

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

Gol><>, 55 bytes

a"Happy birthday to you"3F6sK|6sR}a"Dear Gol><>"6sR{r~H

Voila, explanation coming soon!

Try it online!

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

C# LinqPad, 103 bytes

Regex.Replace(String.Join("\n",Enumerable.Repeat("Happy Birthday to You",4)),"^((.*\n){2}.*)to.+","$1C#")
\$\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.