Luhn algorithm- credit card

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); } 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 ((arr.length - i) % 2 == 0)// 15%2!=0-->no//14%2==0//12%2==0 { 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."); } } }

Mar 12, 2025 - 15:07
 0
Luhn algorithm- credit card

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.

Image description

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 ((arr.length - i) % 2 == 0)// 15%2!=0-->no//14%2==0//12%2==0
            {
                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.");
        }
    }

}