Write a complete program that takes a boolean or non-negative integer input. It must:

  • Output its own source code if the input value is falsy
  • Output its own source code in reverse if the input value is truthy

Your program cannot be palindromic, nor can it read its own source code by any means.

This is code golf - shortest code in bytes wins.

  • 6
    \$\begingroup\$ So... if my language doesn't have booleans. But 0 is falsy and positive integers are truthy. Can I assume that the input will be only 0 or 1 (as stand-ins for booleans - the language will in fact always yield one of those two as the result of a conditional operator)? Or do I have to support any integers, since I can't use "actual" booleans? \$\endgroup\$ Commented Oct 30, 2015 at 14:47

12 Answers 12


CJam, 17 16 bytes


Test it here.

A fairly straight-forward modification of the standard quine. Other solutions for 17 bytes:


If I can assume that the input is only 0 or 1 (as a stand-in for a boolean, which there is no dedicated type for in CJam), I get 15 by omitting the g:



{`"_~"+  e# Standard generalised quine framework. Leaves the source code on the stack. 
  W      e# Push a -1.
  q~     e# Read and evaluate input.
  g      e# signum, turning truthy values into 1 (leaving 0 unchanged).
  #      e# Power. -1^0 == 1, -1^1 == -1.
  %      e# Select every Nth element: a no-op for 1 and reverses the string for -1.
  • \$\begingroup\$ Since 0 and 1 are CJam's Booleans, I don't think you need the g. \$\endgroup\$
    – Dennis
    Commented Oct 30, 2015 at 14:45
  • \$\begingroup\$ @Dennis Yeah I wasn't really sure about that. \$\endgroup\$ Commented Oct 30, 2015 at 14:46
  • \$\begingroup\$ Surprisingly, I somehow beat this. o-o I won't be surprised if you golf it down more though. :D \$\endgroup\$ Commented Oct 30, 2015 at 21:53

Gol><>, 9 bytes


I feel a little awkward posting this, since we already have ><>, Vitsy and Minkolang answers. The only additions to the standard quine are I (read integer input), ? (execute next if truthy) and r (reverse stack).

Try it online.

  • \$\begingroup\$ Join the club! :D You're winning anyways. +1 \$\endgroup\$ Commented Nov 1, 2015 at 15:34
  • 1
    \$\begingroup\$ 8 bytes: sP#Hr?I" \$\endgroup\$
    – Jo King
    Commented Aug 14, 2018 at 13:27

Pyth, 17 bytes


A straightforward modification of the standard Pyth quine.

  • \$\begingroup\$ Damn, I'd been waiting to post this since an hour after it was sandboxed. \$\endgroup\$
    – lirtosiast
    Commented Oct 30, 2015 at 15:50

><>, 17 Bytes

Requires the -v flag (+1 byte) for pushing input onto the stack (or for you to put input on the stack beforehand on the online interpreter).


You could do the below for the same amount of bytes (without the flag) if integer inputs only were allowed (i.e. 0 for falsy, 1 for truthy).


Try it online

Truthy/falsy for ><> are anything not 0 and 0, respectively.


Vitsy, 15 Bytes

...I'm... I'm beating CJam! (shouts over) Mom! Look, ma, I deed it!



Standard quine, but with a twist:

'               Capture the source code as a string
 rd3*           Create the ' character in ASCII
     i86*-      Get the input character as ASCII then subtract 48 from it. 
                    If it's zero, it'll the if statement will skip the next instruction.
          (r    If the top item of the stack is zero, do not do the next item.
                    The next item here is reverse.
            l\O Print out the stack.

Newer Version of Vitsy, 11 Bytes

v            Capture the input as a variable.
 '           Capture the source until encountering another '
  r          Reverse the stack.
   d3*       Push ' to the stack.
      }      Rotate the stack to the right one.
       v)    Push the variable (input) to the stack and test if it is not zero.
         r   If so, reverse the stack.
          Z  Output everything in the stack.

Python 2, 51 bytes

s="print('s=%r;exec s'%s)[::1-2*0**input()]";exec s

Javascript (ES6), 42 bytes


This is a modification of my Bling Quine. It's twice as long, too.

  • \$\begingroup\$ Does this actually output anything? Answers that only work in a REPL are not allowed. \$\endgroup\$
    – feersum
    Commented Oct 30, 2015 at 14:10
  • 1
    \$\begingroup\$ @ETHproductions That would call the function, but it still wouldn't print anything. Also, it wouldn't be a quine anymore. \$\endgroup\$
    – Dennis
    Commented Oct 30, 2015 at 14:51
  • \$\begingroup\$ @Dennis Right. Well, I guess that means prompt() is necessary, unless we switch to Node.JS. I believe $=_=>(A=`$=${$};$(prompt())`,alert(_?A:[...A].reverse().join``));$(prompt()) works properly, although maybe it could be golfed down more. \$\endgroup\$ Commented Oct 30, 2015 at 15:19
  • 1
    \$\begingroup\$ "Your program cannot [...] read its own source code by any means." Does this solution fall into this category? \$\endgroup\$ Commented Oct 30, 2015 at 21:57

Burlesque, 40 bytes

ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh


Burlesque has advanced stack and code manipulation built-ins. In fact, you can't access the source code of the program but you can access the remaining code that is to be executed in the future. This means #Q will return all the code that follows it which is why we have to add everything up to #Q to that code which is what we're doing with ri#Q.

blsq ) #Q1 2++
12 -- this is the result of 1 2++
{1 2 ++} -- this is the result of #Q

++1 2 is technically illegal code since it's stack based. But we can manipulate the code to make it execute as 1 2++:

blsq ) #Q<-#q++1 2 

Working with these built-ins is incredibly tricky and nobody has yet used them for anything productive except for quine related things. If you reverse ++1 2 you get 2 1++ which would produce 21 and not 12. The reason the code above produces 12 is because #Q also includes the <- so in the end we end up executing a lot more than just 2 1++ :p. We end up executing 2 1++#q<- which produces 12.

We can actually replace things in our code for example this code replaces all occurences of ?+ in itself with ?*

blsq ) #Q(?+)(?*)r~5.-#q5 5?+


$ echo "1" | blsq --stdin 'ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh'
hs fi sp "-<" j +. /\ "Q#ir" -~ HS 2Q#ir
$ echo "0" | blsq --stdin 'ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh'
ri#Q2 SH ~- "ri#Q" \/ .+ j "<-" ps if sh

Haskell, 126 118 108 bytes

main=readLn>>=putStr.([t,reverse t]!!);t=s++show s;s="main=readLn>>=putStr.([t,reverse t]!!);t=s++show s;s="

Expects 0 or 1 as input.


Minkolang 0.10, 13 bytes


Try it here.

This is exactly like the standard quine except for these four characters: n,?r. n takes an integer from input, , inverts it, so ? skips r if the input is truthy. Otherwise, r reverses the stack so that it is output in reverse order.

  • 1
    \$\begingroup\$ My thunder is gone. :c \$\endgroup\$ Commented Oct 30, 2015 at 21:58

Java 10 (full program), 282 bytes

interface N{static void main(String[]a){var s="interface N{static void main(String[]a){var s=%c%s%1$c;s=s.format(s,34,s);System.out.print(new Boolean(a[0])?new StringBuffer(s).reverse():s);}}";s=s.format(s,34,s);System.out.print(new Boolean(a[0])?new StringBuffer(s).reverse():s);}}

Try it online.

Java 10 (as lambda function), 154 bytes

b->{var s="b->{var s=%c%s%1$c;s=s.format(s,34,s);return b?new StringBuffer(s).reverse():s;}";s=s.format(s,34,s);return b?new StringBuffer(s).reverse():s;}

Try it online.


interface N{                          // Class
  static void main(String[]a){        //  Mandatory main-method
    var s="interface N{static void main(String[]a){var s=%c%s%1$c;s=s.format(s,34,s);System.out.print(new Boolean(a[0])?new StringBuffer(s).reverse():s);}}";
                                      //   Unformatted source code
    s=s.format(s,34,s);               //   Format the source code (the quine-String)
    System.out.print(                 //   Print:
      new Boolean(a[0])?              //    If the input is true:
       new StringBuffer(s).reverse()  //     Print the quine-String reversed
      :                               //    Else:
       s);}}                          //     Print the quine-String as is


  • The var s contains the unformatted source code
  • %s is used to put this String into itself with s.format(...)
  • %c, %1$c, and 34 are used to format the double-quotes
  • s.format(s,34,s) puts it all together

And then new StringBuffer(s).reverse() is used to reverse the quine-String if necessary based on the input-boolean.


05AB1E, 21 bytes


Try it online.

Modification of the default 0"D34çý"D34çý by adding ‚sè.


0                        # Push 0 to the stack
                         #  STACK: [0]
 "D34çý‚sè"             # Push the string 'D34çý‚sè' to the stack
                         #  STACK: [0, 'D34çý‚sè']
            D            # Duplicate this string
                         #  STACK: [0, 'D34çý‚sè', 'D34çý‚sè']
             34ç         # Push '"' to the stack
                         #  STACK: [0, 'D34çý‚sè', 'D34çý‚sè', '"']
                ý        # Join the stack with '"' delimiter
                         #  STACK: ['0"D34çý‚sè"D34çý‚sè']
                 Â       # Bifurcate (short for Duplicate & Reverse)
                         #  STACK: ['0"D34çý‚sè"D34çý‚sè', 'ès‚Âýç43D"ès‚Âýç43D"0']
                  ‚      # Pair them up:
                         #  STACK: [['0"D34çý‚sè"D34çý‚sè','ès‚Âýç43D"ès‚Âýç43D"0']]
                   s     # Swap to get the boolean input (0 or 1)
                         #  STACK: [['0"D34çý‚sè"D34çý‚sè','ès‚Âýç43D"ès‚Âýç43D"0'], 1]
                    è    # Index the input into the list
                         #  STACK: ['ès‚Âýç43D"ès‚Âýç43D"0']
                         # (Output the top of the stack implicitly)

PS: Automatically prints a trailing newline. If this should be reversed as well, it's 23 bytes instead:


Try it online. (? is an explicit Print without new-line)


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.