| Bytes | Lang | Time | Link |
|---|---|---|---|
| 002 | Uiua | 241128T042009Z | ErikDaPa |
| 007 | Raku Perl 6 rakudo | 250424T164200Z | xrs |
| 030 | SAKO | 250325T195909Z | Acrimori |
| 006 | BrainChild ASM | 241128T043333Z | ATaco |
| 6166 | Piet + asciipiet | 250325T232723Z | anderium |
| 024 | Tcl | 170502T224653Z | sergiol |
| 008 | brainfuck | 170503T011932Z | Dennis |
| 049 | Bespoke | 250311T160341Z | Josiah W |
| 003 | 32bit Arm32 assembly 12 bytes | 250311T130616Z | petroleu |
| 004 | Thumb2 assembly 8 bytes | 250311T130253Z | petroleu |
| 008 | CASIO BASIC CASIO fx9750GIII | 241120T160629Z | madeforl |
| 002 | Uiua | 241118T014835Z | nyxbird |
| 005 | Easyfuck | 240322T132853Z | Quadrupl |
| 004 | BitCycle u | 180117T221832Z | DLosc |
| 017 | Swift | 231231T191038Z | macOSist |
| 010 | Desmos | 170504T012624Z | DanTheMa |
| 008 | R 4.1+ | 231230T072155Z | qwr |
| 005 | Gema | 231230T013745Z | manatwor |
| 021 | Python 3 | 231229T154155Z | Larry Ba |
| 017 | Commodore BASIC CBM/PET | 230530T153844Z | Shaun Be |
| 003 | RegEx | 230530T134709Z | The Empt |
| 024 | Lua | 230530T113245Z | bluswimm |
| 013 | Desmoslang | 230530T034621Z | Dadsdy |
| 002 | Thunno 2 | 230528T143232Z | The Thon |
| 002 | Nekomata | 230528T155902Z | alephalp |
| 015 | Python | 230528T155003Z | The Empt |
| 003 | Pyt | 230225T142133Z | Kip the |
| nan | 230114T134746Z | The Thon | |
| 011 | C gcc | 170504T150213Z | Raycho M |
| 060 | Aussie++ | 220318T221228Z | Bbrk24 |
| 003 | Vyxal | 211223T191845Z | Larry Ba |
| 008 | APOL | 211215T184423Z | Ginger |
| 019 | Python 3 | 211214T145735Z | Larry Ba |
| 025 | Forte | 210606T000727Z | DLosc |
| 041 | Acc!! | 180117T231131Z | DLosc |
| 042 | C clang | 210605T062929Z | Stack Ex |
| 062 | Lolwho.Cares | 210604T223330Z | Robot |
| 012 | Swift | 210604T214508Z | Eric Xue |
| 007 | BRASCA | 210128T113021Z | SjoerdPe |
| 012 | Pxem | 210602T124738Z | user1004 |
| 082 | MMIX | 210516T003751Z | NoLonger |
| 008 | AWK | 210513T004050Z | Pedro Ma |
| 002 | Vyxal | 210512T212600Z | Undersla |
| 016 | Python 3 | 210128T102729Z | Mthd |
| 007 | Sed | 180424T080745Z | Toby Spe |
| 011 | Javascript | 210128T113404Z | Rohan |
| 006 | convey | 210128T051012Z | Jo King |
| 006 | ARM Thumb2 machine code | 210127T212651Z | EasyasPi |
| 002 | MathGolf | 210127T193635Z | Kevin Cr |
| 024 | Rockstar | 200928T103618Z | Shaggy |
| 006 | Labyrinth | 200928T002740Z | Bubbler |
| 020 | MAWP | 200812T064045Z | Razetime |
| 004 | Keg | 191230T123624Z | user8505 |
| 012 | Flurry | 200812T053540Z | Bubbler |
| 2125 | Binary Lambda Calculus | 200126T205035Z | Itai Bar |
| 003 | GolfScript | 200126T095112Z | LCU00 |
| 070 | naz | 200126T070737Z | sporebal |
| 002 | W | 191230T122941Z | user8505 |
| 006 | Triangular | 190917T180658Z | squid |
| 013 | Zsh | 190327T113001Z | GammaFun |
| 002 | Gaia | 190327T140629Z | Giuseppe |
| 002 | 05AB1E | 190327T103515Z | Kevin Cr |
| nan | Sinclair ZX81/Timex TS1000/1500 BASIC ~19 tokenized BASIC bytes | 180104T100724Z | Shaun Be |
| 005 | Gol><> | 180415T061948Z | Bubbler |
| 010 | JavaScript Node.js | 180413T085859Z | Muhammad |
| 009 | Java 8 | 180413T091322Z | Kevin Cr |
| 006 | ><> | 180412T211514Z | SE - sto |
| 041 | Whitespace | 180413T084655Z | Kevin Cr |
| 008 | QBIC | 170502T192543Z | steenber |
| 014 | tinylisp | 180117T235210Z | DLosc |
| 4936 | SNOBOL4 CSNOBOL4 | 171215T184044Z | Giuseppe |
| 009 | GCC command line | 171220T052931Z | l4m2 |
| 010 | Julia 0.6 | 180117T173516Z | gggg |
| 2018 | Chip | 170509T225649Z | Phlarx |
| 017 | PowerShell | 170502T165819Z | colsw |
| 013 | Excel VBA | 170614T202419Z | Taylor R |
| 008 | Aceto | 170509T235021Z | L3viatha |
| 016 | R | 170614T182329Z | Shayne03 |
| 026 | JS ES5 | 170510T123014Z | DN00B |
| 021 | Python 21 Bytes | 170510T041247Z | tgabb |
| 043 | Bash | 180104T080638Z | NTCG |
| 009 | Clean | 180104T103954Z | Οurous |
| 011 | Ruby | 180104T101145Z | G B |
| 007 | Implicit | 171226T223854Z | MD XF |
| 006 | J | 171227T094733Z | Bolce Bu |
| 009 | Google Sheets | 171227T000107Z | Taylor R |
| 005 | x86 opcode | 171218T204427Z | l4m2 |
| 004 | Pushy | 171217T121816Z | FlipTack |
| 006 | Befunge | 171218T150010Z | Jo King |
| 027 | Rust | 171218T144246Z | Håv |
| 011 | Symbolic Python | 171217T123206Z | FlipTack |
| 008 | Braingolf | 170503T145429Z | Mayube |
| 008 | Element | 170805T191846Z | aAaa aAa |
| 002 | 170805T170820Z | totallyh | |
| 002 | Husk | 170801T200220Z | ბიმო |
| 013 | R | 170502T175333Z | Giuseppe |
| 007 | Groovy | 170801T211906Z | Magic Oc |
| 021 | TSQL | 170614T183104Z | BradC |
| 012 | Julia | 170510T010227Z | eaglgene |
| 017 | Excel VBA | 170506T115447Z | Stupid_I |
| 013 | Ruby | 170504T165618Z | anna328p |
| 010 | Clojure | 170504T135418Z | gacelita |
| 013 | R | 170504T104447Z | Sven Hoh |
| 303 | Arnold C | 170504T071934Z | egonr |
| 009 | x8664 Assembly | 170503T154125Z | Cody Gra |
| 009 | Groovy | 170503T235554Z | Johannes |
| 004 | Charcoal | 170503T222306Z | ASCII-on |
| 010 | BrainFlak | 170502T164000Z | DJMcMayh |
| 025 | Swift | 170503T210431Z | Caleb Kl |
| nan | Perl 5 | 170502T182116Z | CraigR88 |
| 517 | Taxi | 170502T201934Z | Engineer |
| 012 | Clojure | 170502T173526Z | mark |
| 014 | Starry | 170502T231020Z | Luis Men |
| 011 | C# | 170503T092530Z | TheLetha |
| 020 | SpecBAS | 170503T093046Z | Brian |
| 010 | Java 8 | 170502T165739Z | Linnea G |
| 010 | Excel | 170503T084444Z | pajonk |
| 019 | JavaScript | 170503T064924Z | Nurrl |
| 022 | C++ | 170503T062244Z | jdt |
| 042 | Silberjoder | 170503T041541Z | quintopi |
| 010 | ClojureScript | 170503T032954Z | madstap |
| 005 | CJam | 170502T214612Z | Esolangi |
| 002 | Japt | 170503T015347Z | Oliver |
| 005 | Befunge98 | 170503T004939Z | MercyBea |
| 040 | Beam | 170503T000215Z | MickyT |
| 012 | Ruby | 170502T222543Z | Value In |
| 029 | Java | 170502T212604Z | Khaled.K |
| 062 | Perl 5 | 170502T210956Z | Chris |
| 002 | k | 170502T203010Z | zgrep |
| 006 | Ohm | 170502T170927Z | Linnea G |
| 005 | Haskell | 170502T194703Z | nimi |
| 038 | Whirl | 170502T194505Z | Engineer |
| 010 | Excel | 170502T194449Z | qoou |
| 014 | BrainFlak | 170502T190944Z | MegaTom |
| 012 | BinaryEncoded Golfical | 170502T173455Z | SuperJed |
| 007 | 05AB1E | 170502T185522Z | osuka_ |
| 317 | Universal lambda | 170502T184016Z | jimmy230 |
| 007 | dc | 170502T182853Z | Digital |
| 006 | Cubix | 170502T174838Z | ETHprodu |
| 012 | MarioLANG | 170502T175742Z | Business |
| 002 | Japt | 170502T162923Z | ETHprodu |
| 004 | braingasm | 170502T174555Z | daniero |
| 002 | Jelly | 170502T173759Z | user6213 |
| 011 | dc | 170502T173217Z | daniero |
| 011 | PHP | 170502T172155Z | Jör |
| 015 | Python | 170502T171617Z | daniero |
| 003 | APL Dyalog | 170502T164058Z | user4180 |
| 004 | Microscript II | 170502T171419Z | SuperJed |
| 010 | AWK | 170502T171233Z | Robert B |
| 002 | J | 170502T171221Z | Adá |
| 011 | /// | 170502T170806Z | sporkl |
| 003 | Math++ | 170502T170742Z | SuperJed |
| 076 | Hexagony | 170502T165703Z | FryAmThe |
| 017 | Batch | 170502T165745Z | Neil |
| 008 | JavaScript ES6 | 170502T165302Z | Shaggy |
| 013 | C gcc | 170502T164100Z | Steadybo |
| 003 | Brachylog | 170502T164856Z | Fatalize |
| 005 | CJam | 170502T164808Z | Luis Men |
| 004 | V | 170502T164730Z | DJMcMayh |
| 005 | V | 170502T163641Z | user4180 |
| 003 | MATL | 170502T164532Z | Luis Men |
| 008 | Mathematica | 170502T164248Z | user6198 |
| 002 | Oasis | 170502T164317Z | Adnan |
| 007 | TIBASIC | 170502T164250Z | Scott Mi |
| 007 | JavaScript ES6 | 170502T164206Z | Arnauld |
| 002 | Jelly | 170502T164200Z | user4854 |
| 027 | C | 170502T164109Z | cleblanc |
| 004 | Retina | 170502T163730Z | Martin E |
| 007 | Alice | 170502T163542Z | Martin E |
| 002 | Pyth | 170502T163459Z | user4854 |
SAKO, 30 bytes
PODPROGRAM:D(N)
D()=N+0*N
WROC
Uses a specification in SAKO that 0*0 = 1.
Full programme version, 41 bytes
1)CZYTAJ:N
DRUKUJ(9,0):N+0*N
STOP1
KONIEC
BrainChild ASM, 6 bytes
asm function f(int)->int{
xpopa
cpyab
notb
addba
xpusha
ret
}
Adds the top of the X Stack to the logical not of it, which would be 1 for 0 and 0 for anything else.
Hex-dump of bytecode
0 1 2 3 4 5 6 7 8 9 A B C D E F
00000000: 34 04 26 1C 2E 14 | 4.&...
BrainChild, 11 bytes
int s=>s||1
Piet + ascii-piet, 6 bytes (1×6=6 codels)
msvcas
Surprisingly simple:
- Read number
- Duplicate
- Not
- Add
- Print number
Does not terminate, a terminating version would be msvcaS ss for 6 additional bytes and codels.
Tcl, 24 bytes
puts [expr max($argv,1)]
Alternative approach, same size:
Tcl, 24 bytes
puts [expr $argv^!$argv]
Bespoke, 49 bytes
doing a no-zero question
a solution:flip,adding N
Adds the inputted number to the result of doing STACKTOP F on it. STACKTOP F results in 1 if the number is zero, and 0 otherwise, essentially "flipping" it.
32-bit Arm32 assembly; 12 bytes, 3 instructions
f:
cmp r0, 0 // 0xE3500000
add.eq r0, r0, 1 // 0x02800001
bx lr // 0xE12FFF1E
Each instruction is 4 bytes long. Like my other answer, it works like as if it were extern int f(int);, and handles 32-bit integers.
Thumb-2 assembly; 8 bytes, 4 insns
f:
cmp r0, 0 // 0x2800
it eq // 0xbf08
adds r0, r0, 1 // 0x1c40
bx lr // 0x4770
Each instruction is 2 bytes long. It follows a normal calling convention and can be called from regular C code as if it were extern int f(int);
Since Thumb doesn't have predicated instruction encodings inside individual instructions, it instead relies on the if-then it instruction.
This code can handle any 32-bit integer.
CASIO BASIC (CASIO fx-9750GIII), 8 bytes
?→A
A+Not A
Easyfuck, 5 bytes
Solution:
+$":'
Explanation:
+ increments the cell at the pointer
$ copies cell at the pointer into storage
" takes in an 8 bit integer
: puts max(current cell, storage) into the current cell
' outputs an 8 bit integer
BitCycle -u, 8 7 4 bytes
-1 byte thanks to Jo King
?/1!
How it works
The -u flag converts decimal inputs to unary and unary outputs to decimal. Inputs emerge from the source ? moving east.
- For positive numbers, the splitter
/sends one bit northward, off the playfield, while the other bits continue straight through. The1bit that started on the playfield, plus all but one of the input bits, land in the sink!, which therefore outputs the same number that was input (or more specifically,n-1+1). - For zero, there are no input bits, the splitter does nothing, and the
1bit that started on the playfield lands in the sink and outputs1.
Swift, 19 18 17 bytes
var f={max(1,$0)}
Taking advantage of the "non-negative" rule here.
Desmos, 10 bytes
Improvement suggested by Aiden Chow.
f(x)=0^x+x
Previous 13-byte solution:
f(x)=max(1,x)
Try it here
Gema, 5 characters
\A0=1
Sample run:
bash-5.2$ gema '\A0=1' <<< '0'
1
bash-5.2$ gema '\A0=1' <<< '10'
10
Python 3, 21 bytes
lambda n:{n:n,0:1}[n]
Also note that {0:1,n:n} doesn't work because if n is 0 the n:n overrides 0:1
Commodore BASIC (CBM/PET, VIC-20, Commodore C64, Commodore C16/+4) 19 BASIC tokens, 17 PETSCII characters with keyword abbreviations
0INPUTN:?N+-(N=.)
Very simple, in Commodore [early Microsoft] BASIC, if you do like PRINT X=Y it will return -1 if true, or 0 if false. A single equals is used for comparison and assignment, and the context is picked up by the interpreter.
We ask for a number with the INPUT keyword, which will enter a numeric value into the variable N. So we will then PRINT N plus a negative of the returned value of N=. with the . being a more performant replacement for 0 in this instance (a very trivial optimisation for such a short program). When N is 0, it adds --1, which just adds 1. When it is not zero, then we add -0 which is zero.
RegEx, 3 Bytes
Match pattern (2 Bytes):
^0
Substitution pattern (1 Byte):
1
Takes input as string without any leading zeros and substitutes it for 1 if it is 0.
Thunno 2, 2 bytes
1|
Attempt This Online! Logical OR with 1.
Alternative:
1§
Attempt This Online! Maximum of input and 1.
Aussie++, 60 bytes
THE HARD YAKKA FOR f IS(x)<YA RECKON x ==0?<BAIL 1;>BAIL x;>
All whitespace is mandatory. Tested in commit 9522366, where all numbers are truthy (see this issue), so I can't just say BAIL x ||1;.
Vyxal, 3 bytes
¬[1
Explanation:
# implict input
¬ # logical not
[1 # 1 if truthy else the original input
# implict output
APOL, 8 bytes
¿(⧣ ⋔ 1)
Explanation
¿( Returning if statement
⧣ Integer input (condition, 0 evaluates to false)
⋔ If item (returns the value of the condition, executed if true)
1 A literal 1 (executed if false)
)
Implicit print
Forte, 25 bytes
9LET0=1:INPUT2:PRINT2:END
Explanation
Clearly, the best way to approach this challenge is to define 0 to be 1. Problem solved.
Seriously. That's exactly what this program does.
9 Line number (doesn't matter as long as it's not 0 or 2)
LET 0=1 Redefine 0 to be 1; all future occurrences of 0 will evaluate as 1
INPUT 2 Redefine 2 to be a number read from input; all future occurrences of 2 will
evaluate as the input number, though of course if that number was 0 it
actually evaluates as 1
PRINT 2 Print that number
END End the program
Acc!!, 75 72 41 bytes
N
_+48/_
Count i while _/48 {
Write _
N
}
Explanation
N reads a character and stores its ASCII code into the accumulator.
Since the first character will always be a digit, the accumulator's value is between 48 and 57. If the accumulator is exactly 48, 48/_ will be 1; otherwise, it will be 0. We add this value to the accumulator. In effect: if the first digit of the number was 0, we consider it as 1; otherwise, we leave it unchanged.
We then enter a loop that writes the accumulator out as a character, reads another character into the accumulator, and loops until the ASCII code we read is less than 48 (probably 10 for newline). Then we halt.
C (clang), 63 42 bytes
main(i){scanf("%i",&i);printf("%i",i?:1);}
Much readable than C answers that already exists here. An if-else statement whether to output 1 or the other.
Thanks to ceilingcat for golfing 21 bytes.
Lolwho.Cares, 62 bytes
*2*+21*1*+21010,**>*2+00210`v
0 ^02210012+*1*< ^<0210002<<
321
Old version:
*2*+210*1*+210101,**>*2+00210`v
0 ^ 102210012+*1*< ^<0210002<<
253
The last line isn't code; it's the input number.
Online interpreter: https://valthe.scheffers.net/testing/lolwhocares
Swift, 12 bytes
Types provided, 12 bytes
{$0==0?1:$0}
{
$0 == 0 ? 1 : $0
}
Types not provided, 20 bytes
{(n:Int)in n==0?1:n}
```swift
{ (number: Int) -> Int in
number == 0 ? 1 : number
}
BRASCA, 14 7 bytes
ig:0=+n
-7 bytes by RezNesX
Explanation
<implicit input> - Push STDIN to stack
ig - Convert numbers from ASCII to number, and concatenate the stack
:0= - If the number is zero (pushes 1)
+n - Add and print as number.
```
Pxem, 12 bytes (content).
Unprintable characters are escaped.
.c\001.y.s\001.d.a
Usage
- Expects stack has only an item, as an argument.
- Stack content: (n, xxx) to (n', xxx, n, xxx).
Usage example
Filename to be _.e.n, with your input from stdin.
MMIX, 8 bytes (2 instrs)
(jxd)
00000000: 6300 0001 f801 0000 c¡¡¢ẏ¢¡¡
(assembly)
foo CSZ $0,$0,1
POP 1,0
Honestly, this function really should be inlined. It costs three instructions to call, but just one to inline.
AWK, 8 bytes
$0||$0=1
Slightly different approach to this answer, reducing 2 bytes.
$0||$0=1 Basically, an OR logic gate. Right part is not considered if the left one is true.
$0 is the input, parsed as true if different from zero, printing the input.
If input is zero, $0 is set to 1 (right part).
As $0 becomes 1, it is parsed true, and then the input is printed.
Vyxal, 2 bytes
¬+
Straightforward answer:
¬ # nots the input (x == 0 ? 1 : 0)
+ # adds it to the input
Sed, 7 bytes
Input in any number base, without any leading zeros. (It doesn't even need to be a consistent base!)
s/^0/1/
0 is the only number that begins with 0; just change that to 1.
Javascript, 8 11 bytes
Probably a bad implementation, but here:
n=>n==0?1:n
Explanation:
n // The input
n == 0 // Checks whether n is zero
?1 // If so, return 1.
:n // However, if n isn't zero, then return n
convey, 6 bytes
{+1
}.
Outputs the max of the input and 1. I think this is the optimal answer, since to use any binary function, there must be a second line with at least two characters.
You can also do the more traditional x+!x for 8 bytes.
ARM Thumb-2 machine code, 6 bytes
Machine code:
b900 3001 4770
Assembly:
.syntax unified
.arch armv6t2
.thumb
.globl no_zero
.thumb_func
no_zero:
// skip next insn if r0 != 0
cbnz r0, .Ldont_add_1
.Ladd_1:
// increment r0
adds r0, #1
.Ldont_add_1:
// Return
bx lr
It doesn't get much simpler than that.
Input is in r0, output is in r0, follows standard calling convention.
ARM Thumb-2 machine code, branch/IT-less, 8 bytes
Only works for non-negative numbers unlike the branching solution.
Machine code:
4241 f140 0000 4770
Assembly:
.syntax unified
.arch armv6t2
.thumb
.globl no_zero_branchless
.thumb_func
no_zero_branchless:
// Negate r0 into r1. We only care about
// setting the flags here.
negs r1, r0
// Add the carry flag to r0. (wide insn)
adc r0, r0, #0
// Return
bx lr
Follows the same calling convention.
negs follows the same rules as subs for setting flags. So, if r0 was positive, the result would have a borrow, but if it was 0, it would not.
Then, we just add with carry against zero to add 1 if r0 was zero, or 0 if r0 was positive.
Yes, you read that correctly.
When subtracting, ARM clears the carry flag to indicate a borrow. It is counterintuitive and usually annoying when golfing.
But I decided to use this for a small challenge.
Unfortunately, adc doesn't have a narrow immediate form, so we either have to waste another register to set it to zero, or use a wide instruction.
MathGolf, 2 bytes
┌+
Explanation:
┌ # Inverted boolean the (implicit) input-integer (1 if 0; 0 for any other integer)
+ # Add this to the (implicit) input-integer
# (after which the entire stack joined together is output implicitly as result)
Labyrinth, 6 bytes
?"1
@!
? Take number input and push it
" No-op, but a 3-way junction:
If top is 0, go straight
1 Convert the 0 to 1 and bounce off the dead end
" Enter the junction again, turn right (reflected to south)
!@ Pop and print as number, turn around the corner and halt
Otherwise, top is positive; turn right
!@ Pop and print as number and halt
Keg, -hr 4 bytes
:0=+
Explanation
: # Take 2 inputs
0= # Check whether the input is 0
# (Yields 1 if the input is 0)
# (Yields 0 otherwise)
+ # Add the input with this result and print it rawly
# i.e. 0->1, 5->5
Flurry, 12 bytes
({})[(){}]{}
Run example
$ ./flurry -nin -c "({})[(){}]{}" 0
1
$ ./flurry -nin -c "({})[(){}]{}" 1
1
$ ./flurry -nin -c "({})[(){}]{}" 2
2
$ ./flurry -nin -c "({})[(){}]{}" 10
10
For single input n, computes n (K n) 1: apply "a constant function that returns n regardless of argument" n times to the number 1.
({}) Pop n and push n
[(){}] Apply (K n); n is popped again from the stack
{} Apply I (equivalent to number 1); popping from empty stack gives I
Reduction into SKI terms don't help much:
n (K n) 1
= I n (K n) 1
= S I K n 1
which translates into <>{{}}(){}{}
Binary Lambda Calculus, 2.125 Bytes
00010110 00110001 0
Translation into lambda calculus:
\n. n (\x. n) (\x. x)
naz, 70 bytes
2a2x1v4a8m2x2v1x1f1r3x2v2e1o3f0x1x2f1a1o0x1x3f1r3x1v4e1o3f0x1x4f0a0x1f
Works for any input integer, provided it's passed as a file terminated with the control character STX (U+0002).
Explanation (with 0x commands removed)
2a2x1v # Set variable 1 equal to 2
4a8m2x2v # Set variable 2 equal to 48 ("0")
1x1f1r3x2v2e1o3f # Function 1
# Read a byte of input
# Jump to function 2 if it equals variable 2
# Otherwise, output it and jump to function 3
1x2f1a1o # Function 2
# Add 1 to the register and output
1x3f1r3x1v4e1o3f # Function 3
# Read a byte of input
# Jump to function 4 if it equals variable 1
# Otherwise, output it and jump back to the start of function 3
1x4f0a # Function 4
# Add 0 to the register
1f # Call function 1
W, 2 bytes
1|
Explanation
a # Take an input
1| # Logical or with 1
# If n is 0, this evaluates to 1
# It evaluates to 0 otherwise
Triangular, 6 bytes
$,w%1<
Ungolfed:
$
, w
% 1 <
-----------------------
$ Read from input as an integer
w, Change directions if ToS != 0 (will go straight to print)
<1 Push 1
% Print ToS as an integer
Zsh, 13 bytes
<<<$[$1?$1:1]
Bash, 15 bytes
echo $[$1?$1:1]
Dash (or other POSIX-compliant shell), 17 bytes
echo $(($1?$1:1))
05AB1E, 2 bytes
_+
Try it online or verify some more test cases.
2 bytes alternative by @Adnan:
$M
Try it online or verify some more test cases.
Explanation:
_ # Check if the (implicit) input is 0 (0 becomes 1; everything else becomes 0)
+ # Add it to the (implicit) input (0 becomes 1; everything else stays the same)
# (and output the result implicitly)
$ # Push both 1 and the input to the stack
M # Push the largest number of the stack (without changing the rest of the stack)
# (and output the top of the stack implicitly as result)
Sinclair ZX81/Timex TS1000/1500 BASIC ~19 tokenized BASIC bytes
1 INPUT A
2 PRINT A+NOT A
This is a more efficient solution than the one below; essentially it takes the NOT value of A (which would be zero in all cases but zero) and adds that to the value of A.
Old answer:
Sinclair ZX81/Timex TS1000/1500 BASIC, ~31 tokenized BASIC bytes
1 INPUT A
2 IF NOT A THEN LET A=NOT A
3 PRINT A
This takes a numeric input from the user and will display 1 if zero is entered by making A NOT A; otherwise it does nothing and displays the numeric value.
Gol><>, 5 bytes
I:z+h
Given n, calculate n + !n, print as int and halt. Unfortunately Gol><> doesn't have implicit input option, so the bytes are the same as regular ><>.
JavaScript (Node.js), 10 bytes
n=>n&&n||1
here is one more
JavaScript (Node.js), 8 bytes
n=>n?n:1
and lastly
JavaScript (Node.js), 7 bytes
n=>n||1
Java 8, 9 bytes
n->n|1>>n
C# .NET, 9 bytes
n=>n|1>>n
Different approach (and 1 byte shorter) than the existing Java/C# .NET answers. (Also works with negative inputs.)
Explanation:
i-> // Method with integer as both parameter and return-type
i| // Return the input bit-wise OR-ed with:
1>>i // 1 bit-wise right-shifted by the input
1>>i will be 1 when the input is 0, and 0 for every other input.
0|1 for input 0 will therefore result in 1, and n|0 for every other input will therefore result in n.
><>, 6 bytes
:?!1n;
Could have been 1 byte shorter if ? had the opposite conditional behaviour.
:? check if nonzero, then either
! a) skip the next instruction or
1 b) push 1 to the stack
n print
; terminate
Whitespace, 41 bytes
[S S S N
_Push_0][S N
S _Duplicate][T N
T T _Read_STDIN_as_integer][T T T _Retrieve][S N
S _Duplicate_input][N
T S N
_If_0_jump_to_Label_0][T N
S T _Print_as_integer][N
N
N
_Exit][N
S S N
_Create_Label_0][S S S T N
_Push_1][T N
S T Print_as_integer]
Letters S (space), T (tab), and N (new-line) added as highlighting only.
[..._some_action] added as explanation only.
Explanation in pseudo-code:
Integer i = STDIN as integer
If i == 0:
Call function Label_0
Print i
Exit program
function Label_0:
Print 1
Exit implicitly with error: Exit not defined
Example runs:
Input: 0
Command Explanation Stack Heap STDIN STDOUT STDERR
SSSN Push 0 [0]
SNS Duplicate top (0) [0,0]
TNTT Read STDIN as integer [0] {0:0} 0
TTT Retrieve [0] {0:0}
SNS Duplicate top (0) [0,0] {0:0}
NTSN If 0: Jump to Label_0 [0] {0:0}
NSSN Create Label_0 [0] {0:0}
SSSTN Push 1 [0,1] {0:0}
TNST Print as integer [0] {0:0} 1
error
Try it online (with raw spaces, tabs and new-lines only).
Stops with error: Exit not defined.
Example runs:
Input: 5
Command Explanation Stack Heap STDIN STDOUT STDERR
SSSN Push 0 [0]
SNS Duplicate top (0) [0,0]
TNTT Read STDIN as integer [0] {0:5} 5
TTT Retrieve [5] {0:5}
SNS Duplicate top (5) [5,5] {0:5}
NTSN If 0: Jump to Label_0 [5] {0:5}
TNST Print as integer [] {0:5} 5
NNN Exit program [] {0:5}
Try it online (with raw spaces, tabs and new-lines only).
QBIC, 11 8 bytes
?:-(a>1)
Thanks to @l4m2 for saving me some bytes!
Explanation:
? PRINT
: an integer taken from the cmd line (and store it as 'a')
- minus
(a<1) -1 if 'a' is less than 1 (can only be 0) or 0 otherwise.
This leaves any a to be a, but turns zeroes into 1 by double negative.
Old code, that didn't use the inline : yet:
:?(a>0)+a+1
Explanation
: Get an int from the cmd line, a
? PRINT
(a>0) if a is greater than 0, this is -1, else 0
+a yields 0 for 0 and a-1 for >0
+1 makes 1's for 0 and a's for all other values
tinylisp, 14 bytes
(q((n)(i n n 1
This is a lambda function. In order to be able to call it, you either need to give it a name using d or call it directly (which would require explicitly closing the parentheses before specifying the argument). Try it online!
The function takes one argument, n. If n is truthy (all positive integers), return n. Otherwise (zero), return 1.
GCC command line, 9 bytes
-Df(x)x?:
-D in GCC checks if the following content has an =; if not, it add the code
#define <followed_content> 1
which in this case turns into
#define f(x)x?: 1
Chip, 20 18 bytes
eaABb
*`\\-!
fcCDd
How?
aABb
Copy the low 4 bits from input to output
cCDd
e
* Set the higher bits of output, so that the values are ASCII digits
f
eaABb
* Replicate any ASCII digits on input to output
fcCDd
-
! Produce a high signal, but only during the first byte
-
aAB
`\\-* Set the lowest bit of output, if the four low bits of input are unset
CD
aAB Set the lowest bit of output, if the four low bits
`\\-! of input are unset, and only on the first byte
CD
PowerShell, 24 22 17 Bytes
blatantly stolen from here
!($a=$args[0])+$a
Explanation
Invert the value, returning 0 for non-0 numbers, and 1 for 0, then add the intitial to it.
this makes it basically 1/0 + value, so for 0 the first value is 1, any other numbers it's 0.
examples:
# !0+0 = 1
# !1+1 = 1
# !9+9 = 9
Excel VBA, 17 13 Bytes
Anonymous VBE Immediate function that takes input as expected type unsigned integer and then outputs to the VBE immediate window
?[Max(A1,1)]
Previous Version
?[If(A1,A1,1)]
Aceto, 9 8 bytes
rid0=`1p
read an integer and duplicate it, then push 0. Are they =? Then (`) push a 1. print the top element.
R 20 16 bytes
pryr::f(n+(n<1))
JS ES5, 26 bytes
function (n){return n?n:1}
Python 21 Bytes
int(max('1',input()))
Takes input from REPL environment
Bash, 43 bytes
if [ $1 -eq 0 ];then echo 1;else echo $1;fi
Ruby, 11 bytes
->n{n|1>>n}
Implicit, 7 bytes
$!{.
Try it online! Explanation:
$ read input
!{ if falsy
. increment
implicit output
J, 6 Bytes
(>.1:)
Standard solution: Return the max of 1 and the argument.
The parenthesis ensure it's evaluated as a monadic hook:
(>.1:) 0
0 >. (1: 0) NB. Definition of a monadic hook.
0 >. 1 NB. 1: is a constant function, always returns 1.
1 NB. >. returns the max of its two arguments.
Google Sheets, 9 Bytes
Anonymous worksheet function that takes input from range A1 and outputs to the calling cell
=Max(1,A1
x86 opcode, 6 5 bytes
41 INC ECX
entry: E3 FD JECXZ SHORT $-1
91 XCHG EAX,ECX
C3 RETN
ECX -> EAX
Befunge, 6 Bytes
&:!+.@
& Gets input as number
:! Duplicates and inverts
+.@ Adds the inverted input to the original, prints and ends the program
Rust, 27 bytes
|x:u64|if x==0{1}else{x};
Anonymous function, or lambda, taking input from x.
First time golfing with Rust, and i must say im quite impressed.
Braingolf, 8 bytes
!?_:1_;
Explanation:
? If last element on stack > 0
! Prevent if check from consuming last element on stack
_ Pop last element on stack and print
: Else
1 Add int literal 1 to end of stack
_ Pop last element on stack and print
; Prevent automatic pop of last element on stack
,,,, 2 bytes
1∨
,,, is on TIO now, so that's cool. Computes input or 1 (logical OR) and implicitly outputs the result.
Husk, 3 2 bytes
|1
Ungolfed/Explanation
-- implicit input N
| -- if N is truthy: N
1 -- else (N==0): 1
Thanks @Zgarb for -1 byte!
R, 13 bytes
max(1,scan())
reads n from stdin. With pmax, it can read in a list and return the appropriate value for each element in the list for +1 byte.
I should note that there is another fine R solution in 13 bytes by Sven Hohenstein which allows for yet another 13 byte solution of
(n=scan())+!n
which makes me wonder if that's the lower limit for R.
Groovy, 7 bytes
{it?:1}
Elvis operator; if true, return self, else return 1. Only false integer value auto-unboxxed to false in Groovy is 0. Thusly, exactly the spec.
T-SQL, 21 bytes
SELECT MAX(1,a)FROM t
SQL input is allowed via a pre-existing named table (table t with INT field a).
Julia, 13 12 bytes
f(n)=n<1?1:n
Unfortunately, Julia doesn't do implicit casting from int to bool, so I have to burn an entire 3 characters just to do a comparison to zero. Saved one byte by safely assuming the number isn't negative. Still too verbose for my taste, though.
Excel VBA 17 bytes
[a1]=IIF(n=0,1,n)
Simple if else statement you can run it from vba immediate window
Ruby, 13 bytes
->i{i==0?1:i}
Clojure, 10 bytes
#(max 1%)
Not much to explain.
R, 13 bytes
n=scan();n+!n
Here, scan is used to read the input value n. The negation of n (i.e., !n, 0 or 1) is added to n.
Arnold C, 303 bytes
IT'S SHOWTIME
HEY CHRISTMAS TREE i
YOU SET US UP 1
GET YOUR ASS TO MARS i
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
BECAUSE I'M GOING TO SAY PLEASE i
TALK TO THE HAND i
BULLSHIT
TALK TO THE HAND 1
YOU HAVE NO RESPECT FOR LOGIC
YOU HAVE BEEN TERMINATED
Trying to explain it:
IT'S SHOWTIME //main()
HEY CHRISTMAS TREE i //int i
YOU SET US UP 1 //i = 1
GET YOUR ASS TO MARS i // ? compiler told me to add that
DO IT NOW // ? compiler told me to add that
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY // something related to reading from stdin
BECAUSE I'M GOING TO SAY PLEASE i // if(i)
TALK TO THE HAND i //print i
BULLSHIT //else
TALK TO THE HAND 1 //print 1
YOU HAVE NO RESPECT FOR LOGIC //endif
YOU HAVE BEEN TERMINATED //end main()
It even beats this answer!
x86-64 Assembly, 10 9 bytes
Following the standard System V AMD64 calling convention, this function accepts a 32-bit unsigned integer parameter via the EDI register, and returns the result via the EAX register:
89 F8 | mov eax, edi ; move parameter from EDI to EAX
85 C0 | test eax, eax ; test input and set flags
75 01 | jnz Finished ; if input is non-zero, then jump to end
FF 40 | inc eax ; otherwise, input is 0, so increment it to 1
| Finished:
C3 | ret ; return, leaving result in EAX
x86-32 Assembly, 8 bytes
We could also write this as 32-bit code, since we're only dealing with 32-bit values.
The nice thing about this is that the INC instruction becomes only 1 byte in length. (In 32-bit mode, inc eax can be encoded simply as 40. In 64-bit mode, 40 is interpreted as a REX prefix, so the leading FF is needed. See Intel's documentation for this instruction.)
The caveat is that most 32-bit calling conventions pass parameters on the stack, rather than in registers, and loading a value from the stack (memory) takes many more bytes. If we can be allowed a __fastcall-style calling convention that passes parameters in registers (supported by virtually all C compilers, so not really cheating, just a bit less standard), then the integer parameter is passed in ECX and we get the following code, for a total of 8 bytes:
89 C8 | mov eax, ecx
85 C0 | test eax, eax
75 01 | jnz Finished
40 | inc eax
| Finished:
C3 | ret
Groovy, 9 bytes
f={it?:1}
Inside a groovy script file you can run with "groovy -D n=0 ":
println System.properties.n?:1
As a closure:
f={it?:1}
1) "?:" is groovy's "elvis-operator"
One instance of where this is handy is for returning a 'sensible default' value if an expression resolves to false-ish
2) "it" refers to a single anonymous parameter the closure is called with
3) 0 evaluates to false in groovy-truth
Charcoal, 4 bytes
I∨N¹
Explanation
I Cast (number is casted to string)
∨N¹ input number logical-or 1
Brain-Flak, 22, 10 bytes
({{}}[]{})
Explanation:
If the input is non-zero, then {{}} will pop everything off the stack and evaluate to the input. If it is zero, nothing will be popped, and it will evaluate to zero. So running ({{}}) gives
Non-zero:
n
Zero:
0
0
At this point, we'll add the height of the stack (0 for non-zero, 1 for zero) and pop one more value off the stack. (since the stack is padded with an infinite number of 0's, this will pop either the top 0 or an extra 0)
Swift, 25 bytes
max(1, Int(readLine()!)!)
If you just have the body of a closure, then it is just 10 bytes:
max(1, $0)
Takes in standard input and then returns the max int, which would either be 1 or higher.
Perl 5, 10 +1 byte for -p flag = 11 bytes
$_=$_*1||1
Run with -p flag.
Taxi, 517 bytes
Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 l 1 r.Pickup a passenger going to Cyclone.Go to Cyclone:n 1 l 1 l 2 r.Pickup a passenger going to Addition Alley.Pickup a passenger going to Knots Landing.Go to Knots Landing:n 2 r 2 r 1 l.Pickup a passenger going to Addition Alley.Go to Addition Alley:w 1 r 1 l 2 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:n 1 r 1 r.Pickup a passenger going to Post Office.Go to Post Office:n 1 l 1 r.
Formatted for humans:
Go to Post Office:w 1 l 1 r 1 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:s 1 l 1 r.
Pickup a passenger going to Cyclone.
Go to Cyclone:n 1 l 1 l 2 r.
Pickup a passenger going to Addition Alley.
Pickup a passenger going to Knots Landing.
Go to Knots Landing:n 2 r 2 r 1 l.
Pickup a passenger going to Addition Alley.
Go to Addition Alley:w 1 r 1 l 2 l.
Pickup a passenger going to The Babelfishery.
Go to The Babelfishery:n 1 r 1 r.
Pickup a passenger going to Post Office.
Go to Post Office:n 1 l 1 r.
The key location is Knots Landing which performs the NOT operation so 0 returns 1 and all other numbers return 0. Now, you can go to Addition Alley to add the result from Knots Landing to the original input.
Clojure, 12 bytes
#(get[1]% %)
The get function takes an associative data structure, a key, and a default value. Vectors are associative using sequential position as the key. So, [1] is a vector with the value 1 at position 0. If get is called with parameter 0 it will return 1, otherwise no other keys exist in the vector so it returns the default value of the parameter.
Starry, 14 bytes
, +' +*`.
Explanation
Space shown as _ .
, Read integer and push to stack
_+ Duplicate
' If non-zero jump to branch label
______+ Push 1
* Add
` Mark branch label
. Print as a number
C#, 11 bytes
n=>n<1?1:n;
This compiles to a Func<int, int>.
SpecBAS - 20 bytes
1 INPUT n: ?n OR n=0
? is shorthand for PRINT
Java 8, 10 bytes
i->i<1?1:i
- Thanks to @LeakyNun for saving -1 byte
- Didn't notice it's a non-negative integer
JavaScript, 19 Bytes
(x)=>{return x?x:1}
Ungolefed, with example:
function f(n) {
if(n)
return n;
else
return 1;
}
f(0); // 1
f(1); // 1
f(2); // 2
Silberjoder, 42 bytes
0+b1,-CB+b1:BC<. +iB-b1+CB-CA[+CA.,-CA]1
How it works:
0
The first three characters are all data and are not executed. They are a newline (10), a zero (48), and a DC3 (19).
+b1
Point b at the "0" character. Note that a is still pointing at the newline character.
,
Read the first character of input.
-CB
Subtract what b is pointing to (the "0") from the first digit.
+b1
Point b at the DC3 character, which has value 19
:BC
Jump to position 19+3=22 if the c is pointing to anything other than zero. This would happen if the first digit of the number was anything other than "0". Otherwise...
<.
Move c on top of the "1" at the end of the program and print it.
+iB
b is still pointing at 19, so we add 19 to the instruction pointer, jumping to the "1" at the end of the program, causing the program to halt after one more cycle. (The extra space is ignored, but we need it there to position this instruction so that the instruction pointer jumps beyond the "]" at the end of the program. If we don't do this, we will enter the loop at the end, and print an extraneous semicolon whenever 0 is input.)
-b1
This is position 22, so we jump here whenever the number didn't start with "0". We move b back to point at the "0".
+CB
Add the 48 back to the first digit of the number, restoring it to its proper character value.
-CA
Subtract the newline from the digit.
[+CA.
If it's not zero, restore it to its original value and print it.
,-CA]
Repeat reading digits, comparing them with newline, and printing them until newline is seen.
1
Data. Ignored. Program halts.
Clojure(Script), 10 bytes
#(max 1 %)
CJam, 5 bytes
ri1e|
Explanation:
ri e# Read token and convert to integer
1e| e# OR with 0
Befunge-98, 5 bytes
&:!+.
Try it Online! (Warning - will take 60 seconds per test because of how TIO treats &)
Explanation
&: Push 2 copies of the input
! Logical not the 1st copy - i.e. push 1 if 0, push 0 otherwise
+ Add them together - this results in +0 normally, but +1 in the case of 0
. Print that value
Loops back around to the first command
& Because there's no more input, the TIO interpreter stalls for a minute and ends.
Befunge-93 Variant, 6 bytes
It's trivial to implement Befunge-93 in a similar way, except we can't use the & to end the program this time. Thus, the code is 1 byte longer, for 6 bytes:
&:!+.@
Beam, 40 bytes
This reads the input as a string as it doesn't have any other option. Basically it checks if the first character is 0 by removing 48 from it. If 0 increment and output as a number, otherwise output it as a number, then read the rest of the input outputting it as characters.
r'''''>`----\
:+n++(------/
/<:<
r@
\u
Ruby, 12 bytes
->n{n<1?1:n}
Alternate version that reads directly from STDIN instead of being a function. Same number of bytes after counting the -p flag (11+1 = 12 bytes). Effectively a port of the Retina solution.
sub /^0/,?1
Perl 5, 6 + 2 bytes for the -l and -p flags
$_||=1
Takes input on separate lines from stdin. Runs with the flags -lp.
Ohm, 11 6 bytes
ö?┼¿1;
Uses CP-437 character encoding. Run with -c flag
Explanation:
ö?┼¿1;
■print( )
┼ ■ first_input()
? ; ■ if( )
ö ■ int(first_input())!=0
¿ ■ else
1 ■ 1
Haskell, 5 bytes
max 1
Usage example: (max 1) 0 -> 1.
Nothing much to explain.
Whirl, 38 bytes
01100011100011110011111100001000111100
Explanation:
01100 op.ccw, op.intio Mem1 = STDIN
011100 math.ccw, math.= If (Mem1 = 0) Then (Math.Val = 1) Else (Math.Val = 0)
0111100 op.cw, op.one Op.Val=1
11111100 math.add Math.Val = Math.Val + Mem1
00 op.one Op.Val=1 (Cheapest way to loop back to the Math wheel)
100 math.store Mem1 = Math.Val
0111100 op.ccw, op.intio STDOUT = Mem1
Excel, 10 Bytes
=A1+(A1=0)
This saves 4 Bytes over the obvious 'IF' statement solution, =IF(A1=0,1,A1).
Binary-Encoded Golfical, 12 bytes
This binary encoding can be converted back to the standard graphical representation using the encoder provided in the Golfical github repo, or run directly using the interpreter by adding the -x flag.
Hexdump of binary encoding:
00 40 02 15 17 14 00 01 23 1D 17 14
Original image:

Magnified 120x, with color labels:

05AB1E, 7 bytes
D0›i,}1
D //push two inputs (implicit)
0› //push input greater than zero
i //if true
,} //print input
1 //push 1. printing is implicit if there is no previous output
Universal lambda, 3 bytes (17 bits)
00010110001100010
It is a function and not a complete program. I think it should work, but didn't actually test it because it doesn't seem easy to do so. It means λx.x(λy.x)(λy.y).
Lazy K, 10 bytes
S(SIK)(KI)
It is a function, untested, too.
dc, 7
?d0r^+p
Relies on the fact that dc evaluates 00 to 1, but 0n to 0 for all other n.
Cubix, 6 bytes
OI!1L@
Somehow managed to fit it on a unit cube... Test it online!
Explanation
Before being run, the code is arranged as a cube net:
O
I ! 1 L
@
The IP (instruction pointer) is then placed on the far-left face (I), facing to the right. The instructions run from there are:
I Input a number from STDIN and push it to the stack.
! If the top number is non-zero, skip the next instruction.
1 Push a 1 (only if the input was zero).
L Turn left. The IP is now on the top face facing the !.
O Output the top item as a number.
The IP then hits ! again, skipping the @ on the bottom face. This is not helpful, as we need to hit the @ to end the program. The IP hits the L again and goes through the middle line in reverse (L1!I) before ending up on the L one more time, which finally turns the IP onto @.
MarioLANG, 12 bytes
;
=[
:<+
=:
How it works
Mario starts in the top left, initially walking right. He reads an int from input (;) and stores it in the current memory cell. Then he falls off the ground (=), hitting [, which makes him ignore the next command if the current cell is 0.
If the cell is not 0, he'll start walking left (<), output the current cell as an int (:), and fall to his death (end of program).
If the cell is 0, he ignores the command to turn left, and keeps walking right. He increments the current cell (+), outputs it, and falls to his death.
Japt, 2 bytes
ª1
Explanation
ª is a shortcut for JS's || operator. Japt has implicit input, so this program calculates input||1, and the result is implicitly sent to STDOUT.
w1 would work as well, taking the maximum of the input and 1.
braingasm, 4 bytes
;z+:
; reads a number from stdin, z+ increments that number if it is 0, : prints.
Jelly, 2 bytes
o1
An alternative to the other Jelly solution. o provides a default value for a zero/empty argument; in this case, we default it to 1.
dc, 11 bytes
[1]sf?d0=fp
[1]sf stores a macro in register f which pushes 1 to the top of the stack,
? reads input,
d0=f runs macro f if input was 0, p prints the top of the stack.
Test:
$ dc -e "[1]sf?d0=fp" <<< 0
1
$ dc -e "[1]sf?d0=fp" <<< 1
1
$ dc -e "[1]sf?d0=fp" <<< 42
42
Python, 15 bytes
lambda n:n or 1
Microscript II, 4 bytes
1sN|
Rough translation:
x=1
push x
x=readnum()
x|=pop()
x is then printed implicitly at the end.
J, 2 bytes
^*
^ [argument] raised to the power of
* the sign of the argument (0 if 0 else 1)
Because 1=0^0 in J.
///, 11 bytes
/#0/1//#//#
Since there is no other way to take input in ///, hard-code the input after the last #.
Works by replacing #0 with 1. Then it removes any remaining #. The # makes sure that an input of 10 would not output 11.
Version that takes input in Itflabtijtslwi:
GGaGGGGbGG/#0/1//#//#ab
Hexagony, 7 6 bytes
)?<@.!
Expanded:
) ?
< @ .
! .
Saved 1 byte thanks to Martin!
If the number is nonzero print it, otherwise add one to it and print that instead.
Batch, 17 bytes
@cmd/cset/a%1+!%1
JavaScript (ES6), 8 bytes
Beaten to the shortest solution (as usual!), so here's an alternative.
n=>n?n:1
Try It
f=
n=>n?n:1
console.log(f(0))
console.log(f(1))
console.log(f(8888))
C (gcc), 14 13 bytes
f(n){n=n?:1;}
Thanks to @betseg for reminding me of the n?:1 trick in the comments of the other C answer!
C, 17 bytes
f(n){return!n+n;}
C, 16 bytes
#define f(n)!n+n
Brachylog, 3 bytes
∅1|
Explanation
If we add the implicit ? (Input) and . (Output), we have:
?∅ Input is empty (that is, [] or "" or 0 or 0.0)
1. Output = 1
| Else
?. Input = Output
CJam, 5 bytes
ri1e>
Explanation
ri e# Read input as an integer
1 e# Push 1
e> e# Maximum. Implictly display
V, 4 bytes
é0À
Abuses an non-preferred but expected behavior, so I can't really call it a bug. Explanation:
In Vim, commands accept a count. For example, <C-a> will increment a number, but 7<C-a> will increment a number by 7. However, you can't use 0 as a count, because
0is already a command (go the first column), andIn the context of a text editor, it rarely makes sense to request that a command be run 0 times.
This is fine for a text editor, but usually obnoxious for a golfing language, so V overwrites some commands so that 0 is a valid count. For example, é, ñ, Ä, and some others. However, since <C-a> is a builtin vim command, it is not overwritten, so running this with a positive input gives:
N " N times:
<C-a> " Increment
But running with 0 as input gives:
0 " Go to column one
<C-a> " Increment
Full explanation:
é0 " Insert a 0
À " Arg1 or 1 times:
<C-a> " Increment
V, 5 bytes
ç^0/<C-a>
Where <C-a> is 0x01.
Explanation
ç " On every line
^0/ " that begins with a zero do:
<C-a> " Increment the number on that line
MATL, 3 bytes
t~+
Explanation
t % Implicit inupt. Duplicate
~ % Logical negation. Converts zero to 1, and nonzero to 0
+ % Add. Implicit display
Mathematica, 9 8 bytes
Per Martin Ender:
#~Max~1&
First idea:
#/. 0->1&
Pure function with replaces 0 with 1. The space is necessary or it thinks we are dividing by .0.
TI-BASIC, 7 bytes
:Prompt X
:X+not(X
Alternatively,
TI-BASIC, 7 bytes
:Prompt X
:max(X,1
JavaScript (ES6), 7 bytes
n=>n||1
Jelly, 2 bytes
+¬
Pretty much exactly my Pyth answer, but it's my first Jelly program.
C, 27 Bytes
f(n){printf("%d\n",n?n:1);}
Retina, 4 bytes
^0
1
If the input starts with a zero, replace that with a 1. (Works because the input is guaranteed to have no leading zeros for non-zero values.)
Alice, 7 bytes
1/s
o@i
Explanation
1 Push 1. Irrelevant.
/ Reflect to SE. Switch to Ordinal.
i Read all input as a string.
Reflect off bottom right corner. Move back NW.
/ Reflect to W. Switch to Cardinal.
1 Push 1.
IP wraps around to last column.
s Sort swap: implicitly convert the input to an integer. Then, if the top stack
element is less than the one below, the two are swapped. It basically computes
min and max of two values at the same time, with max on top.
/ Reflect to NW. Switch to Ordinal.
Immediately reflect off the top boundary. Move SW.
o Implicitly convert the result to a string and print it.
Reflect off bottom left corner. Move back NE.
/ Reflect to S. Switch to Cardinal.
@ Terminate the program.
Pyth, 2 bytes
+!
Explanation
+!
!Q 1 if (implicit) input is 0, 0 otherwise.
+ Q Add the (implicit) input.
