42
3
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 is0
. - 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 code-golf, so shortest answer in bytes wins.
Standard loopholes apply.
1
You should probably put a link to the TNB CMC, since that's where this challenge came from.
– mbomb007 – 2017-05-03T20:53:52.313Does the answer need to be a full function, or can it be the body? – Caleb Kleveter – 2017-05-03T20:58:40.040
1@CalebKleveter The default rule in PPCG is that the answer is either a function or a full program, but not snippets. – Leaky Nun – 2017-05-04T01:56:56.173
Can we print the output with a leading zero? – MD XF – 2017-12-26T21:54:48.247
@MDXF yes, you can. – Leaky Nun – 2017-12-26T22:30:39.573
@LeakyNun the question text says "leading zeros are not allowed" but your comment contradicts this – Giuseppe – 2018-01-17T18:08:59.110