Final Project Credit Card Number Check The last digit of a credit card number is
ID: 3539294 • Letter: F
Question
Final Project 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. Step 1. Starting from the rightmost digit, form the sum of every other digit. For example, if the credit card number is 43589795, then you form the sum 5 + 7 + 8 + 3 = 23 Step 2. Double each of the digits that are not included in the preceding step. Add all digits of the resulting numbers. For example, with the number given above, doubling the digits, starting with the nextExplanation / Answer
I have written the by implementing your algoritham. I have tested with your creditcard information, It got worked perfectly. Please test with your inputs and validate the code. If you have any doubt on code, Please comment. Please rate me. Thanks
import java.util.Scanner;
public class CheckCreditCard {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
System.out.print("Enter Credit Card Number : ");
int creditCardNumber = scanner.nextInt();
processCreditCard(creditCardNumber);
}
public static void processCreditCard(int creditCardNumber){
if(creditCardNumber >= 100000000 || creditCardNumber < 10000000){
System.out.println("Invalid Credit Card Number");
return;
}
boolean isFirst = true;
int firstTotal = 0;
int secondTotal = 0;
int dummyCreditCardNumber = creditCardNumber;
int temp = 0;
while(dummyCreditCardNumber != 0){
temp = dummyCreditCardNumber%10;
dummyCreditCardNumber /= 10;
if(isFirst){
firstTotal +=temp;
isFirst = false;
}else{
temp *= 2;
if(temp >= 10){
while(temp != 0){
secondTotal += temp%10;
temp /= 10;
}
}else{
secondTotal += temp;
}
isFirst = true;
}
}
if((firstTotal + secondTotal) % 10 == 0){
System.out.println("Entered credit card number is valid......");
System.out.println("We have been processed your credit card.....");
}else{
System.out.println("Entered credit card number is not valid.....");
System.out.println("Please check your number and try again.....");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.