1

I need to validate Card number of 15 digits, for that I have to generate checksum digit to complete the number, How can I do this, I have done the following; I have a string of 15 digits, and I want to generate checksum digit to complete it. following this article regarding Luhn Algorithms I have tried like this.

public static bool IsAllDigit(string digits)
    {
        if (string.IsNullOrEmpty(digits))
        {
            return false;
        }
        else
        {
            int numberOfChar = digits.Count();
            if (numberOfChar > 0)
            {
                bool r = digits.All(char.IsDigit);
                return r;
            }
            else
            {
                return false;
            }
        }
    }

    // 5*2 1 6*2 0 6*2 1 0*2 0 0*2 0 0*2 0 3*2 6 2*2
    public static string GenerateCRC(string digits)
    {
        int[] firstRow = new int[15];
        string CRC = string.Empty;
        if (IsAllDigit(digits))
        {
            var array = digits.Select(ch => ch - '0').ToArray();
            for (int i = 0; i < array.Length; i++)
            {
                if (i%2==0)
                {
                    var temp = array[i] * 2;
                    if (temp > 9)
                    {
                        var arr = temp.ToString().Select(t => int.Parse(t.ToString())).ToArray();
                        int f = arr.Sum();
                        firstRow[i] = f;
                    }
                    else
                    {
                        firstRow[i] = array[i]*2;
                    }
                }
                else
                {
                    firstRow[i] = array[i];
                }
            }
            var sum = firstRow.Sum();
            int crc = sum % 10;
            CRC = crc.ToString();
        }
        return CRC;           
    }

Is this a best way to implement this and then validate number. Although it gives correct results but this can be summarize or done in smart way. Suggestions please.

Abdul
  • 1,416
  • 9
  • 26
  • 57
  • 1
    This might be better suited to codereview.stackexchange.com – Joey Dec 14 '18 at 07:22
  • 1
    I'm voting to close this question as off-topic because the question is more of a code review question "_is this a best way_" and would be a better fit at https://codereview.stackexchange.com/ – pstrjds Dec 14 '18 at 07:22
  • If you have code that already works for you but you want feedback on how it could be improved, please ask on https://codereview.stackexchange.com/. but provide more detail about what element you're looking to improve. (Read [the guide](https://codereview.meta.stackexchange.com/questions/2436/how-to-get-the-best-value-out-of-code-review-asking-questions) before posting.) – Jon Skeet Dec 14 '18 at 07:22
  • @pstrjds this is not duplicate, the question you have linked as duplicate tells how to validate number with Luhn algorithms, my question asks how to generate remaining checksum for any provided number – Abdul Dec 14 '18 at 12:14
  • @trighati - I did not vote to close as duplicate, but as off topic and suggested it be asked at codereview, see my above comment. – pstrjds Dec 14 '18 at 14:00

0 Answers0