Question

Write java program to check that a (16-digit) credit card number is valid. A valid credit...

Write java program to check that a (16-digit) credit card number is valid. A valid credit card number will yield a result divisible by 10 when you:

Form the sum of all digits. Add to that sum every second digit, starting with the second digit from the right. Then add the number of digits in the second step that are greater than four. The result should be divisible by 10.

For example, consider the number 4012 8888 8888 1881. The sum of all digits is 89. The sum of the bold digits is 46. There are five bold digits larger than four, so the result is 140. 140 is divisible by 10 so the card number is valid.

    • Along the way, print out the step results:
      • sum of all digits
      • that result plus sum of every second digit
      • that result plus sum of every other digit greater than 4
      • divisible by 10 answer
    • print to the screen the original number & the final answer
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

import java.util.Scanner;
public class Main
{
   public static void main(String[] args) {
  
Scanner sc = new Scanner(System.in);      
       System.out.println("Enter 16-digit credit card number: ");
      
       //inut of credit card number
       String str = sc.nextLine();
  
//calculating sum of digits      
       int sum_of_digits = 0;
       for(int i=0;i<str.length();i++){
       sum_of_digits += Integer.parseInt(str.charAt(i)+"");
       }
      
       //calculating sum of digits of odd places
       int sum_of_odd_place_digits = 0;
       for(int i=str.length()-2;i>=0;i-=2){
       sum_of_odd_place_digits += Integer.parseInt(str.charAt(i)+"");
       }
      
       //counting number of digits grater than 4 at odd places
       int count_of_odd_place_digits_greater_than_4 = 0;
       for(int i=str.length()-2;i>=0;i-=2){
       if(Integer.parseInt(str.charAt(i)+"") > 4)
       count_of_odd_place_digits_greater_than_4++;
       }
      
       //taking total sum
       int sum = sum_of_digits+sum_of_odd_place_digits+count_of_odd_place_digits_greater_than_4;
       System.out.println("sum of all digits : "+sum_of_digits);
      
       //printing desired details
       int sum_ = sum_of_digits+sum_of_odd_place_digits;
       System.out.println("result plus sum of every second digit : "+sum_);
       System.out.println("result plus sum of every other digit greater than 4 is : "+sum);
      
       if(sum%10 == 0){
       System.out.println("Valid credit card");
       }
       else{
       System.out.println("Invalid credit card");
       }
   }
}

SCREENSHOT:

OUTPUT:

Add a comment
Know the answer?
Add Answer to:
Write java program to check that a (16-digit) credit card number is valid. A valid credit...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit...

    Validating Credit Card Numbers Write a program named Creditcard.java that prompts the user for a credit card number and determines whether it is valid or not. (Much of this assignment is taken from exercise 6.31 in the book) Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits, and must start with: 4 for Visa cards 5 for Master cards 6 for Discover cards 37 for American Express cards The algorithm for determining...

  • Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns:...

    Program Set 2 10 points) Credit card number validation Program Credit card numbers follow certain patterns: It must have between 13 and 16 digits, and the number must start with: 4 for Visa cards 5 for MasterCard credit cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or whether a credit card...

  • Credit card numbers follow certain patterns. A credit card number must have between 13 and 16...

    Credit card numbers follow certain patterns. A credit card number must have between 13 and 16 digits. The number must start with the following: 4 for Visa cards 5 for MasterCard cards 37 for American Express cards 6 for Discover cards In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine whether a card number is entered correctly or is scanned correctly by a scanner. Almost all credit card numbers...

  • The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card...

    The Language is for Java ------ Input ------- credit-cards-2.dat //////////////////// Output//////////// Enter a filename\n Credit card number: 3056 9309 0259 04\n Checksum: 50\n Card status: VALID\n Credit card number: 3852 0000 0232 37\n Checksum: 40\n Card status: VALID\n Credit card number: 6011 1111 1111 1117\n Checksum: 30\n Card status: VALID\n Credit card number: 6011 0009 9013 9424\n Checksum: 50\n Card status: VALID\n Credit card number: 3530 1113 3330 0000\n Checksum: 40\n Card status: VALID\n Credit card number: 3566 0020 2036...

  • I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the...

    I want it in C++ 4. When choice 4 (Verify Your Credit Card) is selected, the program should read your credit card (for example: 5278576018410787) and verify whether it is valid or not. In addition, the program must display the type (i.e. name of the company that produced the card). Each credit card must start with a specific digit (starting digit: 1st digit from left to ight), which also is used to detemine the card type according Table 1. Table...

  • Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program...

    Please answer in Visual Studio 2019 c# format. Not python. Thank you. Q. Write a program that works as described in the following scenario: The user enters a credit card number. The program displays whether the credit card number is valid or invalid. Here are two sample runs: Enter a credit card number as a long integer: 4388576018410707 4388576018410707 is valid Enter a credit card number as a long integer: 4388576018402626 4388576018402626 is invalid To check the validity of the...

  • Banks issue credit cards with 16 digit numbers. If you've never thought about it before you...

    Banks issue credit cards with 16 digit numbers. If you've never thought about it before you may not realize it, but there are specific rules for what those numbers can be. For example, the first few digits of the number tell you what kind of card it is - all Visa cards start with 4, MasterCard numbers start with 51 through 55, American Express starts with 34 or 37, etc. Automated systems can use this number to tell which company...

  • How many valid 16-digit credit card numbers are there that start with these 14 digits 4444...

    How many valid 16-digit credit card numbers are there that start with these 14 digits 4444 4444 4444 45 (according to the Luhn check)? Give one of the valid credit card numbers.

  • Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type...

    Using the above described algorithm, create a program that: (IN PYTHON) 1.Asks the user which type of credit card he/she would like to find the checksum for. 2. Based on the user's choice of credit card, asks the user for the n digits of the credit card. [Get the input as a string; it's easier to work with the string, so don't convert to an integer.] 3. Using the user's input of the n digits, finds the last digit of...

  • Write in java . I started it can you finish it. I will rate if code...

    Write in java . I started it can you finish it. I will rate if code works What is this lab about and what will you be working on? In this lab, you will practice new tools and concepts you've learned in class and in lab. Namely this lab will be on methods, arrays (1D and 2D), and on repetition (using loops) In this lab, you will have to complete two activities. Here is what you have to do: Activity...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT