Question
Write a c ++ program . Q1. Modified Programming Exercise 6.37 (page 260) (20 marks) ( Financial: credit card number validation, loop, char to digit
Write a c ++ program . Q1. Modified Programming Exercise 6.37 (page 260) (20 marks)
( Financial: credit card number validation, loop, char to digit conversion, digits extraction from a number )
Credit card numbers follow certain patters. A credit card must have between 13 to 16 digits. The number must starts with the following (prefix):
4 for visa cards
5 for MasterCard cards
37 for American Express cards
6 for Discover cards
In 195 4, Hans Luhn of IBM proposed an algorithm (known as Luhn check or the Mod 10 check ) for validating credit card numbers. For illustration, given a card number: 4388576018402626
a) Double every second digit (highlighted below with green shade) 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.
4
3
8
8
5
7
6
0
1
8
4
0
2
6
2
6
2 x 2 = 4
2 x 2 = 4
4 x 2 = 8
1 x 2 = 2
6 x 2 = 12 (1 + 2 = 3)
5 x 2 = 10 (1 + 0 = 1)
8 x 2 = 16 (1 + 6 = 7)
4 X 2 = 8
b) Now add all single - digit numbers from Step a).
4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37
c) Add all digits in the odd places from right to left in the card number.
6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38
d) Sum the results from Step b) and Step c).
37 + 38 = 75
e) If the result from Step d) is divisible by 10, the card number is valid; otherwise, it is invalid. For example, the above number is inva lid. You may check your own credit card numbers to verify that they are valid.
Write a program that prompts the user to enter a credit card number as a string. Display whether the number is valid. Design your program to use the following functions:
// Return true if the card number is valid
bool isValid( const string& cardNumber )
// Get the result from Step b)
int sumOfDoubleEvenPlace( const string& cardNumber )
// Return this number if it is a single digit, otherwise
// return the sum of two digits
int getDigit( int number )
// Return sum of odd - place digit in the card number
int sumOfOddPlace( const string& cardNumber )
// Return true if substr is the p refix for card number
bool startWith( const string& cardNumber, const string& substr )
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started