WRITE A JAVA PROGRAM: The last digit of a credit card number is the check digit, which protects against 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. Following 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 1 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.
main.java
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter the 8 digit credit card number: ");
int num = sc.nextInt();
String checkNumber = String.valueOf(num);
if(checkNumber.length() == 8){
String allNumbers [] = checkNumber.split("");
int a = Integer.parseInt(allNumbers[0]);
int b = Integer.parseInt(allNumbers[1]);
int c = Integer.parseInt(allNumbers[2]);
int d = Integer.parseInt(allNumbers[3]);
int e = Integer.parseInt(allNumbers[4]);
int f = Integer.parseInt(allNumbers[5]);
int g = Integer.parseInt(allNumbers[6]);
int h = Integer.parseInt(allNumbers[7]);
//First Step mentioned in the question
int total = b + d + f + h;
//Second Step mentioned in the question
int doubleA = a + a;
int doubleC = c + c;
int doubleE = e + e;
int doubleG = g + g;
int sum=0,sum1=0,sum2=0,sum3=0;
for(sum=0 ;doubleA!=0 ;doubleA/=10)
{
sum+=doubleA%10;
}
for(sum1=0 ;doubleC!=0 ;doubleC/=10)
{
sum1+=doubleC%10;
}
for(sum2=0 ;doubleE!=0 ;doubleE/=10)
{
sum2+=doubleE%10;
}
for(sum3=0 ;doubleG!=0 ;doubleG/=10)
{
sum3+=doubleG%10;
}
int correctBit = sum + sum1 + sum2 + sum3;
int finalTotal = total + correctBit;
if (finalTotal%10 == 0){
System.out.println("Credit card is valid");
}
else{
System.out.println("Credit card is invalid");
}
}
else{
System.out.println("Try again....Please enter 8 digit credit card number to proceed");
System.exit(0);
}
}
}
Output:
Get Answers For Free
Most questions answered within 1 hours.