title: Implementing Luhn algorithm using C#
body: <p>I am using following code to implement <a href="https://en.wikipedia.org/wiki/Luhn_algorithm" rel="nofollow noreferrer">Luhn algorithm</a> for credit card check in C# language, but could not get the output to generate the check sum its showing validity. Kindly help me. Thank you in advance.</p>
  <pre><code>public class Program
  {
      private static void Main(string[]creditcard)
      {
          int sum = 0, d;
          string num =&quot;7992739871&quot;;
          int a = 0;
  
          for (int i = num.Length - 2; i &gt;= 0; i--)
          {
              d = Convert.ToInt32(num.Substring(i, 1));
              if (a % 2 == 0)
                  d = d * 2;
              if (d &gt; 9)
                  d -= 9;
              sum += d;
              a++;
          }
  
          if ((10 - (sum % 10) == Convert.ToInt32(num.Substring(num.Length - 1))))
              Console.WriteLine(&quot;valid&quot;);
  
          Console.WriteLine(&quot;sum of digits of the number&quot; + sum);
      }
  }    
  </code></pre>