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

Credit card number check. The last digit of a credit card number is the check di

ID: 3640397 • Letter: C

Question

Credit card number check. The last digit of a credit card number is the check digit, which protects again transcription errors such as an error in a single digit or switching two digits. The following method is used to verify actual credit card numbers, but for simplicity we will describe it for 8 digits instead of 16:
• Starting from the rightmost digit, calculate the sum of every other digit. For example, if the credit card number is 43589795, then you form the sum 5 + 7 + 8 + 3 = 23.
• Double all the digits that were not included in the preceding step. For example, with the number give above you double the digits 9, 9, 5, and 4, which yield 18, 18, 10, and 8. Adding all the digits in these numbers gives 1+8+1+8+1+0+8 = 27.
• Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our example, 23 + 27 = 50, so the number is valid.
Write a program that implements this algorithm. The user should supply a number (not individual digits, but a single number with any number of digits that you can store in a variable of type long), and you should print out whether the number is valid or not. Suggested extension: also print the value of the check digit that would make the number valid.

Explanation / Answer

Hi,

my friend hope this will help u if u need more help message me
PLEASE RATE

Note: the card type is initially Visa u can change it

#include <conio.h>
#include <iostream>
#include <cstring>
#include <ctype.h>
#include <string>

using namespace std;
using std::string;

enum CardType{
MasterCard,
BankCard,
Visa,
AmericanExpress,
Discover,
DinersClub,
JCB
};

bool Validate(CardType cardType, string cardNumber){
int number[16], len=0, srln = cardNumber.length();
for(int i=0; i<srln; ++i){

if(isdigit(cardNumber[i])){
if(len == 16) return false;
number[len++] = cardNumber[i];
}
}

switch(cardType){

case MasterCard:
if(len!=16) return false;
if(number[0] != 5 || number[1] == 0 || number[1] > 5)
return false;
break;

case BankCard:
if(len != 16)
return false;
if(number[0] != 5 || number[1] != 6 || number[2] > 1)
return false;
break;

case Visa:
if(len != 16 && len != 13)
return false;
if(number[0] != 4)
return false;
break;

case AmericanExpress:
if(len != 15)
return false;
if(number[0] != 3 || (number[1] != 4 && number[1] != 7))
return false;
break;

case Discover:
if(len != 16)
return false;
if(number[0] != 6 || number[1] != 0 || number[2] != 1 || number[3] != 1)
return false;
break;

case DinersClub:
if(len != 14)
return false;
if(number[0] != 3 || (number[1] != 0 && number[1] != 6 && number[1] != 8)
|| number[1] == 0 && number[2] > 5)
return false;
break;

case JCB:
if(len != 16 && len != 15)
return false;
if(number[0] != 3 || number[1] != 5)
return false;
break;

default:
cout<<" No such type of card exists .... !";
}

//Using the Luhn's Algorithm
int sum = 0;
for(int i=len-1; i>=0; ++i){
if(i % 2 == len % 2){
int n = number[i] * 2;
sum += (n / 10) + (n % 10);
}
else
sum += number[i];
}
return (sum % 10 == 0);
}

void valid(){
cout << "The credit card number is VALID !";
}

void invalid(){
cout << "The credit card number is INVALID !";
}

int main(){
system("cls");
string ccn, ct;
cout << "Enter the Card Type :";
cin >> ct;
cout << "Enter the Credit card Number :";
cin >> ccn;
CardType card = Visa;
bool val = Validate(card, ccn);
if (val == true) valid();
else invalid();
system("pause");
return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote