title: CS50 Luhn's Algorithm in C. Help much appreciated :)
body: <p>I need to solve the problem on the link below, using Luhn's algorithm and what we have learned so far in CS50 (no arrays).</p>
  <p>My program compiles but it doesn't identify the card type. What am I doing wrong?</p>
  <p>Many thanks in advance!</p>
  <p>Problem: <a href="https://cs50.harvard.edu/x/2020/psets/1/credit/#:%7E:text=check50%20cs50/problems/2020/x/credit" rel="nofollow noreferrer">https://cs50.harvard.edu/x/2020/psets/1/credit/#:~:text=check50%20cs50/problems/2020/x/credit</a></p>
  <pre><code>/* This program checks if a bank card number is syntactically valid, using Luhn's algorithm */
  
  #include &lt;stdio.h&gt;
  #include &lt;cs50.h&gt;
  #include &lt;math.h&gt;
  
  int main(void)
  {
      long long n, temp;
      int digit, last, first, product;
      int lenght = 0;
      int sumOdd, sumEven;
  
  
  // Prompt user for input as long as the inputted number is equal or smaller than zero
  
  do
   {
      n = get_long_long(&quot;Enter your credit card number without special characters:\n&quot;);
  
   } while (n &lt; 0);
  
  
  // Count the number of digits
  
  while (n &gt; 0)
   {
       n = n/10;
       lenght++;
   }
  
  // Check if the number's length is valid
  
  if (lenght != 13 &amp;&amp; lenght != 15 &amp;&amp; lenght != 16)
  {
      printf(&quot;INVALID&quot;);
  }
  
  // Find the last digit and add it to the even sum
  
  while (n &gt; 0)
  {
      last = n % 10;
      temp = n - last;
      sumEven = sumEven + last;
  
  }
  
  /* Starting with second-to-last digit, multiply every other digit by 2. Add those
  products together and then add the sum to the sum of the digits that weren't multiplied by 2 */
  
  while (n &gt; 0)
  
  {
      digit = n % 10;
      temp = n/10;
  
      if (lenght % 2 == 0)
      {
          product = digit * 2;
          sumOdd = sumOdd + product;
  
      } else
  
      {
           sumEven = sumEven + digit;
  
      }
  
  // If the last digit of the sum of sums is zero print the number. Else print INVALID.
  
      if (sumOdd + sumEven % 10 != 0)
      {
          printf(&quot;INVALID\n&quot;);
  
      }
      else
      {
          printf(&quot;%lld\n&quot;, n);
      }
  
  // Identify the user's card type as per CS50 Credit instructions. Cards commencing with 3 are AMEX, with 5 MasterCard and with 4, VISA.
  
  while (first &gt;= 10)
  {
      first = n;
      first = first / 10;
  
      if (first == 3)
      {
          printf(&quot;AMEX\n&quot;);
      }
  
      if (first == 5)
      {
          printf(&quot;MASTERCARD\n&quot;);
      }
  
      if (first == 1)
      {
          printf (&quot;VISA\n&quot;);
      }
  
  }
  
  }
  
  }
  </code></pre>