Luhna
unknown
java
3 years ago
764 B
9
Indexable
// Luhn algorithm
import java.io.*;
public class GFG {
static boolean checkLuhn(String cardNo)
{
int nDigits = cardNo.length();
int nSum = 0;
boolean isSecond = false;
for (int i = nDigits - 1; i >= 0; i--)
{
int d = cardNo.charAt(i) - '0';
if (isSecond == true)
d = d * 2;
nSum += d / 10;
nSum += d % 10;
isSecond = !isSecond;
}
return (nSum % 10 == 0);
}
static public void main (String[] args)
{
String cardNo = "1234567";
if (checkLuhn(cardNo))
System.out.println("This is a valid card");
else
System.out.println("This is not a valid card");
}
}Editor is loading...