Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package luhnalgorithm;
- import java.util.Scanner;
- public class LuhnAlgorithm
- {
- public static void main(String[] args)
- {
- Scanner sc=new Scanner(System.in);
- System.out.print("Enter an ID: ");
- String id=sc.nextLine();
- int sum=0;
- boolean alt=false;
- for(int c=id.length()-1;c>=0;c--)
- {
- int temp=Integer.parseInt(id.substring(c,c+1));
- if(alt==true)
- {
- temp*=2;
- if(temp>9)temp-=9;
- sum+=temp;
- }
- else sum+=temp;
- alt=!alt;
- }
- if(sum%10==0)System.out.println("ID is valid.");
- else System.out.println("ID is invalid.");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment