(Financial: Credit card number validation) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. It must start with

  • 4 for Visa cards
  • 5 for Master cards
  • 37 for American Express cards
  • 6 for Discover cards

In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card is scanned correctly by a scanner. Credit card numbers are generated following this validity check, commonly known as the Luhn Check or the Mod 10 check, which can be described as follows(for illustration, consider the card number 4388576018402626.

  • Double every second digit from right to left. If doubling of a digit results in a two-digit number, add up the two digits to get a single-digit number.
  • Now add all single-digit numbers from Step 1. 4+4+8+2+3+1+7+8=37
  • Add all digits in the odd places from right to left in the card number. 6+6+0+8+0+7+8+3=38
  • Sum the results from Step 2 and Step 3. 37+38=75
  • If the result from Step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018410707 is valid.

Write a program that prompts the user to enter a credit card number as a long integer. Display whether the number is valid or invalid. Design your program to use the following methods:

/** Return true if the card number is valid */
Public static Boolean isValid(long number)

/**Get the result from Step 2 */
Public static int sumOfDoubleEvenPlace(long number)

/**Return this number if it is a single digist, otherwise, return the sum of the two digits*/
Public static int getDigit(int number)

/**Return sum of odd-place digits in number */
Public static int sumOfOddPlace(long number)

/**Return true if the digit d is a prefix for number */
Public static int getSize(long d)

/**Return the number of digits in d */
Pubic static int getSize(long d)

/**Return the first k number of digits from number. If the number of digits in number is less than k, return number. */
Public static long getPrefix(long number, int k)

Credit Card Number Validation: Complete program 6.31 with the following enhancements:

The program should allow the user to input more than one credit card number in a session (handled one at a time). For example, the user can be asked whether they want to continue and yes or no response can be processed or you can use a sentinal input value. Your program should be clear in directing the user on how the program will handle multiple inputs.

The program should have additional output when the user exits the program. This additional output will be the following statistics:

  • The number of Visa Cards processed
  • The number of Master Cards processed
  • The number of American Express Cards processed
  • The number of Discover Cards processed
  • The total number of Cards processed
Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.