title: Write a program in C to apply Luhn's algorithm for credit card validation
body: <p>I need my program to prompt a user for an input and re-prompt in case the input doesn't follow a credit card format (ex: negative numbers or letters, etc.) and then apply the algorithm to see if the number is a valid credit card number and if yes, whether it's Visa, MasterCard or AmEx.</p>
  <p>I know that this question has been answered with different codes on this website, I swear I read everything that I could possibly find (on this site and elsewhere on the net) but I'm having a really hard time understanding the C syntax and I wanted to try to come up with something myself instead of copying bits of codes I don't understand from other answers. If someone can help me out and look at what I've done so far and tell me what I'm doing wrong I would be really grateful. Also, any tips that could help me make sense of the C syntax logic better would be extremely appreciated.</p>
  <p>My program is compiling but when I run it it's acting up in a very weird way: when I enter an input sometimes it will say that it is invalid (even if it's a valid number) and sometimes it will just not return anything after I press enter and won't stop running no matter how many times I press the return key.</p>
  <p>Here is my code so far:</p>
  <pre class="lang-c prettyprint-override"><code>
  #include &lt;stdio.h&gt;
  #include &lt;cs50.h&gt;
  #include &lt;math.h&gt;
  
  int main(void)
  {
      printf(&quot;Please give me your credit card number:\n&quot;) ;
  
      long long card_num ;
  
      do
      {
          card_num = GetLongLong() ;
      }
      while (card_num &lt; 1 || card_num &gt; 9999999999999999) ;
  
      // Make a copy of the card number to be used and modified throughout the process.
  
      long long temp_num = card_num ;
      int digit = 0 ;
      int count = 0 ;
      int sum_a = 0 ;
      int sum_b = 0 ;
  
      // Isolate every digit from the credit card number using a loop and the variable 'digit'.
      // Keep track of the amount and position of each digit using variable 'count'.
  
      while (card_num &gt;= 0)
      {
          digit = card_num % 10 ;
          count++ ;
          temp_num = (card_num - digit) / 10 ;
          break ;
  
  
       // Apply Luhn's algorithm using two different 'for' loops depending on the position of each digit.
          
          for (count = 0 ; count % 2 == 0 ; count++)
          {
              sum_a = sum_a + ((card_num % 10) * 2) ;
          
              while ((card_num % 10) * 2 &gt;= 10)
              {
                  sum_a = (sum_a % 10) + 1 ;
              } 
          }
          
          for (count = 0 ; count % 2 != 0 ; count++)
          {
              sum_b = sum_b + digit ;
          }
          
          return sum_a ;
          return sum_b ;
          return count ;
      }
  
      // Checking the validity of the number according to Luhn's algorithm
  
      int total_sum = sum_a + sum_b ;
  
      if (total_sum % 10 != 0)
      {
         printf(&quot;This is an invalid number.\n&quot;) ; 
      }
  
  // If the number entered doesn't have the right amount of digits according
  // to variable 'count', declare the number as invalid.
  
      if (count != 13 || count != 15 || count != 16)
      {
          printf(&quot;This is an invalid number.\n&quot;) ;
      }
  
  // Reset value of variable 'temp_num' and apply calculations that will isolate the first two digits. 
  // Store the results in a variable 'company_id'.
  
      temp_num = card_num ;
      int company_id ;
  
      while (temp_num &gt; 100)
      {
          temp_num = card_num - (card_num % 10) ;
          company_id = temp_num / 10 ;
      }
  
      return company_id ;
  
  // Print the type of credit card depending on the company ID and amount of digits.
  
      if (company_id &gt; 50 &amp;&amp; company_id &lt; 56 &amp;&amp; count == 16)
      {
          printf(&quot;MASTERCARD\n&quot;) ;
      }
      else if ((company_id == 4) &amp;&amp; (count == 13 || count == 16))
      {
          printf(&quot;VISA\n&quot;) ;
      }
      else if ((company_id == 34 || company_id == 37) &amp;&amp; (count == 15)) 
      {
          printf(&quot;AMEX\n&quot;) ;
      }
      else
      {
          printf(&quot;This is an invalid number.\n&quot;) ;
      }
  
      return 0 ;
  
  }
  </code></pre>