Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Validating Credit Cards Credit card numbers are not randomly chosen. There are r

ID: 3940822 • Letter: V

Question

Validating Credit Cards Credit card numbers are not randomly chosen. There are rules that determine which credit card numbers are valid. This is a limited attempt to prevent fraud. If you just make up some nmbe is unlikely to be valid. You are going to write a program to determine if a number is a valid credit card number A credit card number must have between 13 and 16 digits. The first digit or two deter- mines what of card it is: gins with Card Type isa merican 6 over For our purposes, a number that doesn't begin with one of these sequences is not a valid The algorithm for validating credit cards is credited to Hans Luhn of IBM. To illustrate First, we see that this must be a Visa card, since it starts with 4. To validate it 1. Double every second digit from right to left. If doubling of a digit results in a two-digit credit card number the algorithm, we will use the sample number 4388576018402626. number, add up the two digits to get a single-digit number .6*2.12 (1 + 2-3) .5*2=10 (1 +0 = 1) 8*2=16 (1 +6-7) 2. Now, add all the single digit numbers from the previous step. 4+4+8+2+3+1+1+8=37 3. Add all the digits in the odd places from right to left in the number 6+6+0+8+0+7+8+3=38 4. Add these two values together 37 38 = 75

Explanation / Answer

using netbeans ide-

import java.util.Scanner;
public class CCardValidation {


    public static boolean Valid(long num) {

        int total = sumOfDoubleEvenPlace(num) + sumOfOddPlace(num);


        if ((total % 10 == 0) && (prefixMatched(num, 1) == true) && (getSize(num)>=13 ) && (getSize(num)<=16 )) {
            return true;
        } else {
            return false;
        }
    }

    public static int getDigit(int num) {

        if (num <= 9) {
            return num;
        } else {
            int firstDigit = num % 10;
            int secondDigit = (int) (num / 10);

            return firstDigit + secondDigit;
        }
    }
    public static int sumOfOddPlace(long num) {
        int result = 0;

        while (num > 0) {
            result += (int) (num % 10);
            num = num / 100;
        }

        return result;
    }

    public static int sumOfDoubleEvenPlace(long num) {

        int result = 0;
        long temp = 0;

        while (num > 0) {
            temp = num % 100;
            result += getDigit((int) (temp / 10) * 2);
            num = num / 100;
        }

        return result;
    }

    public static boolean prefixMatched(long num, int d) {

        if ((getPrefix(num, d) == 4)
                || (getPrefix(num, d) == 5)
                || (getPrefix(num, d) == 37)
                || (getPrefix(num, d) == 6 )) {

            if (getPrefix(num, d) == 3) {
                System.out.println(" Visa Card ");
            } else if (getPrefix(num, d) == 5) {
                System.out.println(" Master Card ");
            } else if (getPrefix(num, d) == 37) {
                System.out.println(" American Express Card ");
            }else if (getPrefix(num, d) == 6) {
                System.out.println(" discover ");
            }
            return true;

        } else {

            return false;

        }
    }

    public static int getSize(long d) {

        int count = 0;

        while (d > 0) {
            d = d / 10;

            count++;
        }

        return count;

    }

    public static long getPrefix(long num, int k) {

        if (getSize(num) < k) {
            return num;
        } else {

            int size = (int) getSize(num);

            for (int i = 0; i < (size - k); i++) {
                num = num / 10;
            }

            return num;

        }

    }

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

        System.out.print("Enter a credit card number as a long integer: ");

        long input = sc.nextLong();


        if (Valid(input) == true) {
            System.out.println(" " + input + " is Valid. ");
        } else {
            System.out.println(" " + input + " is Invalid. ");
        }

    }
}
    output: Enter a credit card number as a long integer: 4388576018410707
                4388576018410707 is Valid.