#include<stdio.h> // have four spaces before code lines
#include<cs50.h> // this will insert them in the code block
#include<math.h>
bool checksum(long long num);
bool visa(long long num);
bool mastercard(long long num);
bool amexp(long long num);
int main()
{
long long card;
do
{
printf("Number: \n");
card = GetLongLong();
}
while (card<=0);
if(visa(card)||mastercard(card)||amexp(card)) //checks valid format for card number
{
checksum(card);
}
if (checksum(card)) // for printing result
{
if(visa(card))
{
printf("VISA\n");
}
else if(amexp(card))
{
printf("AMEX\n");
}
if(mastercard(card))
{
printf("MASTERCARD\n");
}
}
else
{
printf("INVALID\n");
}
}
bool visa(long long num) //conditition for visa
{ int a=0;
while(num!=0)
{
num = num/10;
a++;
}
if((a==13 || a==16)&&(num/pow(10,12)==4||num/pow(10,15)==4))
{
return true;
}
return false;
}
bool mastercard(long long num)
{ int a=0;
while(num!=0)
{
num = num/10;
a++;
}
if((a==16)&&((num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)||(num/pow(10,14)==51)))
{
return true;
}
return false;
}
bool amexp(long long num)
{ int a=0;
while(num!=0)
{
num = num/10;
a++;
}
if((a==15)&&(num/pow(10,13)==34||num/pow(10,13)==37))
{
return true;
}
return false;
}
bool checksum(long long num)
{
int a=0;
int sum=0;
int sum1=0;
while(num!=0)
{
int b = num%10;
if(a%2==0)
{
sum=sum+b;
}
else
{
sum1=sum1+b;
}
num = num/10;
a++;
}
int sum2=sum+sum1;
if (sum2%10==0)
{
return true;
}
return false;
}
its printing every number invalid
num
is 0,num/pow(10,14)
is most likely 0, too. Also, your checksum calculation might be a bit off. In case of odda
, you'd have to add2*b-9*(b>4)
or something like that, not justb
.