Software Contest title----(third) credit card number verification

Source: Internet
Author: User

The topics are as follows

L (programming question) credit card number verification

When you enter a credit card number, there is no fear of losing the wrong loss. You don't have to worry about it, because not a random credit card number is legal, it has to be validated through the Luhn algorithm.

The process of the checksum:

1. Starting with the last digit of the card number, the inverse will add the odd digits (1, 3, 5, and so on).

2, starting from the last digit card number, reverse the number of even digits, first multiplied by 2 (if the product is two digits, then subtract 9), and then sum.

3, the sum of odd digits plus the sum of even digits, the result should be divisible by 10.

For example, the card number is: 5432123456788881

The odd, even digits (marked in red) Distribution: 5432123456788881

Odd digits and =35

The result of an even digit multiplied by 2 (some minus 9): 1 6 2 6 1 5 7 7, sum = 35.

Finally, the 35+35=70 can be divisible by 10, which determines that the checksum passes.

Please write a program that obtains the card number from the standard input and then determines if the checksum is passed. "Fail" is displayed by displaying: "Success".

For example, user input: 356827027232780

Program output: Successful


The code is as follows

Package com.bird.software;

Import Java.util.Scanner;

/**
 * @category Luhn algorithm verifies credit card account
 * @author Bird
 */public
class Test2 {public
	static void main ( String[] args) {
		System.out.println ("Please enter credit card account number");
		Scanner n = new Scanner (system.in);
		String num = N.nextline ();
		int o = odd (num);
		int e = even (num);
		if ((o+e)%10 = = 0)
			System.out.println ("Success");
		else
			System.out.println ("failed");
	}
	
	The addition of the public static int odd (String s) {//odd digits
		
		char[] array = S.tochararray ();
		int temp = 0;
		int sum = 0;	System.out.println (array.length);
		for (int i = i>= 0; i-=2) {
			temp = array[i]-' 0 ';		System.out.print (temp);
			sum = temp;
		}
		return sum;
	}
	
	public static int even (String s) {
		char[] array = S.tochararray ();
		int temp = 0;
		int sum = 0;
		
		for (int i = i >= 1; i-=2) {
			temp = array[i]-' 0 ';
			temp = temp * 2;
			if (Temp > 9)
				Temp-= 9;
			sum = temp;
		}
		return sum;
	}


The test effect program output is as follows

356406010024817     Success
358973017867744     success
356827027232781     failure
306406010024817     failure
358973017867754     Failure


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.