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 without utilizing arrays. 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

Code:

import java.util.*;
public 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 a=sc.nextInt();
   int sum_s=0, sum_d=0, sum=0, c=8;
   for(int i=a; i>0; i=i/10)
   {
   int d=i%10;
   if(c%2==0)
   sum_s = sum_s + d;
   else
   {

   for(int j=2*d;j>0;j=j/10)
   {
   int d1=j%10;
   sum_d = sum_d + d1;
   }
   }
   c--;
   }
   sum = sum_s + sum_d;
   if(sum % 10 == 0)
   System.out.println("Number is valid");
   else
   {
   System.out.println("Number is not valid");
   int k = a % 10;
   sum = sum - k;
   k = ((sum/10) + 1)*10 - sum;
   System.out.println("The check digit should be: "+k);    
   }
   }
}

Please refer to the screenshot of the code to understand the indentation of the code:

Output:

1.

2.

For any doubts or questions comment below.


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...
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...
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.   
Is 100202345X a valid ISBN number? If not, what would the correct check digit have to...
Is 100202345X a valid ISBN number? If not, what would the correct check digit have to be ? Solve the congruence 121x ≡ 5 mod 350.
The records of Check$mart Bank show that the average credit card balance of its customers is...
The records of Check$mart Bank show that the average credit card balance of its customers is $3,325 with a standard deviation of $1,500. Assume that the distribution of these credit card balances is approximately normal. (a) What is the probability that an account balance is less than $2,500? (b) What is the probability that an account balance is more than $5,000? (c) What is the probability that an account balance is between $3,000 and $4,000? (d) 99% of account balances...
A number is a Universal Product Code (UPC) if its last digit agrees with the following...
A number is a Universal Product Code (UPC) if its last digit agrees with the following computations: • The sum of the odd position digits (not including the last) is M. That is we add the first digit to the third digit to the fifth digit etc. • The sum of the even position digits (not including the last) is N. • c = (3M + N)%10. • If c = 0 then the check digit is 0. • If...
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the...
The number 52430 is a 5-digit number whereas 03201 is not a 5-digit number. Determine the number of 5-digit multiples of 5 that can be created using the digits 0 to 9, if digits may not be repeated
The number of credit card holders of a bank in two different cities (city - X...
The number of credit card holders of a bank in two different cities (city - X and city - Y) settling their excess withdrawal amounts in time without attracting interest follows binomial distribution. The manager (collections) of the bank feels that the proportion of the number of such credit card holders in the city - X is not different from the proportion of the number of such credit card holders in the city - Y. To test his intuition, a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT