Question

In: Computer Science

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

NOTE: the given card number 43589795 should produce a valid number

Solutions

Expert Solution

// Java program to check if a given credit
// card is valid or not.
import java.util.Scanner;

public class CreditCard {
   // Main Method
   public static void main(String[] args)
   {
       System.out.print("Enter 8 digit credit card cardNumber :");
       int cardNumber = new Scanner(System.in).nextInt();
       String num = cardNumber + "";
      
      
       int sumOfDoubleEvenPlace = 0;
       for (int i = num.length() - 2; i >= 0; i -= 2)
           sumOfDoubleEvenPlace += getDigit(Integer.parseInt(num.charAt(i) + "") * 2);
      
       // Return sum of odd-place digits in cardNumber
       int sumOfOddPlace = 0;
       for (int i = num.length() - 1; i >= 0; i -= 2)
           sumOfOddPlace += Integer.parseInt(num.charAt(i) + "");  
      
       //Return true if the card cardNumber is valid
       boolean isValid = (sumOfDoubleEvenPlace + sumOfOddPlace) % 10 == 0;
       System.out.println(cardNumber + " is " +
       (isValid? "valid" : "invalid"));
   }

   // Return this cardNumber if it is a single digit, otherwise,
   // return the sum of the two digits
   public static int getDigit(int cardNumber)
   {
       if (cardNumber < 9)
           return cardNumber;
       return cardNumber / 10 + cardNumber % 10;
   }
}


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...
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...
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...
Write a program IN JAVA that asks the user for a number. The program should check...
Write a program IN JAVA that asks the user for a number. The program should check the number to ensure that it is valid (if not, the user should enter a valid number to continue.) The program should print out all of the prime numbers from 2 up to the number, with up to 10 numbers per line. (Recall: A prime number is a number that is only divisible by itself and 1.) The code should ask the user if...
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...
write a java code. Consider a four digit number such as 6587. Split it at two...
write a java code. Consider a four digit number such as 6587. Split it at two digits, as 65 and 87. Sum of 65 and 87 is 152. Sum of the digits of 152 is 8. Given the starting and ending four digit numbers and a given target number such as 10, write a program to compute and print the number of instances where the sum of digits equals the target.
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.
In Java: "The program should be able to read a two-digit number d from the standard...
In Java: "The program should be able to read a two-digit number d from the standard input using Scanner class and outputs the second digit followed by a string literal "<-->" followed by the first digit. Run your program and make sure it prints 2<-->5 when d=52"
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT