0
votes

This a problem from CS50 finding validation of credit card number using luhn's algorithm. I wrote this code but whenever I put any credit card number, the output comes as invalid.Can anyone please help me what my mistake here and how can I move forward with this code.

This is luhn's algorithm formula-- most cards use an algorithm invented by Hans Peter Luhn of IBM. According to Luhn’s algorithm, you can determine if a credit card number is (syntactically) valid as follows:

Multiply every other digit by 2, starting with the number’s second-to-last digit, and then add those products’ digits together. Add the sum to the sum of the digits that weren’t multiplied by 2. If the total’s last digit is 0 (or, put more formally, if the total modulo 10 is congruent to 0), the number is valid!

//Project Using Luhn's Algorithm - Credit Card validation

#include<cs50.h>
#include<stdio.h>
#include<string.h>
int main()
{
   long long ccnum = 0;



    int digitcounter = 0 ;
    char result[11] ;
    int sum = 0 ;
    int  divisor = 10 ;

    do
    {
      ccnum = get_long("Enter your credit card number: ") ;
    }
    while (ccnum <= 0) ;

//--------------------CHECKSUM--------------------------------

    //Case-01(Multiply every other digit by 2):

    long long tempccnum = ccnum ;
    while (tempccnum > 0)
    {
        int lastdigit = tempccnum % 10 ;
        sum = sum + lastdigit ;
        tempccnum = tempccnum / 100 ;
    }

    // Case-02(Multiplication every other digit by 2):

    tempccnum = ccnum / 10 ;
    while(tempccnum > 0)
    {
        int secondlastdigit = (tempccnum % 10) ;

        int tempmultiply = secondlastdigit*2 ;

        sum = sum + (tempmultiply % 10) + (tempmultiply / 10)  ;

        tempccnum = tempccnum / 100 ;
    }
//----------first two digit extraction------------

   long long initdigit = 0, tempinitdigit = 0 ;

   while(ccnum)
   {
     initdigit = tempinitdigit  ; // initdigit will hold the first two digits.
     tempinitdigit = ccnum  ;
     ccnum = ccnum/10 ;
   }




//-------------Digit counter----------
    while(ccnum != 0)
    {
          // iterate until ccnum becomes 0
         // remove last digit from ccnum in each iteration
        // increase digitcounter by 1 in each iteration

        ccnum = ccnum / 10 ;
        digitcounter++ ;
    }
  //------------------Final part - Condition checking------------------------

    if(sum % 10 == 0)
    {
        if (digitcounter == 15 && (initdigit == 34 || initdigit == 37))
        {
            strcpy(result, "AMEX\n") ;
        }

        else if (digitcounter == 16 && (initdigit >= 51 && initdigit <=55))
        {
             strcpy(result,"MASTERCARD\n") ;
        }

        else if ((digitcounter == 13 || digitcounter == 16) && tempinitdigit == 4)
        {
             strcpy(result,"VISA\n") ;
        }

        else
        {
             strcpy(result,"INVALID\n") ;
        }

    }

    else
    {
        strcpy(result,"INVALID\n") ;
    }
    printf("%s\n", result) ;
}
2
What's the value of ccnum after you've counted the digits? - Mat

2 Answers

0
votes

Your code to extract the first 2 digits is wrong:

  while(ccnum)
   {
     initdigit = tempinitdigit  ; // initdigit will hold the first two digits.
     tempinitdigit = ccnum  ;
     ccnum = ccnum/10 ;
   }

That will just set initdigit to the 2nd digit. However tempinitdigit does hold the 1st digit so you should add this after the while loop:

   initdigit = initdigit + tempinitdigit * 10;

The best way to troubleshoot these kind of problems is to step through the code with a debugger.

0
votes

in the first loop, you state this:

    while(ccnum != 0)
    {
          // iterate until ccnum becomes 0
         // remove last digit from ccnum in each iteration
        // increase digitcounter by 1 in each iteration

        ccnum = ccnum / 10 ;
        digitcounter++ ;
    }

(you say, literally: iterate until ccnum becomes 0) If you remove all digits of ccnum and get 0, how are you going to process all those digits? The second, and next loops you have in your code receive 0 as the value of ccnum (you have been shotting at it until it died, remember :) )