I supposed to write a recursive code that finds the position of a particular digit in a number, starting from the right. Eg position(12345,3) = 3, position(123513, 3) = 1 ...
My code goes like this
This works well and fine for all numbers except for cases when the digit is not found in the number. Eg position(12345, 8)Code:int position(int number, int digit) { if ((number%10) == digit) return 1; return 1+ position(number/10, digit); }
I am supposed to return a 0 value, not print nothing when I use printf to print out the value.
Eg position(1000, 4) = 0
It looks easy but I am stumped by it. I'm sure some of you experts out there can help me with this.
Thanks.