-5

this is my first post so excuse me if I am not being clear enough.

As the title says I want to split a number into smaller parts, for example 1998 to 1,9,9,8. I have looked around but I am stuck trying to figure out the right term to search for.

Reason why I want this done is to later multiply every other number with either 1 or 2 to be able to examine whether it's correct or not.

Sorry I can't provide any code because I am not sure how I should tackle this problem.

  • 2
    Mod 10 (or DivRem) is likely what you want to Google for. – mjwills Oct 06 '20 at 13:23
  • mjwills solution will work and be the most efficient. The *simplest* solution, however, would be to (1) convert your number into a string `myNumber.ToString()`, and then (2) convert that string into a char array `myString.ToCharArray()`. You can then loop through those chars and use `Char.GetNumericValue()` to do your calculation. – Heinzi Oct 06 '20 at 13:31
  • BTW, what you are trying to do sounds very similar to [calculating a GTIN check sum](https://stackoverflow.com/q/10143547/87698). – Heinzi Oct 06 '20 at 13:31

2 Answers2

1

As @mjwills said, mod is what you want.

    static IEnumerable<int> Digitize(int num)
    {
        while (num > 0)
        {
            int digit = num % 10;

            yield return digit;

            num = num / 10;
        }
    }

And then call it like so:

    static void Main(string[] _)
    {
        int num = 1998;

        int[] digits = Digitize(num).ToArray();

        Console.WriteLine($"Original: {num}");

        foreach (var digit in digits)
            Console.WriteLine(digit);
    }
MikeJ
  • 1,299
  • 7
  • 10
0

make a string of that number and make it like this

```
int a = 1990;
int[] intArray = new int[a.ToString().Length];
int index = 0;
foreach( char ch in a.ToString())
{
   intArray[index] = int.Parse(ch.ToString());
   index++;
}
milad
  • 43
  • 5