KeeganT

Luhn Algorithm

Feb 26th, 2019
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.73 KB | None | 0 0
  1. package luhnalgorithm;
  2. import java.util.Scanner;
  3.  
  4. public class LuhnAlgorithm
  5. {
  6.     public static void main(String[] args)
  7.     {
  8.         Scanner sc=new Scanner(System.in);
  9.         System.out.print("Enter an ID: ");
  10.         String id=sc.nextLine();
  11.         int sum=0;
  12.         boolean alt=false;
  13.         for(int c=id.length()-1;c>=0;c--)
  14.         {
  15.             int temp=Integer.parseInt(id.substring(c,c+1));
  16.             if(alt==true)
  17.             {
  18.                 temp*=2;
  19.                 if(temp>9)temp-=9;
  20.                 sum+=temp;
  21.             }
  22.             else sum+=temp;
  23.             alt=!alt;
  24.         }
  25.         if(sum%10==0)System.out.println("ID is valid.");
  26.         else System.out.println("ID is invalid.");
  27.     }    
  28. }
Advertisement
Add Comment
Please, Sign In to add comment