public class Credit { public static void main(String[] args) { // This reads some number from the terminal. // Don't wor

Business, Finance, Economics, Accounting, Operations Management, Computer Science, Electrical Engineering, Mechanical Engineering, Civil Engineering, Chemical Engineering, Algebra, Precalculus, Statistics and Probabilty, Advanced Math, Physics, Chemistry, Biology, Nursing, Psychology, Certifications, Tests, Prep, and more.
Post Reply
answerhappygod
Site Admin
Posts: 899603
Joined: Mon Aug 02, 2021 8:13 am

public class Credit { public static void main(String[] args) { // This reads some number from the terminal. // Don't wor

Post by answerhappygod »

Public Class Credit Public Static Void Main String Args This Reads Some Number From The Terminal Don T Wor 1
Public Class Credit Public Static Void Main String Args This Reads Some Number From The Terminal Don T Wor 1 (43.26 KiB) Viewed 28 times
Public Class Credit Public Static Void Main String Args This Reads Some Number From The Terminal Don T Wor 2
Public Class Credit Public Static Void Main String Args This Reads Some Number From The Terminal Don T Wor 2 (51.58 KiB) Viewed 28 times
Public Class Credit Public Static Void Main String Args This Reads Some Number From The Terminal Don T Wor 3
Public Class Credit Public Static Void Main String Args This Reads Some Number From The Terminal Don T Wor 3 (47.14 KiB) Viewed 28 times
public class Credit { public static void main(String[] args) { // This reads some number from the terminal. // Don't worry about this for now and just use the variable long number = Comp122.getLong ("Number: "); // do stuff here with the number System.out.println("VISA"); } }
Luhn's Algorithm So what's the secret formula? Well, most cards use an algorithm invented by Hans Peter Luhn of IBM. According to Luhn's algorithm, you can determine if a credit card number is (syntactically) valid as follows: 1. Multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together. 2. Add the sum to the sum of the digits that weren't multiplied by 2. 3. If the total's last digit is 0 (or, put more formally, if the total modulo 10 is congruent to o), the number is valid! Example That's kind of confusing, so let's try an example with David's Visa: 4003600000000014. 1. For the sake of discussion, let's first underline every other digit, starting with the number's second-to-last digit: 4003 6 0 0 0 0 0 0 0 0 0 14 Okay, let's multiply each of the underlined digits by 2:
Okay, let's multiply each of the underlined digits by 2: 1:2, 0:2, 0:2, 0:2, 0:2, 6:2, 0:2, 4.2 That gives us: 2, 0, 0, 0, 0, 12, 0, 8 1 1 Now let's add those products' digits (i.e., not the products themselves) together: 2+0+0+0+0+1+2+0+ 8 = 13 2. Now let's add that sum (13) to the sum of the digits that weren't multiplied by 2 (starting from the end): 13+ 4+ 0 + 0 + 0 + 0 + 0 + 3 + 0 = 20 3. Yup, the last digit in that sum (20) is a O, SO David's card is legit! So, validating credit card numbers isn't hard, but it does get a bit tedious by hand. Let's write a program.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!
Post Reply