0
#include<cs50.h>
#include<stdio.h>

int main(void)
{
   long cc, wcc, d = 1;
   int sum = 0, n = 0, fd ,ftd;

   do
   {
      cc = get_long(" number :  ");
   }
   while ( n < 0 );

   wcc = cc/10;

   while ( wcc > 0 )
    {
      int ld = wcc % 10;
      int a  = ld * 2;
      sum = sum + (a%10) + (a/10) ;
      wcc = wcc / 100;

    }
   wcc = cc;
   while ( wcc > 0 )
   {
      int ld = wcc % 10;
      sum = sum + ld;
      wcc = wcc / 100 ;
   }

   wcc = cc;
   while(wcc != 0)
   {
      wcc = wcc / 10;
      n++;
   }

   for(int i = 0; i < n - 2; i++)
   {
      d = d * 10 ;
   }

   fd  =  cc / d;
   ftd  = cc / (d/10);

   if ( sum % 10 == 0)
   {
      if ( ( ftd == 34 && (n == 15) )  || (ftd == 37 && ( n == 15 ) ) )
      {
         printf("AMEX\n");
      }
      else if ( ftd >= 51 && ftd <=55 && (n == 16 ) )
      {
         printf("MASTERCARD\n");
      }
      else if ( fd == 4 && (n == 13 || n == 16 ) )
      {
         printf("VISA\n");
      }
   }
      else
      {
         printf("INVALID\n");
      }
} 
0

This code, as written, should just prompt for the cc number and drop through because n = 0 before the loop and isn't changed inside the loop. Since n<0 is false, it should just drop through every time. If it were n <= 0, it would be an infinite loop and just keep looping. Either way, both are wrong. You should be checking cc, not n.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .