55
\$\begingroup\$

Task

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

Input

A non-negative integer.

  • If you would like to accept the string as input, the string would match the following regex: /^(0|[1-9][0-9]*)$/, i.e. it must not have any leading zeroes, except when it is 0.
  • If you accept a real integer as input, you may assume that the integer is within the handling capability of the language.

Output

A positive integer, specified above. Leading zeroes are not allowed. Your output should match the regex /^[1-9][0-9]*$/.

Testcases

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.

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

164 Answers 164

1 2 3 4 5
6
0
\$\begingroup\$

C (clang), 63 42 bytes

main(i){scanf("%i",&i);printf("%i",i?:1);}

Try it online!

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.

\$\endgroup\$
3
  • 3
    \$\begingroup\$ If statement is an overkill for such task...... \$\endgroup\$
    – Wasif
    Commented Jun 5, 2021 at 6:55
  • \$\begingroup\$ @Wasif It's pretty much the way for beginners. Using if statements always help with programs, and so here it is. Except ceilingcat has golfed it that it's not very visible. \$\endgroup\$ Commented Jun 6, 2021 at 0:10
  • \$\begingroup\$ brevity/creativity is the target of code golf, readability be dammed, an if statement is sooooo boring, after the golf it looks much better, you might want to check Tips for golfing in C \$\endgroup\$
    – Wasif
    Commented Jun 6, 2021 at 5:07
0
\$\begingroup\$

Thunno, \$ 2 \log_{256}(96) \approx \$ 1.65 bytes

1~

Attempt This Online!

Explanation: logical OR (~) with 1.

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

Pyt, 3 bytes

1←↑

Try it online!

1       push 1
 ←      get input
  ↑     get maximum value on the stack; implicit print
\$\endgroup\$
0
\$\begingroup\$

Python, 15 Bytes

lambda x:x or 1

Thanks for choosing a falsey value to avoid!

Try it online!

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

Desmoslang, 14 13 Bytes

IS+1-\sgn(DOT

Old:

ISE\{D=0,D\}OT
\$\endgroup\$
0
\$\begingroup\$

Lua, 24 bytes

print(0<0+...and...or 1)

Try it online!

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

Swift, 19 18 17 bytes

var f={max(1,$0)}

Taking advantage of the "non-negative" rule here.

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

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
\$\endgroup\$
0
\$\begingroup\$

Uiua, 2 bytes

↥1

Try it!

The ↥ maximum with 1.

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

CASIO BASIC (CASIO fx-9750GIII), 8 bytes

?→A
A+Not A
\$\endgroup\$
0
\$\begingroup\$

Uiua, 2 bytes

↥1

Try this online!

No need for explanation but here's one:

↥1 => determine maximum between n and 1
\$\endgroup\$
0
\$\begingroup\$

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

Try It Online!

BrainChild, 11 bytes

int s=>s||0

Try It Online!

\$\endgroup\$
-1
\$\begingroup\$

Excel VBA 17 bytes

[a1]=IIF(n=0,1,n)

Simple if else statement you can run it from vba immediate window

\$\endgroup\$
2
  • 2
    \$\begingroup\$ Hey, just a note on convention with VBA and Excel VBA for PPCG - you must take input from either a cell or from a passed variable. You can output to the immediate window, msgbox, function value, to a cell or by modifying a passed variable. This means that your above solution is not valid as it does not take input from cell or a passed variable, but rather relies on the variable being passed to it. \$\endgroup\$ Commented Jun 14, 2017 at 20:29
  • 5
    \$\begingroup\$ Save two bytes and make it a valid answer with ?-([A1]=0)+[A1] \$\endgroup\$ Commented Aug 7, 2017 at 14:00
-1
\$\begingroup\$

JS ES5, 26 bytes

function (n){return n?n:1}
\$\endgroup\$
3
  • \$\begingroup\$ Save one byte by removing the space after function. \$\endgroup\$
    – Adalynn
    Commented Aug 6, 2017 at 0:30
  • \$\begingroup\$ n||1 \$\endgroup\$
    – l4m2
    Commented Dec 18, 2017 at 20:42
  • \$\begingroup\$ I think you can also write this as function(n)n||1 (taking the n||1 idea from l4m2) \$\endgroup\$
    – user41805
    Commented Jan 5, 2018 at 15:15
1 2 3 4 5
6

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.