The following checksum formula is widely used by banks and credit card companies
ID: 3651448 • Letter: T
Question
The following checksum formula is widely used by banks and credit card companies to validate legal account numbers:d0 + f(d1) + d2 + f(d3) + d4 + f(d5) + d6 + ... = 0 (mod 10)
The di are the decimal digits of the account number and f(d) is the sum of the decimal digits of 2d (for example, f(7) = 5 because 2 * 7 = 14 and 1 + 4 = 5). For example, 17327 is valid because 1 + 5 + 3 + 4 + 7 = 20, which is a multiple of 10. Implement the function f and write a program to take a 10-digit integer as a command-line argument and print a valid 11-digit number with the given integer as its first 10 digits and the checksum as the last digit.
Explanation / Answer
Please rate...
Program CreditCardCheckSum.java
====================================================
class CreditCardCheckSum
{
public static void main(String args[])
{
String n=args[0];
if(n.length()!=10)
{
System.out.println("Wrong number of digits entered");
System.exit(1);
}
int i,s=0;
for(i=n.length()-1;i>=0;i--)
{
if(i%2!=0)
{
s=s+f((2*Integer.parseInt(n.charAt(i)+""))+"");
}
if(i%2==0)s=s+Integer.parseInt(n.charAt(i)+"");
}
int c=0;
while(true)
{
if(s%10!=0)
{
c++;
s=s+c;
}
else break;
}
String fv=""+n+c;
System.out.println("The final number is: "+fv);
}
public static int f(String a)
{
if(a.length()==1)return Integer.parseInt(a);
if(a.length()==2)return (Integer.parseInt(a.charAt(0)+"")
+Integer.parseInt(a.charAt(1)+""));
return 0;
}
}
===========================================================
sample output:
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.