# Increment each digit in a number to form a new number

Today I've found /r/dailyprogrammer and I've solved an easy challenge. I'm new to coding and I'm not sure if this is good way to solve this kind of problems. Could you, please, give me some hints? How to make my code more clear and readable? Thanks!

Challange description:

A number is input in computer then a new no should get printed by adding one to each of its digit. If you encounter a 9, insert a 10 (don't carry over, just shift things around).

For example, 998 becomes 10109.

Bonus

This challenge is trivial to do if you map it to a string to iterate over the input, operate, and then cast it back. Instead, try doing it without casting it as a string at any point, keep it numeric (int, float if you need it) only.

private static int Challange(int number)
{
int digits = (int)Math.Log10(number); //this is number of digits -1
int result = 0;
for (int i=0; i <= digits; i++)
{
int tens = (int)(Math.Pow(10, digits - i));
int currentDigit = ((number / tens) % 10);

if (currentDigit == 9) result *= 10;
result += (currentDigit + 1) * tens;
}
return result;
}

• Is 0 a valid input? If so, what should the result be? – YawarRaza7349 Feb 27 '19 at 20:48

It's a matter of taste but I find a recursive solution to be reasonably readable :

private static int IncrementDigits(int number){
if (number == 0){
return 0;
}

int lastDigit = number % 10;
int firstDigits = number / 10;

if (lastDigit == 9){
return 10 + 100 * IncrementDigits(firstDigits);
} else {
return lastDigit + 1 + 10 * IncrementDigits(firstDigits);
}
}


Note that it only works for number > 0.

Here's a working example: https://dotnetfiddle.net/xUKs6M

• You could use System.Math.DivRem to do the division and modulo operations (on .Net Core this would be faster, the .Net Framework implementation does the naïve thing of doing the division twice) – user190892 Feb 27 '19 at 20:45
• @mistertribs: Thanks. I wanted to stay close enough to OP's answer. I don't really like the output argument in Math.DivRem, to be honest. – Eric Duminil Feb 27 '19 at 21:04

It works, but it's a bit tricky to follow the logic and work out why. If you work from the other end then you can avoid the Log and Pow and shifting the partial result, and I find that easier to read.

Challange is not a descriptive name. What does the method do? IncrementDigits might be a better name, for example.

Computing powers repeatedly and log are not cheap operations. You could substitute them with cheaper alternatives:

• Instead of computing powers of tens, you could start with 1 and multiply in the loop steps.
• Instead of computing the number of digits and using a counting loop, you could divide by 10 until reaching 0.

Something like this:

if (number == 0) {
return 1;
}

int work = number;

int tens = 1;
int result = 0;
while (work > 0) {
int digit = work % 10;
work /= 10;

result += tens * (digit + 1);
tens *= 10;
if (digit == 9) {
tens *= 10;
}
}
return result;
{"ft_lang_label":"__label__en","ft_lang_prob":0.8251552,"math_prob":0.9575562,"size":1064,"snap":"2021-21-2021-25","text_gpt3_token_len":285,"char_repetition_ratio":0.09622642,"word_repetition_ratio":0.0,"special_character_ratio":0.2951128,"punctuation_ratio":0.14798206,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9943395,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-05-09T04:21:59Z\",\"WARC-Record-ID\":\"<urn:uuid:b4afac2d-8b74-4ced-8c40-487173b4b3bc>\",\"Content-Length\":\"187856\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:30d2decf-038f-44de-a72f-610c1d0c3381>\",\"WARC-Concurrent-To\":\"<urn:uuid:620c1676-e024-4fb8-8464-d9c87dd24f10>\",\"WARC-IP-Address\":\"151.101.129.69\",\"WARC-Target-URI\":\"https://codereview.stackexchange.com/questions/214403/increment-each-digit-in-a-number-to-form-a-new-number/214419\",\"WARC-Payload-Digest\":\"sha1:AFZZ4SQRIZJ7C2JTLNYUACEIOBK4BY6T\",\"WARC-Block-Digest\":\"sha1:U7YGHIIBJVWAE3G7TVLNM5CIYLAJH6P5\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-21/CC-MAIN-2021-21_segments_1620243988955.89_warc_CC-MAIN-20210509032519-20210509062519-00160.warc.gz\"}"}