Code.GeekInterview.com
 
 

Java Code Base

 
Code Samples Java
 

Credit Card Number Verification


Code ResourceAuthor: srinivasaraobora  

Difficulty Level: Beginner

Published: 23rd Apr 2009   Read: 7669 times  

Filed in: Java
Add Comment


 

 

Sponsored Links


 

 
Code for checking a number which is Credit Card Number or not?
This is code is for checking the credit card no is correct or not
 


Sample Code
  1. class Credit{
  2.  
  3. public static void main(String a[]){
  4.  
  5.  
  6.  String cardNumber="xxxxxxxxxxxxxxxx";
  7.  
  8.        
  9.         int sum = 0;
  10.  
  11.         for(int i = cardNumber.length() - 1; i >= 0; i -= 2) {
  12.            
  13.             sum += Integer.parseInt(cardNumber.substring(i, i + 1));
  14.              
  15.                System.out.println(Integer.parseInt(cardNumber.substring(i, i + 1)));
  16.             if(i > 0) {
  17.                 int d = 2 * Integer.parseInt(cardNumber.substring(i - 1, i));        
  18.                  
  19.                 if(d > 9) d -= 9;
  20.                
  21.                 sum += d;
  22.                  
  23.             }
  24.         }
  25.        
  26.         System.out.println(sum % 10 == 0);
  27.     }
  28. }
  29.  
Copyright GeekInterview.com


Next Article: Multilevel Inheritance


 

Latest Code Samples

 

Popular Code Samples

 

Related Code Samples

 

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    



Popular Coders

# Coder NameHits
1. srinivasaraobora130745
2. iamdvr97694
3. venki_madesh35134
4. Raju18488
5. parmod kumar duhan15756
6. Kiran.jakkaraju14058
7. chowsys7457
8. Venkateswara Rao6476
9. ashish.cns6348
10. Vamshidhar Matam5428

Active Coders

Refined Tags