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

5
\$\begingroup\$

Reng, 66 bytes

Noncompeting.

A"uoY ot yadhtriB yppaH"¤ao;A"gneR raeD yadhtriB yppaH ~"S0gO0gao;

Simple enough. A is 10, or a newline. "uoY ot yadhtriB yppaH" pushes that string, ¤ duplicates the string, ao; outputs everything. The second part is more interesting. "A"gneR raeD yadhtriB yppaH ~" pushes that string. S pushes 28, and 0g sets the character at (0, 28) to the top of stack, or a ~, the end program signal. This sets the last A to a tilde. Then, O0g sets the character at(0, 24) to a space/nop, removing the duplicate stack command. We print the third line using ao;. Then, we wrap around to the beginning of the line and print the last line again.

Here's a GIF, because who doesn't like those?

GIIIIF

\$\endgroup\$
5
\$\begingroup\$

05AB1E, 36 bytes

”ŽØ¢©”©" to You"«Ð®" Dear 05AB1E"«s»

TIO Nexus

Try it online!

Output:

Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear 05AB1E
Happy Birthday to You

Explanation:

”ŽØ¢©”                                Push "Happy Birthday"
                                       STACK: ["Happy Birthday"]
      ©                               Store in register w/o popping
       " to You"«                     Append " to You"
                                       STACK: ["Happy Birthday to You"]
                 Ð                    Triplicate the string
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday to You"]
                  ®                   Push "Happy Birthday" from register
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday to You", "Happy Birthday"]
                   " Dear 05AB1E"«    Append " Dear 05AB1E"
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday to You", "Happy Birthday Dear 05AB1E"]
                                  s   Swap top two strings
                                       STACK: ["Happy Birthday to You", "Happy Birthday to You", "Happy Birthday Dear 05AB1E", "Happy Birthday to You"]
                                   »  Join by newlines
\$\endgroup\$
4
\$\begingroup\$

BBC BASIC, 81 / 76 ASCII characters

Download interpreter at http://www.bbcbasic.co.uk/bbcwin/bbcwin.html

Unfortunately, it's not possible to choose between strings within an expression, so the line endings are repetitively encoded in a DATA statement. On the other hand, when you read the last line, it does sound quite musical.

FORk=1TO4READa$PRINT"Happy Birthday ";a$:NEXT 
DATAto You,to You,Dear BASIC,to You

We can, however, choose what substring to select from a given string, for a saving of 5 characters (note that k=3 evaluates to -1 when true):

FORk=1TO4PRINT"Happy Birthday ";MID$("Dear BASICto You",11+(k=3)*10,10):NEXT

BBC BASIC, 69 ASCII characters

A shorter way:

a$="Happy Birthday "b$="to You"PRINTa$;b$'a$;b$'a$;"dear BASIC"'a$;b$
\$\endgroup\$
  • \$\begingroup\$ I had no idea that you didn't need a colon for one-liners... \$\endgroup\$ – Beta Decay Oct 15 '14 at 18:14
  • 1
    \$\begingroup\$ @BetaDecay according to the official syntax you do need the colon in most cases. But in practice BBC BASIC for Windows lets you get away without them. The original BBC Micro manual gave certain advice for improving the speed of your programs, of which I've taken in the following: use lowercase variable names, and eliminate as many unnecesary characters as possible. \$\endgroup\$ – Level River St Oct 15 '14 at 18:32
4
\$\begingroup\$

Groovy (64)

4.times{println"Happy Birthday ${it==2?'Dear Groovy':'to You'}"}
\$\endgroup\$
4
\$\begingroup\$

VBA 99

Why VBA? Why not?

Sub H(): For i = 0 To 3: MsgBox "Happy Birthday " & IIf(i = 2, "Dear VBA", "to You"): Next: End Sub

Ungolfed

Sub H()
    For i = 0 To 3
        MsgBox "Happy Birthday " & IIf(i = 2, "Dear VBA", "to You")
    Next
End Sub
\$\endgroup\$
  • 2
    \$\begingroup\$ Still shorter than C# :D \$\endgroup\$ – Brandon Oct 17 '14 at 12:22
  • \$\begingroup\$ For what it's worth (2.5yrs later), you can remove the auto-formatting to get it down to 86 bytes: Sub H():For i=0To 3:MsgBox"Happy Birthday " &IIf(i=2,"Dear VBA","to You"):Next:End Sub \$\endgroup\$ – Engineer Toast Apr 27 '17 at 13:03
  • \$\begingroup\$ @EngineerToast I don't feel like that's legit. The only environment that compiles that code enforces those spaces. \$\endgroup\$ – RubberDuck Apr 27 '17 at 13:31
  • 1
    \$\begingroup\$ I used to think that, too, so I did some searching on meta and found a post that made sense to me saying why autoformatting should be discounted. It's not wrong either way but I somewhat enjoy finding all the white space I can remove from VBA and have it still work. \$\endgroup\$ – Engineer Toast Apr 27 '17 at 14:02
  • \$\begingroup\$ @EngineerToast I'll actually buy that, because the official grammar doesn't require the white space. \$\endgroup\$ – RubberDuck Apr 27 '17 at 14:05
4
\$\begingroup\$

Common Lisp (62 60 chars)

(format t"~@{Happy Birthday ~[to You~;dear CL~]~%~}"0 0 1 0)

Output

Happy Birthday to You
Happy Birthday to You
Happy Birthday dear CL
Happy Birthday to You
\$\endgroup\$
  • \$\begingroup\$ Is the space before and after the string needed? \$\endgroup\$ – kay Oct 21 '14 at 1:40
  • \$\begingroup\$ @Kay No, the space is not needed. I removed it, which saves 2 chars. Thanks. \$\endgroup\$ – coredump Oct 21 '14 at 5:58
4
\$\begingroup\$

Casio CFX-9850G, 121 bytes 96

Just for the fun of it, the Happy Birthday song for the CFX-9850G graphing calculator.

Some instructions are just displayed as multiple bytes, but as far as I know they are encoded as 1 or 2 bytes. I marked them with colors. Edit: I checked the memory, so the program uses 121 bytes in the calculator.

The song text would not fit in a line, especially line 3 because in text mode the calculator can only display 7 rows and 21 columns. So I print the lyrics in graphing mode, which uses a tiny font.

Happy Birthday, CFX-9850G. Program

Here is the output:

Happy Birthday CFX-9850G. Output

\$\endgroup\$
4
\$\begingroup\$

Qt 5.4 qmake pro file, 119 bytes

Since the winner has already been announced, I thought I'd give a try for the witty category.

O=$$escape_expand(\n)
A=Happy Birthday
B=$$A to you$$O
CONFIG(debug,debug|release){log($$A dear qmake$$O)}log($$B)

This abuses qmake horribly.

qmake under default settings will parse a script 3 times over. For debug, release, and a build_pass. So we just let it throw out the "Happy Birthday" for all logs, with an extra "To you..." when a debug makefile is being generated.

Happy Birthday to you
Happy Birthday to you
Happy Birthday dear qmake
Happy Birthday to you
\$\endgroup\$
4
\$\begingroup\$

Retina, 47 49 50 52 bytes

The program is encoded as ISO-8559-1 (which the official interpreter accepts). The leading newline is significant.


¶¶x¶

Happy Birthday to You
.{27}u
Dear Retina

Try it online.

Explanation

Step 1: Replace the (empty) input with “newline, newline, x, newline”.

Step 2: In between every character, insert Happy Birthday to You. We now have:

Happy Birthday to You
Happy Birthday to You
Happy Birthday to YouxHappy Birthday to You
Happy Birthday to You

Step 3: replace a 28-character string ending in u and not containing a newline with Dear Retina. The only occurrence of that is to YouxHappy Birthday to You.

\$\endgroup\$
4
\$\begingroup\$

Java, 127 125 bytes

class h{public static void main(String[]a){String b="\nHappy Birthday ",d=b+"to You";System.out.print(d+d+b+"Dear Java"+d);}}

However, if you ignore all of the excess stuff, it is only 82 80 bytes:

String b="\nHappy Birthday ",d=b+"to You";System.out.print(d+d+b+"Dear Java"+d);

Oh, and it starts with a newline, saving 2 bytes.

\$\endgroup\$
  • 1
    \$\begingroup\$ output is not exactly as required, since there's an extra line at the start \$\endgroup\$ – Supuhstar Oct 17 '14 at 1:13
  • 3
    \$\begingroup\$ @Supuhstar It merely says to output the song happy birthday. It makes no requirement for excess newlines/whitespace. \$\endgroup\$ – Stretch Maniac Oct 17 '14 at 2:56
  • \$\begingroup\$ it says "writes the following text to the standard output or an arbitrary file:"... so... maybe you could stretch that to include extra bits? It doesn't sound like this is the intended answer, though. \$\endgroup\$ – Supuhstar Oct 17 '14 at 3:02
  • \$\begingroup\$ I'm not exactly sure what you mean, but System.out.println is a standard output. \$\endgroup\$ – Stretch Maniac Oct 17 '14 at 3:04
  • \$\begingroup\$ I don't mean the output method, but the actual output, which starts with a blank line \$\endgroup\$ – Supuhstar Oct 17 '14 at 3:14
4
\$\begingroup\$

Excel VBA, 687 686 686 681 Bytes

From the problem statement:

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.

Code

Full subroutine that takes no input and outputs a happy birthday song for VBA onto the range [A1:CM23] of the ActiveSheet object.

Global l
Sub a
Cells.RowHeight=48
For l=0To 3
p[A1:A5,B3,C1:C5,F1,E2:E5,G2:G5,F3,I1:I5,J1,K2,J3,M1:M5,N1,O2,N3,Q1:Q2,R3:R5,S1:S2]
p[Y1:Y5,Z1,AA2,Z3,AA4,Z5,AC1:AE1,AD2:AD4,AC5:AE5,AG1:AG5,AH1,AI2,AH3,AI4:AI5,AK1:AM1,AL2:Al5,AO1:AO5,AP3,AQ1:AQ5,AS1:AS5,AT1,AU2:AU4,AT5,AX1,AW2:AW5,AY2:AY5,AX3,BA1:BA2,BB3:BB5,BC1:BC2]
If l-2Then p[BI1:BK1,BJ2:BJ5,BN1,BM2:BM4,BN5,BO2:BO4,BU1:BU2,BV3:BV5,BW1:BW2,BZ1,BY2:BY4,BZ5,CA2:CA4,CC1:CC4,CD5,CE1:CE5]Else p[BI1:BI5,BJ1,BK2:BK4,BJ5,BM1:BM5,BN1:BO1,BN3,BN5:BO5,BR1,BQ2:BQ5,BS2:BS5,BR3,BU1:BU5,BV1,BW2,BV3,BW4:BW5,CC1:CC4,CD5,CE1:CE4,CG1:CG5,CH1,CI2,CH3,CI4,CH5,CL1,CK2:CK5,CM2:CM5,CL3]
Next
End Sub
Sub p(r)
r.Offset(6*l).Interior.Color=0
End Sub

Output

enter image description here

\$\endgroup\$
3
\$\begingroup\$

JavaScript, 74 73

for(i=5;--i;)console.log('Happy birthday',i-2?'to you':'dear JavaScript')

Thanks to Dancrumb for the byte-saving tip. It would be 68 67 bytes if I could use 4 alerts instead of newlines.

for(i=5;--i;)alert('Happy birthday',i-2?'to you':'dear JavaScript')
\$\endgroup\$
  • 1
    \$\begingroup\$ You can shave off a byte with: for(i=5;--i;)console.log('Happy birthday',i-2?'to you':'dear JavaScript') \$\endgroup\$ – Dancrumb Oct 13 '14 at 16:15
  • 1
    \$\begingroup\$ If you want alert(), then use only 1, based on my comment at the other JavaScript answer. That will be 71 character without alert() flood. \$\endgroup\$ – manatwork Oct 13 '14 at 16:26
  • \$\begingroup\$ You could also avoid the loops and let the output just be the expression itself (which is true on newer consoles): (a="\nHappy Birthday")&&(b=a+" to You")&&b+b+a+" Dear JavaScript"+a (67 too). \$\endgroup\$ – AmadeusDrZaius Oct 18 '14 at 19:19
3
\$\begingroup\$

J (44)

2|.'Happy birthday ',"1'dear J',>3#<'to you'
\$\endgroup\$
3
\$\begingroup\$

Swift, 83

Run this in Xcode Playground

let h="\nhappy birthday"
let t=" to you"
let f=h+t+h+t+h+" dear Swift"+h+t
print(f)
\$\endgroup\$
  • \$\begingroup\$ If you put the space after happy birthday, you can get rid of the two in to you and dear Swift, saving you one byte \$\endgroup\$ – Beta Decay Oct 15 '14 at 6:11
  • \$\begingroup\$ If you declare all 3 variables on the same line, you can save 8 bytes. \$\endgroup\$ – NobodyNada - Reinstate Monica Oct 18 '14 at 20:56
3
\$\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\$
3
\$\begingroup\$

Powershell one liner: 73 71

1..4|%{"Happy Birthday $(if($_-eq3){"Dear Powershell"}else{"to You"})"}

\$\endgroup\$
  • 1
    \$\begingroup\$ You could shrink this to 71 if you did $_-eq3 \$\endgroup\$ – SomeShinyMonica Oct 16 '14 at 13:06
3
\$\begingroup\$

APL: 45

4 23⍴'Dear APL',⍨61⍴'Happy birthday to you  '

Explanation:

61⍴ reapeats the string up to length 61, after which "Dear APL" is concatenated (,)

Note that ⍨ inverts arguments, so it's added at the end.

4 23⍴ just makes a matrix 4x23 using the string on the right, starting from the beginning of the string each time it reaches the end.

\$\endgroup\$
3
\$\begingroup\$

Pyth, 42

V4+"Happy Birthday "?"to You"nN2"Dear Pyth

Demonstration:

$ pyth -c 'V4+"Happy Birthday "?"to You"nN2"Dear Pyth'
Happy Birthday to You
Happy Birthday to You
Happy Birthday Dear Pyth
Happy Birthday to You

How it works:

V4 sets up a for loop, with N iterating over 0, 1, 2, 3.

Then, we create the string starting with "Happy Birthday ", and ending with "to You" if N does not equal 2 (nN2 calculates this), or ending with "Dear Pyth" otherwise.

Exact tie with CJam - language name and character count.


Alternative, 43 character solution:

J"Happy Birthday to You"JJ+<J15"Dear Pyth"J

It's one character longer because it needs an end quote.

\$\endgroup\$
  • \$\begingroup\$ Aren't the V and N implemented after the question was asked ? \$\endgroup\$ – Optimizer Oct 21 '14 at 19:38
  • 1
    \$\begingroup\$ @Optimizer No, they were added a while ago. Looking back at the commit log, they were added on August 21st, in a commit called: Pyth 2.1.0 - Changes to -;\AcPUVXz Out of curiosity, what gave you that impression? \$\endgroup\$ – isaacg Oct 21 '14 at 19:50
  • \$\begingroup\$ Your recent Pyth tips answer left me thinking that it is something new and u always keep saying that you keep on adding features :). \$\endgroup\$ – Optimizer Oct 21 '14 at 19:56
  • \$\begingroup\$ @Optimizer Ah - while I do add new things, I'm also trying to give general programming tips on that page, which cover everything in Pyth. \$\endgroup\$ – isaacg Oct 21 '14 at 20:21
3
\$\begingroup\$

Julia, 75 67 65 characters

a,b,c="Happy Birthday ","To You\n","Dear Julia\n";print("$a$b$a$b$a$c$a$b")

a="Happy Birthday";b=("$a To You\n");print("$b$b$a Dear Julia\n$b")

a="Happy Birthday To You\n";print("$a$a$(a[1:15])Dear Julia\n$a")

Just for fun, here is an ungolfed version, with a loop (114 characters):

for i = 1:4
    a,b,c = "Happy Birthday ","To You","Dear Julia"
    i != 3 ? println("$a$b") : println("$a$c")
end
\$\endgroup\$
3
\$\begingroup\$

Bubblegum, 42 bytes

0000000: f3 48 2c 28 a8 54 70 ca 2c 2a c9 48 49 ac  .H,(.Tp.,*.HI.
000000e: 54 28 c9 57 88 cc 2f e5 22 4a d4 25 35 b1  T(.W../."J.%5.
000001c: 48 c1 a9 34 29 29 27 35 bd 34 17 bb 1e 00  H..4))'5.4....

Note that this answer is non-competing, since Bubblegum is a lot younger than this challenge.

\$\endgroup\$
3
\$\begingroup\$

Fishing, 116 bytes

v+CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
  `Happy Birthday `P{`to You`N}P{N}P{{`Dear Fishing`N}}P{N
\$\endgroup\$
  • \$\begingroup\$ I've posted a few questions, but here's my first answer! \$\endgroup\$ – Arcturus Oct 9 '15 at 23:25
3
\$\begingroup\$

Seriously, 59 57 bytes (non-competitive)

"Happy Birthday to You";"Happy Birthday Dear Seriously"@;

Explanation

Push "Happy Birthday to You", duplicate, push "Happy Birthday Dear Seriously", rotate the two top elements, duplicate, and implicit popping and printing.

\$\endgroup\$
  • \$\begingroup\$ 57 bytes: "Happy Birthday to You";"Happy Birthday Dear Seriously"@; \$\endgroup\$ – Mego Jan 8 '16 at 4:24
3
\$\begingroup\$

///, 48 bytes

/U/Happy Birthday //T/Uto You/T
T
UDear \/\/\/
T
\$\endgroup\$
3
\$\begingroup\$

Underload, 59 Bytes

(to You
):(Happy Birthday ):S~S:S~:S~:S(Dear Underload
)SSS

I was hindered by Underload's lack of stack manipulation commands. I had to push stuff in a strange order and swap a bit more than I would have preferred, but oh well...

Stack trace:

(...) | (to You)
:     | (to You)(to You)
(...) | (to You)(to You)(Happy Birthday )
:S    | (to You)(to You)(Happy Birthday )         ; output "Happy Birthday "
~     | (to You)(Happy Birthday )(to You)
S     | (to You)(Happy Birthday )                 ; output "to You"
:S    | (to You)(Happy Birthday )                 ; output "Happy Birthday "
~     | (Happy Birthday )(to You)
:S    | (Happy Birthday )(to You)                 ; output "to You"
~     | (to You)(Happy Birthday )
:S    | (to You)(Happy Birthday )                 ; output "Happy Birthday "
(...) | (to You)(Happy Birthday )(Dear Underload)
S     | (to You)(Happy Birthday )                 ; output "Dear Underload"
S     | (to You)                                  ; output "Happy Birthday "
S     |                                           ; output "to You
\$\endgroup\$
3
\$\begingroup\$

Brainfuck, 593 characters

Used this

++++++++++[>+>+++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++<<<<<<<<<-]>>>>++.>>+++++++.>>++..>+.<<<<<<<++.>++++++.>>>>+++++.>++.++.<-.----.<.>>>.<<<<<<<.>>>>>>.-----.<<<<<<.>>>+++++++++.>>>.++++++.<<<<<<<.>>>.>>.>>-----..>.<<<<<<<.>.>>>>+++++.>++.++.<-.----.<.>>>.<<<<<<<.>>>>>>.-----.<<<<<<.>>>.>>>.++++++.<<<<<<<.>>>.>>.>>-----..>.<<<<<<<.>.>>>>+++++.>++.++.<-.----.<.>>>.<<<<<<<.>++.>>>>+.<.>>--.<<<<<<.>--.>>>>>.<<.>++++.>----.<---.>+++++++.<<++.>+++++.<<<<<<.>>>.>>--.>>-----..>.<<<<<<<.>.>>>>--.>++.++.<-.----.<.>>>.<<<<<<<.>>>>>>.-----.<<<<<<.>>>.>>>.++++++.
\$\endgroup\$
3
\$\begingroup\$

C++, 135 bytes

Great thanks to Steadybox and DJMcMayhem for shortening this code by 55 characters! :)

#include<iostream>
#include<string>
std::string s="Happy Birthday ",t="to you\n",u="Dear C++\n";int main(){std::cout<<s+t+s+t+s+u+s+t;}
\$\endgroup\$
  • 1
    \$\begingroup\$ You can use + instead of <<. \$\endgroup\$ – DJMcMayhem Jan 11 '18 at 17:58
  • 1
    \$\begingroup\$ Welcome to PPCG! You can remove the space from between #include and <headerName>. Also, you can declare the strings in one go so you don't have to repeat the string. (After that using the std:: prefix is shorter than using namespace std;, too). Like this. \$\endgroup\$ – Steadybox Jan 11 '18 at 18:11
2
\$\begingroup\$

Lua: 91 72 characters

First pass:

for i=1,4 do io.write'Happy Birthday 'if i==3 then print'Dear Lua'else print'to You'end end

Demo

Even smaller:

for i=1,4 do print('Happy Birthday '..(i==3 and'Dear Lua'or'to You'))end

Demo

TI-BASIC: 65

:PROGRAM:H
:"HAPPY BIRTHDAY TO YOU
:Disp Ans,Ans,sub(Ans,1,15)+"DEAR TI-BASIC
:Disp Ans

In TI-BASIC, all built-in commands (even multi letter ones) are either one or two bytes on the calculator. The size I used represents the (minimum, since names take space) space taken by this code on my TI-84+SE running OS version 2.53MP. Seemingly wrong syntax (unclosed strings) is intentional.

TI-68k: 90

:h()
:Prgm
:"Happy Birthday to You"→a
:Disp a,a,mid(a,1,15)&"Dear TI-68k",a
:EndPrgm

Again, the size I used is the minimum real size on the calc (in this case, a TI-92).

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

R, 69 chars

for(i in 1:4)cat('Happy Birthday',ifelse(i==3,'Dear R','to You'),'\n')

Or

for(i in 1:4)cat('Happy Birthday',if(i==3)'Dear R' else 'to You','\n')

All output:

Happy Birthday to You 
Happy Birthday to You 
Happy Birthday Dear R 
Happy Birthday to You 

Otherwise with 66 characters there was, without the for loop:

a=rep('to You',4);a[3]='Dear R';cat(paste('Happy Birthday',a,'\n'))

But it outputs:

Happy Birthday to You 
 Happy Birthday to You 
 Happy Birthday Dear R 
 Happy Birthday to You 
\$\endgroup\$
2
\$\begingroup\$

C#

LINQPad (69):

string x="\nHappy Birthday ",y=x+"to You";(y+y+x+"Dear C#"+y).Dump();

Console (75):

string x="\nHappy Birthday ",y=x+"to You";Console.Write(y+y+x+"Dear C#"+y);

Ungolfed:

string x = "\nHappy Birthday ";
string y = x + "to You";

//LINQPad
(y + y + x + "Dear C#" + y).Dump(); 

//Console
Console.Write(y+y+x+"Dear C#"+y);
\$\endgroup\$
2
\$\begingroup\$

Rust, 98 bytes

fn main(){for i in range(0i,4){println!("Happy birthday {}",match i{2=>"dear Rust",_=>"to you"})}}
\$\endgroup\$
  • \$\begingroup\$ Suggest 0..4 instead of range(0i,4) \$\endgroup\$ – ceilingcat Nov 8 '17 at 22:13
  • \$\begingroup\$ @ceilingcat: And swap match for if...else, use print!,neglect fn main: 78 bytes tio.run/##KyotLvmflqeQm5iZp6FZ/… \$\endgroup\$ – Phil H Sep 4 at 10:28

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.