[Solved] Credit Card Long/String Validation [closed]


You have mistake in both sumOfEvenPlaces() and sumOfOddPlaces() methods.

In sumOfEvenPlaces(long number) you have to change

 number %= 10; 

to

 number /= 10;

You can only skip a digit using number / 10 not by number % 10

And in both functions change

 while (number % 10 != 0 || number / 10 != 0)

to

 while (number > 0)

by using while (number > 0) the loop will work until all digits in number are processed.

Try these modified methods , This will work :-

public static int sumOfEvenPlaces(long number) {
    int sum = 0;
    int remainder;
    number /= 10;          // change 1
    while (number > 0) {   // change 2
        remainder = (int) (number % 10);
        sum = sum + getDigit(remainder * 2);
        number /= 100;
    }
    return sum;
}

public static int sumOfOddPlaces(long number) {
    int sum = 0;
    int remainder;

    while (number > 0) {   // change 3 
        remainder = (int) (number % 10);
        sum = sum + getDigit(remainder);
        number /= 100;
    }
    return sum;
}

Output :-

Enter Credit Card Number for Validation: 
 4246345689049834
4246345689049834 is invalid

0

solved Credit Card Long/String Validation [closed]