42
\$\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\$
  • 1
    \$\begingroup\$ You should probably put a link to the TNB CMC, since that's where this challenge came from. \$\endgroup\$ – mbomb007 May 3 '17 at 20:53
  • \$\begingroup\$ Does the answer need to be a full function, or can it be the body? \$\endgroup\$ – Caleb Kleveter May 3 '17 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 May 4 '17 at 1:56
  • \$\begingroup\$ Can we print the output with a leading zero? \$\endgroup\$ – MD XF Dec 26 '17 at 21:54
  • \$\begingroup\$ @MDXF yes, you can. \$\endgroup\$ – Leaky Nun Dec 26 '17 at 22:30

124 Answers 124

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

Gol><>, 5 bytes

I:z+h

Try it online!

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

|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

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)
|improve this answer|||||
\$\endgroup\$
0
\$\begingroup\$

Gaia, 2 bytes

1Ṁ

Try it online!

Takes the ax of 1 and the input.

|improve this answer|||||
\$\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

|improve this answer|||||
\$\endgroup\$
  • 1
    \$\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\$ – Taylor Scott Jun 14 '17 at 20:29
  • 2
    \$\begingroup\$ Save two bytes and make it a valid answer with ?-([A1]=0)+[A1] \$\endgroup\$ – Engineer Toast Aug 7 '17 at 14:00
1 2 3 4
5

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.