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.

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.

public class AccountNumberchecker { /** * @param args */ public static void main(String[] args) { String acc_no="17327"; //take the card number int sum=0; //initialize sum as zero int a=acc_no.length(); //check out the length for(int j=0;j<a;j++ ) { if(j%2!=0) // this is just to build the logic as only alternate postion's element will be processed f(d1),f(d3).... { sum=sum calculatesum((acc_no.charAt(j))); } else { //alternate numbers will be added as it is sum=sum (int)(acc_no.charAt(j)-48); } } //Checking the card status System.out.println("The card no is " Checksum(sum)); } private static int calculatesum(char a) { int b1=a-48; //Convert the character to integer int b=b1*2; //Multiplied by 2 as per logic if (b>10) { //if greater than zero then add two digit return implfunc(b); } else { //if it is not greater than zero then return as it is return b; } } private static int implfunc(int b) { //Add the two digits of a two digit number return ((b%10)+ (b/10)); } private static String Checksum(int a) { //As per logic the mod operator will be implemented on //the summation if (a%10==0) return "Valid"; else return "invalid"; } }We are done!!

How To Check Credit Card Or Debit Card Number In Java
Reviewed by Animesh Chatterjee
on
March 14, 2013
Rating:

## No comments: