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

(40 points) Credit card numbers follow certain patterns. A credit card number mu

ID: 3704273 • Letter: #

Question

(40 points) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following:

4 for Visa cards

5 for MasterCard 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 is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly known as the Luhn check or the Mod 10 check. It 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 the two digits to get a single digit number.

2*2 =4, 2*2=4, 4*2=8, 1*2=2, 6*2=12 (1+2 =3), 5*2=10 (1+0=1)

8*2= 16(1+6=7), 4*2=8              

Now add all single -digit numbers from step 1.

Add all digits in the odd places from right to left in the card number.

Sum the results from step 2 and step 3.

If the result from step 4 is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the number 4388576018402626 is invalid, but the number 4388576018410707 is valid.

Write a program that prompts the user to enter a credit card number as a string and displays whether the number is valid.

Your program should have at least the following functions:

bool isValid (string cardNumber ): This function returns true if the card number is valid

int sumOfDoubleEvenPlace (string cardNumber ): This function gets the result from step 2

int getDigit (int number): This function returns this number if it is a single digit, otherwise, returns the sum of the two digits

int sumOfOddPlace(string cardNumber ): This function returns the sum of odd-place digits in the card number

Do not use any global variables.

You can use function length() or size() to ?nd out the number of characters in a string. Also, you can access each character in a string by its index. For example:

string word = “Hi” ;

cout <<word.size(); // displays 2

cout <<word[0]; // displays H

To convert a character into a corresponding decimal number, you can use ASCII value of the character. For example, to convert character ‘2’ into the corresponding decimal number 2, you can do the following:

int num = ‘2’ - ‘0’; // the ASCII value for ‘2’ is 50 and for ‘0’ is 48

cout <<num; // displays 2

The following example displays the sum of the numbers in a string:

    string number="246";

       

       int sum = 0;

       for (int i = 0; i < number.size(); i++)

       {

              sum += number[i] - '0' ;

       }

  cout << sum ; // displays 12

Explanation / Answer

import java.util.Scanner;

public class CreditCard

{

boolean lengthStart(String num)

{

boolean result = false;

if(num.length() >= 13 && num.length() <= 16)

{

switch(num.charAt(0))

{

case '4' :

result = true; //visa cards

break;

case '5' :

result = true; //master card

break;

case '6' :

result = true; //discover card

break;

case '3' :

if(num.charAt(1)!='7')

{

result = false; //American express card

}

else

{

result = true;

}

break;

default :

result = false;

}

}

else

{

result = false;

}

return result;

}

int sumOfDoubleEvenPlace(String num)

{

int sum = 0;

//Reverse the string first

int len = num.length();

String rnum = "";

for(int i = len -1; i >=0; i--)

{

rnum = rnum + num.charAt(i);

}

for(int i=1; i<len; i=i+2)

{

//step 2

int twice = (rnum.charAt(i) - '0') * 2;

if( twice > 9)//handling two digit double

{

twice = (twice/10)+(twice%10);

}

sum = sum+twice;

}

return sum;

}

int sumOfOddPlace(String num)

{

int sum = 0;

//reversing the string first

int len = num.length();

String rnum = "";

for(int i = len -1; i >=0; i--)

{

rnum = rnum + num.charAt(i);

}

for(int i=0; i<len; i=i+2)

{

sum = sum + (rnum.charAt(i) - '0');

}

return sum;

}

public boolean isValid(String num)

{

boolean result = false;

result = lengthStart(num);

System.out.println(result);

//step 1

int sum1 = sumOfDoubleEvenPlace(num);

System.out.println(sum1);

//step 3

int sum2 = sumOfOddPlace(num);

System.out.println(sum2);

//step 4

int sum = sum1 + sum2;

System.out.println(sum);

if(sum%10 == 0)

{

result = true;

}

else

{

result = false;

}

return result;

}

public static void main(String[] args)

{

Scanner s1 = new Scanner(System.in);

System.out.println("Enter Credit Card Number :");

String num = s1.nextLine();

s1.close();

CreditCard c1 = new CreditCard();

boolean result = c1.isValid(num);

if(result == true)

{

System.out.println(" Card Number is Valid!");

}

else

{

System.out.println(" Card Number is InValid!");

}

}