2

I want to write a method that returns digit of a number (num) by index (place) while not using array only loops and conditions.

For example:

  1. For the input num = 17489, place = 1 the method returns 9
  2. But if the index does not exist in the like num = 17489, place = 6 return -1

something in my code doesn't work if I set i = 0 case 1) work but 2) no if i set i = 1 case 2) work but 1)no please help figure whats wrong

    public static int Digit_by_place(int num, int place)
    {
        int digit = 0, i = 0;

        // the lop run until the place in the 
        while (num > 0 && i <= place)number
        {
            digit = num % 10;
            num = num / 10;
            i++;

            // if there is no didgit in the input place return -1
            if (i < place && num == 0)
            {
                return -1;
            }

            // return the last digit from left (the bigest)
            if (num == 0 && digit > 0 && i == place)
            {
                return digit;
            }
        }

        return digit;
    }
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
MarcoPolo
  • 21
  • 3
  • A basic understanding of english language would be really helpful. It´s really hard to understand what works and what not. Apart from this please clearify what "not working" actually means. – MakePeaceGreatAgain Dec 21 '18 at 11:14
  • @HimBromBeere in my code if i set i=1 and input any index between 1-num length it works the problem is if i input iindex>num length for example 14879,5 return 1 cause thats the last digit in the num but if i input 14879,6 it still return 1 insted of -1 – MarcoPolo Dec 21 '18 at 11:38

3 Answers3

4

We can exploit the fact that int can have at most 10 digits.

Code:

public static int Digit_by_place(int num, int place) {
  if (place <= 0 || place > 10)   // place must be in [1..10] range
    return -1;
  else if (num == 0)              // special case num == 0
    return place == 1 ? 0 : -1;

  for (int i = 1; i < place; ++i)  
    num /= 10;

  return num == 0 ? -1 : Math.Abs(num % 10);
}

Demo:

using System.Linq;
...

Tuple<int, int>[] tests = new Tuple<int, int>[] {
  Tuple.Create( 17489,  1),
  Tuple.Create( 17489,  2),
  Tuple.Create( 17489,  3),
  Tuple.Create( 17489,  4),
  Tuple.Create( 17489,  5),
  Tuple.Create( 17489,  6),
  Tuple.Create(-17489,  1),
  Tuple.Create(-17489,  6),
  Tuple.Create( 17489,  0),
  Tuple.Create( 17489, -1),
  Tuple.Create(-17489, -1),
  Tuple.Create( 17489,  5),
  Tuple.Create(-17489,  5),
  Tuple.Create(     0,  1),
  Tuple.Create(     0,  4),
};

var report = string.Join(Environment.NewLine, tests
  .Select(test => 
     $"Digit_by_place({test.Item1,6}, {test.Item2,2}) = {Digit_by_place(test.Item1, test.Item2),2}"));

Console.Write(report);

Outcome:

Digit_by_place( 17489,  1) =  9
Digit_by_place( 17489,  2) =  8
Digit_by_place( 17489,  3) =  4
Digit_by_place( 17489,  4) =  7
Digit_by_place( 17489,  5) =  1
Digit_by_place( 17489,  6) = -1
Digit_by_place(-17489,  1) =  9
Digit_by_place(-17489,  6) = -1
Digit_by_place( 17489,  0) = -1
Digit_by_place( 17489, -1) = -1
Digit_by_place(-17489, -1) = -1
Digit_by_place( 17489,  5) =  1
Digit_by_place(-17489,  5) =  1
Digit_by_place(     0,  1) =  0
Digit_by_place(     0,  4) = -1
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
  • Shouldn't `Digit_by_place(-17489, 1) = 9` return -9? – Magnus Dec 21 '18 at 12:28
  • @Magnus: I assume digits be in `[0..9]` otherwise `-1` which now stands for *error* will conflict with `-1` from, say `Digit_by_place(-17489, 5)`. If *signed* digits are required, we should remove `Math.Abs` – Dmitry Bychenko Dec 21 '18 at 12:30
  • Yeh, I guess that makes sense if -1 is used for out of range – Magnus Dec 21 '18 at 12:35
1

Using mathematics only.

public int GetNumber(int number, int position)
{
    if(number == 0 && position == 1) 
       return 0;
    if(position <= 0 || position > (int)(Math.Log10(number = Math.Abs(number)) + 1)) 
       return -1;
    return (int)((number % (Math.Pow(10, position))) / (Math.Pow(10, position - 1)));
}
Magnus
  • 45,362
  • 8
  • 80
  • 118
  • Now it's correct, +1; however using `double` math methods (`Math.Log10` and `Math.Pow`) when solving pure `integer` task is IMHO an overshoot. – Dmitry Bychenko Dec 21 '18 at 13:15
0
for(int i=0; i<place; i++)
{
       num = num / 10;
       if (num <= 0)
           return -1;
}
return num % 10;

this should work if you start to count with 0

GrabnaMax
  • 47
  • 7