0
#include <cs50.h>
#include <stdio.h>

int length(long long n);
int lastdigit(long long n);
int stay(long long n);
int addmutiply(long long n);
int odd(long long n);
int digit2(long long n);
int digit1(long long n);

int main(void)
{
    long long n;
    do
    {
        n=get_long_long("credit card number:");
    }
    while(n<0);
    int l=length(n);
    int e = odd(n)+addmutiply(n);
    int u = digit2(n);
    int i = digit1(n);
    int o =e/10;
    if(o==0)
    {
        if(l==15&&(u==34||u==37))
        {
            printf("American Express");
        }
        else if(l==16&&u>50&&u<56)
        {
            printf("MasterCard");
        }
        else if((l==13||l==14)&&i==4)
        {
            printf("visa");
        }
        else
        {
            printf("invalid!");
        }
    }
    else
    {
        printf("invalid!");
    }
    printf("\n");    
} 

int length(long long n)
{
    int a =0;
    while(n>0)
    {
        n=n/10;
        a++;
    }
    return a;
}

int lastdigit(long long n)
{
    n = n%10;
    return n;
}

int stay(long long n)
{
    n = (n-n%10)/10;
    return n;
}

int addmutiply(long long n)
{
    int total = 0;
    while(n>0)
    {
        long long x = stay(n);
        int w,e,add;
        int q = lastdigit(x)*2;
        if (q>10)
        {
            w = lastdigit(q);
            e = stay(q);
            add = w+e;
        }
        else
        {
            add = q;
        }
        total += add;
        n=stay(n);
        n=stay(n);
    }
    return total;
}

int  odd(long long n)
{
    int tt = 0;
    while (n>0)
    {
        tt+=lastdigit(n);
        n = n/10;
        n = n/10;
    }
    return tt;
}

int digit2(long long n)
{
    do
    {
        n = n/10;
    }
    while(n>=100);
    return n;   
}

int digit1(long long n)
{
    do
    {
        n=n/10;
    }
    while(n>=10);
    return n;
}
  • I can not get the correct results, and I spent two hours on this and still don't know how to solve it. Thank you for your help!! – Duncan Wan Feb 21 at 4:06

You must log in to answer this question.

Browse other questions tagged .