Here are 6 different versions of function findDigit with the same pre- and post- conditions.
Trace with some or all of the following data:
findDigit (30568,2);
findDigit (324,5);
findDigit(-4532,3)
int findDigit(int num, int place) // pre : num is a valid integer; place >= 1 // post: findDigit returns the value in the position place { int dividend = 10; for (int count = 1; count < place; count++) dividend = dividend * 10; num = num / dividend; return (num % 10); }
int findDigit(int num, int place) { int divide = 1; for (int count = 2; count <= place; count ++) divide = divide * 10; return (num / divide); {
int findDigit ( int num, int place) { return (int) num / power (10,place+1)/ power(10,place); }
int findDigit ( int num, int place) { double result = num; for (int count = 1; count <= place; count++) result = result - 1; while (result >= 1) result = result - 1; return int( result * 10); }
int findDigit(int num, int place) { int count = 1; int x = abs(num); while ((x >= 10) && (count != place)) { count ++; x = x / 10; } if (count == place) return (x % 10); else return 0; }
int findDigit (int num, int place) { if (place == 1) return (num % 10); return (findDigit (num / 10, place -1); {