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. Display 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 and 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="2461";
int sum = 0;
for (int i = 0; i < number.size(); i++)
{
sum += number[i] - '0' ;
}
cout << sum ; // displays 13
The following example adds all digits in the odd places from right to left.
string number="2461";
int sum = 0;
for (int i = number.size()-1; i >=0; i-=2)
{
sum += number[i] - '0' ;
}
all the program has to be followed a instruction of above and function has to be used for this program.
Thanks.
Given below is the code for the question.
Please do rate the answer if it was helpful. Thank you
#include <iostream>
#include <string>
using namespace std;
bool isValid (string cardNumber ) ;//This function returns true
if the card number is valid
int sumOfDoubleEvenPlace (string cardNumber ); // This function
gets the result of double even positioned digits and adding them
up
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
int main()
{
string cardNumber;
cout << "Enter card number: ";
cin >> cardNumber;
if(isValid(cardNumber))
cout << cardNumber << " is a valid credit card number"
<< endl;
else
cout << cardNumber << " is NOT a valid credit card
number" << endl;
}
bool isValid (string cardNumber ) //This function returns true if
the card number is valid
{
int s1 = sumOfDoubleEvenPlace(cardNumber);
int s2 = sumOfOddPlace(cardNumber);
int sum = s1 + s2;
if(sum % 10 == 0)
return true;
else
return false;
}
int sumOfDoubleEvenPlace (string cardNumber ) // This function gets
the result of double even positioned digits and adding them
up
{
int sum = 0;
int len = cardNumber.size();
for(int i = len - 2; i >= 0; i-=2)
{
int digit = cardNumber[i] - '0'; //subtract asciii value to get
int
sum = sum + getDigit(2 * digit);
}
return sum;
}
int getDigit (int number)// This function returns this number if it
is a single digit, otherwise, returns the sum of the two
digits
{
if (number < 10)
return number;
else
{
int d1 = number / 10;
int d2 = number % 10;
return d1 + d2;
}
}
int sumOfOddPlace(string cardNumber )// This function returns the
sum of odd-place digits in the card number
{
int sum = 0;
int len = cardNumber.length();
for(int i = len-1; i >= 0; i -= 2)
{
int digit = cardNumber[i] - '0';
sum = sum + digit;
}
return sum;
}
output
-----
Enter card number: 4388576018402626
4388576018402626 is NOT a valid credit card number
Enter card number: 4388576018410707
4388576018410707 is a valid credit card number
Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...
Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit 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...
Credit card numbers adhere to certain constraints. First, a valid credit card number must have between 13 and 16 digits. Second, it must start with one of a fixed number of valid prefixes : 1 • 4 for Visa • 5 for MasterCard • 37 for American Express • 6 for Discover cards In 1954, Hans Peter Luhn of IBM proposed a “checksum” algorithm for validating credit card numbers . 2 The algorithm is useful to determine whether a card...
Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...
I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...
Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...
REQUIREMENTS: Write a Graphical User Interface (GUI) program using JavaFX that prompts the user to enter a credit card number. Display whether the number is valid and the type of credit cards (i.e., Visa, MasterCard, American Express, Discover, and etc). The requirements for this assignment are listed below: • Create a class (CreditNumberValidator) that contains these methods: // Return true if the card number is valid. Boolean isValid(String cardNumber); // Get result from Step 2. int sumOfDoubleEvenPlace(String cardNumber); // Return...
Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...
Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you: Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10. For example, consider the number 4012 8888 8888 1881. The...
2.Consider the following credit card numbers and tell me if they are valid or not. If a number is invalid, write the valid credit number by changing the “check” digit. Show your work for full marks. a. 4519 6731 6055 4166 b. 4515 1663 4722 3333 c. How many valid (according to the Luhn check) 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 45? Give one of the valid credit card numbers. 3....
The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card number: 3056 9309 0259 04\n Checksum: 50\n Card status: VALID\n Credit card number: 3852 0000 0232 37\n Checksum: 40\n Card status: VALID\n Credit card number: 6011 1111 1111 1117\n Checksum: 30\n Card status: VALID\n Credit card number: 6011 0009 9013 9424\n Checksum: 50\n Card status: VALID\n Credit card number: 3530 1113 3330 0000\n Checksum: 40\n Card status: VALID\n Credit card number: 3566 0020 2036...