Question

Please do it in c++ please show steps as much as possible to help me understand...

Please do it in c++ please show steps as much as possible to help me understand the code thank you

Credit Card error detection check -> Luhn Algorithm, (http://www.freeformatter.com/credit-card-number-generator-validator.html#cardFormats)

From the rightmost digit, which is the check digit, moving left, double the value of every second digit; if the product of this doubling operation is greater than 9 (e.g., 7 * 2 = 14), then sum the digits of the products (e.g., 10: 1 + 0 = 1, 14: 1 + 4 = 5).

Take the sum of all the digits. If the total modulo (Links to an external site.)Links to an external site. 10 is equal to 0 (if the total ends in zero) then the number is valid according to the Luhn formula; else it is not valid.

Assume an example of an account number "7992739871" that will have a check digit added, making it of the form 7992739871x:

Account number

7

9

9

2

7

3

9

8

7

1

x

Double every other

7

18

9

4

7

6

9

16

7

2

x

Sum of digits

7

9

9

4

7

6

9

7

7

2

=67

The check digit (x) is obtained by computing the sum of digits then computing 9 times that value modulo 10 (in equation form, (67 * 9 mod 10)). In algorithm form:

Compute the sum of the digits (67).

Multiply by 9 (603).

The last digit, 3, is the check digit.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Code in C++:

#include<iostream>
using namespace std;
int main()
{
   string accNum;
   cout << "Enter account number: ";
   cin >> accNum;
   int sum=0;
   //Iterate entire account number
   for(int i=0;i<accNum.length();i++)
   {
       //convert digit to number
       int digit= accNum.at(i) - '0';
       //double thedigits at odd positions
       if(i%2!=0)
       {
           //After doubling the digit if it is greater than 9 then remove 9 from it
           if(2*digit>9)
               sum = sum + (2*digit-9);
           //Else just add double of digit
           else
               sum=sum+2*digit;
       }
       //Add digits at even positions
       else
           sum=sum+digit;
   }
   //find last digit x which satisifies our conditions
   for(int x=0;x<10;x++)
   {
       //If sum+x divisible by 10 then print it and break
       if((sum+x)%10==0)
       {
           //output
           cout << "The last digit, "<< x <<", is the check digit."<<endl;
           break;
       }
   }
   return 0;
}

output screenshot: On multiple screens

Add a comment
Know the answer?
Add Answer to:
Please do it in c++ please show steps as much as possible to help me understand...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write the code in C++. This is all one question with multiple requirements. 1) Create...

    Please write the code in C++. This is all one question with multiple requirements. 1) Create an enumerated data type called CrdCard The types of Credit Cards to create are all 16 digits long except American Express: American Express (34 or 37), Visa (Starts with a 4), MasterCard (Starts with 51-55), Discover (Starts with 6011). 2) Create a function that takes in the enumerated type and returns a valid credit card number using the Luhn Algorithm (also called the Modulus...

  • The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card...

    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...

  • coding luhnDouble equations in haskell here is the question: DEFINING FUNCTIONS: Assignment Description: The Luhn algorithm...

    coding luhnDouble equations in haskell here is the question: DEFINING FUNCTIONS: Assignment Description: The Luhn algorithm is used to check bank card numbers for simple errors such as mistyping a digit, and proceeds as follows: consider each digit as a separate number; moving left, double every other number from the second last; subtract 9 from each number that is now greater than 9; add all the resulting numbers together; if the total is divisible by 10, the card number is...

  • Write in java . I started it can you finish it. I will rate if code...

    Write in java . I started it can you finish it. I will rate if code works What is this lab about and what will you be working on? In this lab, you will practice new tools and concepts you've learned in class and in lab. Namely this lab will be on methods, arrays (1D and 2D), and on repetition (using loops) In this lab, you will have to complete two activities. Here is what you have to do: Activity...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    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...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    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...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    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...

  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    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...

  • Write java program to check that a (16-digit) credit card number is valid. A valid credit...

    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...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    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...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT