Question

In: Computer Science

The last digit of a credit card number is the check digit, which protects againts transaction...

The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if the credit card is number is 43589795 then you form the sum 5+7+8+3 = 23 2  Double each digit that we have not included in the preceding step. Add all digits of resulting numbers. For example, with the number given above, doubling the digits starting with next to last one, yields 18, 18, 10, 8. Adding all digits in these values yield 1+8+1+8+1+0+8 = 27  Add sum of the two preceding steps. If the last digit of the result is zero, then the number is valid number Write a Java program that implements this algorithm (Designing your solution and perhaps wring the algorithm in pseudocode might be helpful). Your program should ask the user 8 digit credit card number and the printout if the credit card is valid or invalid card Grading Criteria: a) The correctness of your program/solution b) Variable naming (self describing) c) Identification of proper data type and constants (if applicable) d) Appropriate commenting and Indentation.

Solutions

Expert Solution

`Hey,

Note: Brother if you have any queries related the answer please do comment. I would be very happy to resolve all your queries.

import java.util.Scanner;

public class CreditCardCheck
{
public static void main(String args[])
{
String ns,n;
while(true)
{
  
System.out.print("Enter the 8 digit credit card number: ");
Scanner s=new Scanner(System.in);
ns=s.nextLine();
if(ns.length()==8)break;
else System.out.println("Wrong length, enter again");
}
n=String.format("%s %s",ns.substring(0,4),ns.substring(4,8));
//System.out.println(n);
int s=Integer.parseInt(n.charAt(1)+"")+Integer.parseInt(n.charAt(3)+"")+Integer.parseInt(n.charAt(6)+"")+Integer.parseInt(n.charAt(8)+"");

String s1=(2*Integer.parseInt(n.charAt(0)+""))+"";
String s2=(2*Integer.parseInt(n.charAt(2)+""))+"";
String s3=(2*Integer.parseInt(n.charAt(5)+""))+"";
String s4=(2*Integer.parseInt(n.charAt(7)+""))+"";

int t=0;
if(s1.length()==2)t=Integer.parseInt(s1.charAt(0)+"")+Integer.parseInt(s1.charAt(1)+"");
else t=Integer.parseInt(s1.charAt(0)+"");
if(s2.length()==2)t=t+Integer.parseInt(s2.charAt(0)+"")+Integer.parseInt(s2.charAt(1)+"");
else t=Integer.parseInt(s2.charAt(0)+"");
if(s3.length()==2)t=t+Integer.parseInt(s3.charAt(0)+"")+Integer.parseInt(s3.charAt(1)+"");
else t=Integer.parseInt(s3.charAt(0)+"");
if(s4.length()==2)t=t+Integer.parseInt(s4.charAt(0)+"")+Integer.parseInt(s4.charAt(1)+"");
else t=Integer.parseInt(s4.charAt(0)+"");

int t1=t+s;
String t2=t1+"";
if(t2.charAt(1)=='0')System.out.println("The number is valid");
else System.out.println("The number is not valid");
}
}

Kindly revert for any queries

Thanks.


Related Solutions

The last digit of a credit card number is the check digit, which protects against transcription...
The last digit of a credit card number is the check digit, which protects against 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 numbers with 8 digits instead of 16: 1.) Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 4358 9795, then you...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against transcription errors such as error in a single digit or switching two digits. The following method is used to verify actual credit card number but, for simplicity, we will describe it for numbers with 8 digits instead of 16: Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against 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 numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
Credit Card Number Check. The last digit of a credit card number is the check digit,...
Credit Card Number Check. The last digit of a credit card number is the check digit, which protects against 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 numbers with 8 digits instead of 16: • Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is...
Write a java program The last digit of a credit card number is the check digit,...
Write a java program The last digit of a credit card number is the check digit, which protects againts transaction errors. The following method is used to veryfy credit card numbers. For the simplicity we can assume that the credit card has 8 digits instead of 16. Follwing steps explains the algorithm in determining if a credit card number is a valid card.  Starting from the right most digit, form the sum of every other digit. For example, if...
Suppose that a check digit is assigned to a four-digit number by appending the remainder after...
Suppose that a check digit is assigned to a four-digit number by appending the remainder after division by 7. If the number 36806 has a single-digit error in the first position, determine the possibilities for the correct number.
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid...
Problem Description:A very simple algorithm (Luhn Algorithm) to check whether a credit card number is valid is shown below:We assume that there are 16 digits in a credit card number.Start from the first digit of the credit card number as in position 1; second digit as in position 2; so on until the last digit as in position 16;If a digit is in even numbered position, take the digit itself as its representative;If a digit is in odd numbered position,...
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
Identify precautions for accepting the types of payments: cash, check, credit card, and debit card.   
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...
Use attribute directives to display credit card logo based on the credit card number Use simplified...
Use attribute directives to display credit card logo based on the credit card number Use simplified rules as follows: 4 visa 5 mastercard 34 and 37 amex 30, 36, 38, 39 diners 60, 64, 65 discover
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT