2

I wanna make a funcion that will take a natural number and make a new number so every digit in the old number will be incremented and if the digit is 9 it will become zero, but not to check specificly if the digit is 9. example: 930 will return 41 9999 will return 0 879021 will return 980132.

This is what i got so far:

int newNumber(int n)
{
    int dig;
    if (n < 9)
        return n + 1;

    dig = n % 10;
    dig++;
    n = n / 10;
    n = n * 10 + dig;

    return newNumber(n/10);
}
Bathsheba
  • 231,907
  • 34
  • 361
  • 483
piscoony
  • 31
  • 5

5 Answers5

2

There are a couple of issues with your code: It doesn't handle a single digit of 9 (which cause a stack overflow). Adding 1 to 9 makes 10 not 0.

I've run it through the sample data you supplied and it seems to work (in C#) and it has a hard core recursive line at the end.

    int newNumber(int n)
    {
        if (n == 9)
            return 0;
        if (n < 9)
            return n + 1;
        return (newNumber(n / 10) * 10) + newNumber(n % 10);
    }
Code Gorilla
  • 962
  • 9
  • 23
  • thanks, i am trying to handle the 9 issue without specificly checking if n == 9 – piscoony Jan 04 '18 at 11:45
  • 1
    @piscoony Then try `if(n<=9) return (n+1)%10;` but unless this is a challenge there's no guarantee that is more efficient than the other. In fact the best way to improve this is remove the recursion and do it in a loop! You could also implement a lookup table of 10 `int`s `{1,2,3,4,5,6,7,8,9,0}` but again, why? – Persixty Jan 04 '18 at 11:49
  • @meaning-matters yup – piscoony Jan 04 '18 at 11:52
  • 2
    @Persixty yeah i am learning about recursive functions in c and try this as a challenge to do this without if n == 9 then return 0 – piscoony Jan 04 '18 at 11:53
1

Here's to avoid the check for n == 9:

int newNumber(int n)
{
    static int table[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };

    return (n <= 9) ? table[n] : (newNumber(n / 10) * 10) + newNumber(n % 10);
}

A lookup table seems the most appropriate and does exactly what the requirements describe. Trying to use the non-compatible arithmetic operators results in side effects (as we see in Bathsheba's answer for example), that then need to be corrected.

meaning-matters
  • 21,929
  • 10
  • 82
  • 142
  • Yes, this is clearer than my answer which is bordering on an exercise in obfuscation. Writing `n < 10` might keep the OP happy in that the 9 gets disappeared. Have an upvote. – Bathsheba Jan 04 '18 at 13:18
  • @Bathsheba I have the feeling we'd get the best solution by combining your nice tail recursion with my lookup table. (But busy with other things to sort that out now.) – meaning-matters Jan 04 '18 at 13:20
1
unsigned newNumber(unsigned n, unsigned c = 0)
{
    return n ? (n + 1) % 10 + 10 * newNumber(n / 10, 1 + c) : !c;
}

is one way, and it will treat 0 as 1, via the !c branch where c counts the number of recursions. Note the tail recursion in the ternary conditional branch - some compilers will optimise a tail recursion out to a simple loop, see What is tail recursion?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
1

Bathsheba's solution posted above is very elegant by using the ternary operator, but it will give you a wrong result if the input is zero. To avoid that you may use a stub function:

#include <stdio.h>

int incDigits(int n)
{
    return n ? (n + 1) % 10 + incDigits(n / 10) * 10 : 0;
}

int newNumber(int n)
{
    return n ? incDigits(n) : 1;
}

int main()
{
    for(int i = 0; i <= 100; ++i)
    {
        int n = newNumber(i);
        printf("%d -> %d\n", i, n);
    }
}

EDIT: user meaning-matters also posted a way to fix the input value problem using a lookup table, but he still has to check if n equals 9, which is something you don't want. So I believe using a stub function still is the best way.

Jango
  • 378
  • 4
  • 8
-1

Two ternary operator has been used to take care of the two cases: i) number equal to 9 ii) number not equal to 9 => Another ternary operator is used to take care of further two possible cases: a) number greater than 9( return sum of num(n/10)*10 and num(n%10) ); this can be further elaborated based on the argument fed to the num function. b)number smaller than 9(return number plus one(n+1))

Once this function is called from the main function with argument equal to the number to be transformed in the manner asked in the question, each call in line4 will undergo recursion until they pass the argument to the subsequent iteration less than or equal to 9(which leads to termination of the recursion). With basic understanding of recursion, the above para can easily be understood in context to the subroutine below.

Blockquote

int num(int n)//line1 {//line2 int t;//line3 t=(n==9?0:(n>9?num(n/10)*10+num(n%10):n+1));//line4 return t;/line5 }//line6

Blockquote