The Luhn algorithm doesn’t have a formula in the conventional mathematical sense. Instead, it’s made up of a series of steps.
Step 1: Starting from the right, double the value of the second-to-last digit and continue doing the same for every second digit. If the result of any doubling operation is greater than nine, then add the digits of the result together to obtain a single-digit number.
Example: 6 × 2 = 12; 1 + 2 = 3
Step 2: Find the sum of all the digits that you didn’t double and the new values that you got from doubling.
Step 3: Determine if the total sum is a multiple of ten. According to the Luhn algorithm, the number is considered to be valid if the total ends in zero.
For example, let’s verify the number 79927398713 using the Luhn algorithm formula.
Double every second digit from the right:
1 x 2 = 2
8 x 2 = 16 (1 + 6 = 7)
3 x 2 = 6
2 x 2 = 4
9 x 2 = 18 (1 + 8 = 9)
Add all of the digits together, including the undoubled digits:
7 + 9 + 9 + 4 + 7 + 6 + 9 + 7 + 7 + 2 + 3 = 70
The number 79927398713 is valid according to the Luhn algorithm because the result is 70, which is a multiple of 10.
These steps form the “formula” or procedure that the Luhn algorithm follows to validate or generate numbers.
package String_start;import java.util.Scanner;public class crerid_luhn_algo {public static void main(String[] args) {Scanner sc = new Scanner(System.in);System.out.println("Enter 16 digit number");String cardNo = sc.nextLine();if (cardNo.length() == 16) {validate_card(cardNo);} else {System.out.println("Enter valid 16 digit number");String cardNo1 = sc.nextLine();if (cardNo1.length() == 16) {validate_card(cardNo1);} elseSystem.out.println("Try again");}}private static void validate_card(String cardNo) {int[] arr = new int[16];int num = 0;int mul = 0;for (int i = 0; i < arr.length; i++) {arr[i] = cardNo.charAt(i) - '0';// ex:'4' - '0' gives 52 - 48 = 4// ingeter convertion}for (int i = arr.length - 1; i >= 0; i--) {if (i % 2 == 0)// 15%2!=0-->no//14%2==0//12%2==0// for right to left{mul = arr[i] * 2;// 14//12//10if (mul > 9) {mul = mul % 10 + mul / 10;// seprate numnum = num + mul;} else// (mul < 9){num = num + mul;}} else // 15%2!=0//13%2!=0{num = num + arr[i];}}System.out.println(num);if (num % 10 == 0) {System.out.println("Valid card number.");} else {System.out.println("Invalid card number.");}}}package String_start; import java.util.Scanner; public class crerid_luhn_algo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter 16 digit number"); String cardNo = sc.nextLine(); if (cardNo.length() == 16) { validate_card(cardNo); } else { System.out.println("Enter valid 16 digit number"); String cardNo1 = sc.nextLine(); if (cardNo1.length() == 16) { validate_card(cardNo1); } else System.out.println("Try again"); } } private static void validate_card(String cardNo) { int[] arr = new int[16]; int num = 0; int mul = 0; for (int i = 0; i < arr.length; i++) { arr[i] = cardNo.charAt(i) - '0';// ex:'4' - '0' gives 52 - 48 = 4// ingeter convertion } for (int i = arr.length - 1; i >= 0; i--) { if (i % 2 == 0)// 15%2!=0-->no//14%2==0//12%2==0// for right to left { mul = arr[i] * 2;// 14//12//10 if (mul > 9) { mul = mul % 10 + mul / 10;// seprate num num = num + mul; } else// (mul < 9) { num = num + mul; } } else // 15%2!=0//13%2!=0 { num = num + arr[i]; } } System.out.println(num); if (num % 10 == 0) { System.out.println("Valid card number."); } else { System.out.println("Invalid card number."); } } }package String_start; import java.util.Scanner; public class crerid_luhn_algo { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter 16 digit number"); String cardNo = sc.nextLine(); if (cardNo.length() == 16) { validate_card(cardNo); } else { System.out.println("Enter valid 16 digit number"); String cardNo1 = sc.nextLine(); if (cardNo1.length() == 16) { validate_card(cardNo1); } else System.out.println("Try again"); } } private static void validate_card(String cardNo) { int[] arr = new int[16]; int num = 0; int mul = 0; for (int i = 0; i < arr.length; i++) { arr[i] = cardNo.charAt(i) - '0';// ex:'4' - '0' gives 52 - 48 = 4// ingeter convertion } for (int i = arr.length - 1; i >= 0; i--) { if (i % 2 == 0)// 15%2!=0-->no//14%2==0//12%2==0// for right to left { mul = arr[i] * 2;// 14//12//10 if (mul > 9) { mul = mul % 10 + mul / 10;// seprate num num = num + mul; } else// (mul < 9) { num = num + mul; } } else // 15%2!=0//13%2!=0 { num = num + arr[i]; } } System.out.println(num); if (num % 10 == 0) { System.out.println("Valid card number."); } else { System.out.println("Invalid card number."); } } }
Enter fullscreen mode Exit fullscreen mode
Output:Invalid Ex
Enter 16 digit number
4532015112830367
51
Invalid card number.
Output:valid Ex
Enter 16 digit number
4532015112830366
50
Valid card number.
Check Date Month year
package String_start;import java.time.LocalDate;import java.util.Scanner;public class Time_date {static Scanner sc = new Scanner(System.in);static LocalDate date = LocalDate.now();public static void main(String[] args) {System.out.println(date);System.out.println("Enter year");int year = sc.nextInt();finddate(year);}private static void finddate(int year) {int cYear = date.getYear();if (cYear <= year) {System.out.println("card year valid = " + year);int cMonth = date.getMonthValue();System.out.println("Enter month");int month = sc.nextInt();if (cMonth <= month) {System.out.println("card month valid = " + month);int cDay = date.getDayOfMonth();System.out.println("Enter Date");int day = sc.nextInt();if (cDay <= day) {System.out.println("card date valid = " + date);} else {System.out.println("card year invalid");}} else {System.out.println("card month invalid");}} else {System.out.println("card date invalid");}}}package String_start; import java.time.LocalDate; import java.util.Scanner; public class Time_date { static Scanner sc = new Scanner(System.in); static LocalDate date = LocalDate.now(); public static void main(String[] args) { System.out.println(date); System.out.println("Enter year"); int year = sc.nextInt(); finddate(year); } private static void finddate(int year) { int cYear = date.getYear(); if (cYear <= year) { System.out.println("card year valid = " + year); int cMonth = date.getMonthValue(); System.out.println("Enter month"); int month = sc.nextInt(); if (cMonth <= month) { System.out.println("card month valid = " + month); int cDay = date.getDayOfMonth(); System.out.println("Enter Date"); int day = sc.nextInt(); if (cDay <= day) { System.out.println("card date valid = " + date); } else { System.out.println("card year invalid"); } } else { System.out.println("card month invalid"); } } else { System.out.println("card date invalid"); } } }package String_start; import java.time.LocalDate; import java.util.Scanner; public class Time_date { static Scanner sc = new Scanner(System.in); static LocalDate date = LocalDate.now(); public static void main(String[] args) { System.out.println(date); System.out.println("Enter year"); int year = sc.nextInt(); finddate(year); } private static void finddate(int year) { int cYear = date.getYear(); if (cYear <= year) { System.out.println("card year valid = " + year); int cMonth = date.getMonthValue(); System.out.println("Enter month"); int month = sc.nextInt(); if (cMonth <= month) { System.out.println("card month valid = " + month); int cDay = date.getDayOfMonth(); System.out.println("Enter Date"); int day = sc.nextInt(); if (cDay <= day) { System.out.println("card date valid = " + date); } else { System.out.println("card year invalid"); } } else { System.out.println("card month invalid"); } } else { System.out.println("card date invalid"); } } }
Enter fullscreen mode Exit fullscreen mode
Output:
2025-03-13
Enter year
2025
card year valid = 2025
Enter month
7
card month valid = 7
Enter Date
16
card date valid = 2025-03-13
暂无评论内容