title: Validate credit card number using luhn algorithm
body: <p>I have a question regarding the following programming assignment.</p>
  
  <p>Credit card numbers follow certain patterns. A credit card must have between 13 and 16 digits. It must  start with:</p>
  
  <p>•   4 for Visa cards</p>
  
  <p>•   5 for Master cards</p>
  
  <p>•   37 for American Express cards</p>
  
  <p>In 1954, Hans Luhn of IBM proposed an algorithm for validating credit card numbers. The algorithm is useful to determine if a card number is entered correctly or if a credit card is scanned correctly by a scanner. Almost all credit card numbers are generated following this validity check, commonly know as the Luhn check or the Modulus 10 check, which can be described as follows. For illustration, consider the card number 4388576018402625.</p>
  
  <ol>
  <li>Double every second digit from right to left. If doubling of a digit results in a 2-digit number, add up the two digits to get a single-digit number.</li>
  </ol>
  
  <p>2 x 2 = 4</p>
  
  <p>2 x 2 = 4</p>
  
  <p>4 x 2 = 8</p>
  
  <p>1 x 2 = 2</p>
  
  <p>6 x 2 = 12  (1+2= 3)</p>
  
  <p>5 x 2 = 10  (1+0= 1)</p>
  
  <p>8 x 2 = 16  (1+6= 7)</p>
  
  <p>4 x 2 = 8</p>
  
  <ol>
  <li><p>Add all the single digit numbers from step 1
  4 + 4 +8 + 2 +3 + 1 + 7 + 8 = 37</p></li>
  <li><p>Add all digits in the odd places from right to left in the card number</p></li>
  </ol>
  
  <p>5 + 6 + 0 + 8 + 0 + 7 + 8 + 3 = 37</p>
  
  <ol>
  <li><p>Sum the results from step 2 and step 3
  37 + 37 = 74</p></li>
  <li><p>If the result from step is divisible by 10, the card number is valid; otherwise, it’s invalid. For example, the number 4388576018402625 is invalid, but the number 4388576018410707 is a valid Visa card; the number 6011000593748745 is invalid, but the number 6011000593748746 is a valid Discover card.</p></li>
  </ol>
  
  <p>I tried to solve it as shown in the following code: </p>
  
  <pre><code>import java.util.Scanner;
  
  public class CreditCardValidation {
  
      public static boolean isValid(long number) {
  
          int total = sumOfDoubleEvenPlace(number) + sumOfOddPlace(number);
  
  
          if ((total % 10 == 0) &amp;&amp; (prefixMatched(number, 1) == true) &amp;&amp; (getSize(number)&gt;=13 ) &amp;&amp; (getSize(number)&lt;=16 )) {
              return true;
          } else {
              return false;
          }
      }
  
      public static int getDigit(int number) {
  
          if (number &lt;= 9) {
              return number;
          } else {
              int firstDigit = number % 10;
              int secondDigit = (int) (number / 10);
  
              return firstDigit + secondDigit;
          }
      }
      public static int sumOfOddPlace(long number) {
          int result = 0;
  
          while (number &gt; 0) {
              result += (int) (number % 10);
              number = number / 100;
          }
  
          return result;
      }
  
      public static int sumOfDoubleEvenPlace(long number) {
  
          int result = 0;
          long temp = 0;
  
          while (number &gt; 0) {
              temp = number % 100;
              result += getDigit((int) (temp / 10) * 2);
              number = number / 100;
          }
  
          return result;
      }
  
      public static boolean prefixMatched(long number, int d) {
  
          if ((getPrefix(number, d) == 4)
                  || (getPrefix(number, d) == 5)
                  || (getPrefix(number, d) == 3)) {
  
              if (getPrefix(number, d) == 3) {
                  System.out.println("\nVisa Card ");
              } else if (getPrefix(number, d) == 5) {
                  System.out.println("\nMaster Card ");
              } else if (getPrefix(number, d) == 3) {
                  System.out.println("\nAmerican Express Card ");
              }
  
              return true;
  
          } else {
  
              return false;
  
          }
      }
  
      public static int getSize(long d) {
  
          int count = 0;
  
          while (d &gt; 0) {
              d = d / 10;
  
              count++;
          }
  
          return count;
  
      }
  
      public static long getPrefix(long number, int k) {
  
          if (getSize(number) &lt; k) {
              return number;
          } else {
  
              int size = (int) getSize(number);
  
              for (int i = 0; i &lt; (size - k); i++) {
                  number = number / 10;
              }
  
              return number;
  
          }
  
      }
  
      public static void main(String[] args) {
  
          Scanner sc = new Scanner(System.in);
  
          System.out.print("Enter a credit card number as a long integer: ");
  
          long input = sc.nextLong();
  
  
          if (isValid(input) == true) {
              System.out.println("\n" + input + " is Valid. ");
          } else {
              System.out.println("\n" + input + " is Invalid. ");
          }
  
      }
  } 
  </code></pre>
  
  <p>My question is how can I use an array to store the credit card number instead of using a long number.</p>