# Stay away from zero

Given a non-negative integer n, output 1 if n is 0, and output the value of n otherwise.

# Input

A non-negative integer.

input output
0      1
1      1
2      2
3      3
4      4
5      5
6      6
7      7

# Scoring

This is , so shortest answer in bytes wins.

Standard loopholes apply.

• You should probably put a link to the TNB CMC, since that's where this challenge came from. – mbomb007 May 3 '17 at 20:53
• Does the answer need to be a full function, or can it be the body? – Caleb Kleveter May 3 '17 at 20:58
• @CalebKleveter The default rule in PPCG is that the answer is either a function or a full program, but not snippets. – Leaky Nun May 4 '17 at 1:56
• Can we print the output with a leading zero? – MD XF Dec 26 '17 at 21:54
• @MDXF yes, you can. – Leaky Nun Dec 26 '17 at 22:30

## C++, 22 bytes

[](int i){return!i+i;}

Try it online

=MAX(A1,1)

# SpecBAS - 20 bytes

1 INPUT n: ?n OR n=0

? is shorthand for PRINT

## C#, 11 bytes

n=>n<1?1:n;

This compiles to a Func<int, int>.

# Starry, 14 bytes

, +'      +*.

Try it online!

### Explanation

Space shown as _ .

,           Read integer and push to stack
_+          Duplicate
______+     Push 1
Mark branch label
.           Print as a number
• My first Starry answer – Luis Mendo May 2 '17 at 23:10

# 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.

# Desmos, 13 bytes

f(x)=max(x,1)

Try it here

• Desmos tips: you can have lists of integers like this. – Leaky Nun May 4 '17 at 1:58

# Clojure, 10 bytes

#(max 1%)

Not much to explain.

• I think you can shorten this to #(max 1%) (9 bytes). tio.run/nexus/… – Dennis May 4 '17 at 14:37

# 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.

# 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.

# Symbolic Python, 11 bytes

_+=_==(_>_)

Try it online!

_+=           # Output = Input +
_==(_>_)                       Input == 0

Anonymous worksheet function that takes input from range A1 and outputs to the calling cell

=Max(1,A1
• This look very cool mate :) – NTCG Jan 4 '18 at 8:07

# Bash, 43 bytes

if [ $1 -eq 0 ];then echo 1;else echo$1;fi

## Python 21 Bytes

int(max('1',input()))

Takes input from REPL environment

# 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.

Try it online!

Try it online!

# BitCycle, 8 7 bytes

-1 byte thanks to Jo King

?v<
!+~

Try it online!

Uses the -U flag to convert decimal inputs to "signed unary," which in this case means unary for positive integers and 0 for zero.

### How it works

Let's run two example inputs: 3 and 0.

An input of 3 gets converted to 111 and emerges from the source (?) moving east. It follows the arrow down to the +, where the 1 bits turn right (west). They fall into the sink (!) and are converted back to decimal 3 and output.

An input of 0 gets converted to 0. When the 0 bit reaches the +, it turns left into the dupneg (~). Here the 0 turns right (south, off the playfield) and a negated copy of it turns left (north). This negated copy, being a 1 bit, goes back around to the + and turns right. It falls into the sink, is converted to decimal 1, and is output.

# Triangular, 6 bytes

$,w%1< Try it online! 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

1|

## Explanation

a   # Take an input
1| # Logical or with 1
# If n is 0, this evaluates to 1
# It evaluates to 0 otherwise

# 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
1x2f1a1o         # Function 2
# Add 1 to the register and output
1x3f1r3x1v4e1o3f # Function 3
# Read a byte of input
# Otherwise, output it and jump back to the start of function 3
1x4f0a           # Function 4
# Add 0 to the register
1f               # Call function 1

# CJam, 5 bytes

ri1e>

Try it online!

### Explanation

ri   e# Read input as an integer
1    e# Push 1
e>   e# Maximum. Implictly display

## Batch, 17 bytes

@cmd/cset/a%1+!%1