Question

In: Computer Science

Credit Cards Credit card numbers follow patterns. Cards must have 13 - 16 digits (inclusive) Card...

Credit Cards

Credit card numbers follow patterns.

  1. Cards must have 13 - 16 digits (inclusive)
  2. Card numbers must start with the number(s):
    • 4 for Visa cards
    • 5 for MasterCard cards
    • 37 for American Express cards
    • 6 for Discover cards

Given that a number has satisfied the above criteria A and B the following steps are taken to validate the number:

  1. Double every second digit from right to left.  If doubling the digit results in a two-digit number, add the two digits to get a single digit number
  2. Add all of the single digit numbers from step 1 above
  3. Add all of the digits in the odd places from right to left in the card number
  4. Sum the results from steps 2 and 3 above
  5. If the result from step 4 is evenly divisible by 10 the card number is valid.

Given the number:

4388576018402626

The steps 1 - 5 above would be (after determining that A an B above have been satisfied):

Step 1:

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

Step 2:

4 + 4 + 8 + 2 + 3 + 1 + 7 + 8 = 37

Step 3:

6 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 38

Step 4:

37 + 38 = 75

Step 5:

75 % 10

You are to write a program that will prompt the user to enter a card number.  You are then to determine if the number entered is valid or not.  You are then to print to the screen if the number entered is a valid credit card number or not.

Your are to complete this program using functions. You are to have a function main in which you will only call the functions you have developed.  The last line of your code is to be a call to main as below:

main()

Here are some valid numbers for testing purposes:

American Express

378282246310005

American Express

371449635398431

Discover

6011111111111117

Discover

6011000990139424

MasterCard

5555555555554444

MasterCard

5105105105105100

Visa

4111111111111111

Visa

4012888888881881

Visa

4222222222222

Done in Python Format please

Solutions

Expert Solution

Solution ::

The solution for the above question is given below with the screenshot of output.

---------------------------------------------------------------------------------------------------------------

I have kept the logic simple and output as per the question.

I have used to numbers given in question:

  • American Express 378282246310005
  • American Express 371449635398431
  • Discover 6011111111111117
  • Discover 6011000990139424
  • MasterCard 5555555555554444
  • MasterCard 5105105105105100
  • Visa 4111111111111111
  • Visa 4012888888881881
  • Visa 4222222222222

If there is anything else do let me know in comments

---------------------------------------------------------------------------------------------------------------

--------------- CODE TO COPY -----------------------------------------------------------------------

def main():
  # user will input the credit card number as a string
  # call the function isValid() and print whether the credit card number is
  # valid or not valid
  
  num = input("Enter how many number you want to check ?")
  
  for i in range( 0 , int(num) ):
    
    card_no = input("\nEnter a credit card number as a long integer: ")
    
    if ( isValid( card_no )):
      print( str(card_no) + " is valid.")
    else:
      print( str(card_no) + " is invalid.")
  

def isValid(number) -> bool:
  # Return true if the card number is valid
  # hint you will have to call function sumOfDoubleEvenPlace() and sumOfOddPlace
  
  # It must have between 13 and 16 digits
  if ( len(number) < 13 or  len(number) > 16  ):
    return False
    
  # It must startswith 4,5,6 0r 37
  if ( ( number.startswith("4") == False) and ( number.startswith("5") == False) and ( number.startswith("6") == False) and ( number.startswith("37") == False) ):
    return False
  
  sum_of_even = sumOfDoubleEvenPlace(number)
  sum_of_odd  = sumOfOddPlace(number)
  
  sum_even_odd = sum_of_even + sum_of_odd
  
  if ( sum_even_odd %10 == 0 ):
    return True
    
  return False
  

def sumOfDoubleEvenPlace(number) -> int:
  # Get the result from Step 2
  
  result = 0 
  
  for i in range( len(number) - 2, -1 , -2):
    
    num = int ( number[i] ) * 2
    
    num = getDigit( num )
    
    result += num
  
  return result

def getDigit(number) -> int:
  # Return this number if it is a single digit, otherwise, return
  # the sum of the two digits
  if ( number > 9 ):
      number = int( str(number)[0] ) + int( str(number)[1] ) 
      
  return number

def sumOfOddPlace(number)-> int:
  # Return sum of odd place digits in number
  # Here are the two sample runs using the credit card numbers above:
  result = 0 
  
  for i in range( len(number) -1 , -1 , -2):
    
    num = int ( number[i] )
    result += num
    
  return result

print("Credit card number validation Program")
print()
main()

---------------------------------------------------------------------------------------------------------------

Output :

Please rate my answer. Thank you..............


Related Solutions

Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375...
Credit card numbers typically consist of 13, 15, or 16 digits. For example, 4690 3582 1375 4657 is a hypothetical credit card number. The first digit designates the system. In (3.1.1), the first digit, 4, shows that the card would be a Visa card. The following digits specify other information such as the account number and bank number. (The precise meaning depends on the type of card.) The last digit it special; it is computed from the previous digits and...
Many types of identification numbers, including credit card numbers, must satisfy the Luhn Algorithm in order...
Many types of identification numbers, including credit card numbers, must satisfy the Luhn Algorithm in order to be considered "valid". This algorithm verifies the number by performing the following operation: starting from the right-most digit, double every 2nd digit. If this doubling causes that digit to be greater than 9, subtract 9 from it. Now add up all the new digits of the number (including the digits that weren't doubled). If the sum is evenly divisible by 10, then the...
An individual has Centurion Card from American Express (credit cards). The credit card is most valuable...
An individual has Centurion Card from American Express (credit cards). The credit card is most valuable credit card in the world. Can this individual claim that he/she is a wealthy individual?
In poker, there is a 52 card deck with 4 cards each of each of 13...
In poker, there is a 52 card deck with 4 cards each of each of 13 face values. A full house is a hand of 5 cards with 3 of one face value, and 2 of another. What is the probability that a random poker hand is a full house? You can leave your answer in terms of bionomial co-efficients and similar factors, but please explain each term.
A game comes with a deck of cards with each card showing one of the numbers...
A game comes with a deck of cards with each card showing one of the numbers 2, 3, 4, 5, 6, or 8. Each card occurs one time in red, one time in purple, and one time in blue. Given that a blue card is drawn, what is the probability the card is a 4?
Recording credit card and debit card sales Restaurants do a large volume of business with credit and debit cards. Suppose Summer,
Question Recording credit card and debit card sales Restaurants do a large volume of business with credit and debit cards. Suppose Summer,Sand, and Castles Resort restaurant had these transactions on January 28, 2018: National Express credit card sales $ 10,800 Value Card debit card sales 10,000. Requirements1. Suppose Summer, Sand, and Castles Resort’s processor charges a 2% fee and deposits sales net of the fee. Journalize these sales transactions for the restaurant.2. Suppose Summer, Sand, and Castles Resort’s processor charges...
write C# console app No arrays Credit card validation Problem. A credit card number must be...
write C# console app No arrays Credit card validation Problem. A credit card number must be between 13 and 16 digits. It must start with: –4 for visa cards –5 for mater cards –37 for American express cards –6 for discover cards •Consider a credit card number 4380556218442727 •Step1: 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. •Step2: now add...
Credit card usage in the United States is substantial. Many startup companies use credit cards as...
Credit card usage in the United States is substantial. Many startup companies use credit cards as a way to help meet short-term financial needs. The most common forms of debt for startups are use of credit cards and loans from relatives. Suppose that you start up Brothers Sandwich Shop. You invested your savings of $20,000 and borrowed $70,000 from your relatives. Although sales in the first few months are good, you see that you may not have sufficient cash to...
Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending...
Write a for loop in .java to display all numbers from 13 - 93 inclusive, ending in 3. • Write a for loop to display a string entered by the user backwards.
Simon recently received a credit card with an 13% nominal interest rate. With the card, he...
Simon recently received a credit card with an 13% nominal interest rate. With the card, he purchased an Apple iPhone 5 for $460. The minimum payment on the card is only $20 per month. If Simon makes the minimum monthly payment and makes no other charges, how many months will it be before he pays off the card? Do not round intermediate calculations. Round your answer to the nearest month.   month(s) If Simon makes monthly payments of $60, how many...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT