I have a credit card verification that is giving me a little bit of trouble. I can get the program to verify everything but Amex. I'm only needing to verify Discover, Visa, MC, and Amex. It simply tells me Amex cards are invalid. I've used a few different websites to gather my test numbers just to make sure my numbers weren't bad. Any help is appreciated.
using System;
public class creditcardnumbercheck
{
static void Main(string[] args)
{
long number;
Console.Write("Enter credit card number: ");
number = Convert.ToInt64(Console.ReadLine());
if (isValid(number))
{
Console.WriteLine("{0} is a valid credit card", number);
}
else
{
Console.WriteLine("{0} is not a valid credit card", number);
}
}// end Main
/*return true if # is valid*/
public static bool isValid(long number)
{
int n;
long p;
p = getPrefix(number, 1);
if (p < 4)
{
return false;
}
if (p > 6)
{
if (getPrefix(number, 2) != 3)
{
return false;
}
}
n = getSize(number);
if (n < 13 || n > 16)
{
return false;
}
if ((sumOfDoubleEvenPlace(number) + sumOfoddPlace(number)) % 10 != 0)
{
return false;
}
return true;
}
/*Get result from step 2 in pdf*/
public static int sumOfDoubleEvenPlace(long number)
{
int i, sum = 0;
for (i = 1; i <= 16; i++)
{
if (i % 2 == 0)
{
sum = sum + getDigit((int)(number % 10) * 2);
}
number /= 10;
}
return sum;
}
/*return this # if it is a single digit, otherwise, return
* the sum of two digits */
public static int getDigit(int number)
{
if (number < 10)
{
return number;
}
else
{
return (number / 10) + (number % 10);
}
}
/* return sum of odd place digits in number */
public static int sumOfoddPlace(long number)
{
int i, sum = 0;
for (i = 1; i <= 16; i++)
{
if (i % 2 == 1)
{
sum = sum + (int)(number % 10);
}
number /= 10;
}
return sum;
}
/* return true if the digit 'digit' is a prefix for number*/
public static bool prefixMatched(long number, int digit)
{
if (getPrefix(number, 1) == digit || getPrefix(number, 2) == digit)
{
return true;
}
return false;
}
// return the number of digits in d
public static int getSize(long digit)
{
int n = 0;
while (digit != 0)
{
n++;
digit /= 10;
}
return n;
}
/* return the first k number of digits from number. If the number of digits in number is less than k, return number. */
public static long getPrefix(long number, int k)
{
if (k == 1)
{
return number / 1000000000000000;
}
if (k == 2)
{
return number / 100000000000000;
}
return 0;
}
}