Question

In: Computer Science

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 4358 9795, then you form the sum 5 + 7 + 8 + 3 = 23.

• Double each of the digits that were 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 next-to-last one, yields 18 18 10 8. Adding all digits in these values yields 1 + 8 + 1 + 8 + 1 + 0 + 8 = 27.

• Add the sums of the two preceding steps. If the last digit of the result is 0, the number is valid. In our case, 23 + 27 = 50, so the number is valid. Write a program in Java that implements this algorithm. The user should supply an 8-digit number, and you should print out whether the number is valid or not. If it is not valid, you should print the value of the check digit that would make it valid.

Solutions

Expert Solution

Hi there,

I have solved the question according to the steps described above.

The Code:

import java.util.*;

public class creditCard {
   public static void main(String[] args) {
       int num,length=0;
       Scanner sc= new Scanner(System.in);   
       //loop till we get a valid length credit card
       do
       {
           System.out.print("Enter a 8 digit number : ");
           num = sc.nextInt();
           int temp=num;
           length=0;
           while(temp!=0)
           {
               length++;
               temp/=10;
           }  
           if(length!=8)
               System.out.println("Length is not 8. Please try again!!");
       }while(length!=8);
       //create a array to store the digits      
       int[] arr = new int[8];
       int temp=num;
       for(int i=7;i>=0;i--)
       {
           arr[i]=temp%10;
           temp/=10;
       }
       //calculate the value to check validity
       int sum=arr[7]+arr[5]+arr[3]+arr[1]+calc(arr[6])+calc(arr[4])+calc(arr[2])+calc(arr[0]);
       //check for validity
       if(sum%10==0)
           System.out.println("Valid credit card!!");
       else
       {
           int diff;
           if(arr[7] + (10-sum%10) > 9)
               diff=arr[7]-sum%10;
           else
               diff=arr[7]+(10-sum%10);  
           System.out.println("Invalid credit card!! The correct check digit is: "+ diff);  
       }
   }
   //function to calculate the sum of digits of a number
   private static int calc(int x){
       x*=2;
       int sum=0;
       while(x!=0)
       {
           sum+=x%10;
           x/=10;
       }
       return sum;  
   }
}            

The Code(Screenshot) :

The Output(Screenshot) :

Valid output:

Invalid output:

I hope you liked my answer. If you have any doubt, please do mention it in the comment section.

Happy Coding :)


Related Solutions

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