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

int main(void)

{
    long long n;
    int nDigits;
    long long k1, k2, k3, k4, k5, k6, k7,k8,k9,k10,k11,k12,k13,k14,k15,k16;
    int t1, t2;

    do
    {
        printf("Number: ");
        n = GetLongLong();

        nDigits = floor(log10(llabs(n)))+1;

        k1 = (n/1) % 10;
        k2 = (n/10) % 10;
        k3 = (n/100) % 10;
        k4 = (n/1000) % 10;
        k5 = (n/10000) % 10;
        k6 = (n/100000) % 10;
        k7 = (n/1000000) % 10;
        k8 = (n/10000000) % 10;
        k9 = (n/100000000) % 10;
        k10 = (n/1000000000) % 10;
        k11 = (n/10000000000) % 10;
        k12 = (n/100000000000) % 10;
        k13 = (n/1000000000000) % 10;
        k14 = (n/10000000000000) % 10;
        k15 = (n/100000000000000) % 10;
        k16 = (n/1000000000000000) % 10;

        k2*=2, k4*=2, k6*=2, k8*=2, k10*=2, k12*=2, k14*=2, k16*=2;
        t1 = (k2 % 10) + (k2/10) + (k4 % 10) + (k4/10) + (k6 % 10) + (k6/10) + (k8 % 10) + 
        (k8/10) + (k10 % 10) + (k10/10) + (k12 % 10) + (k12/10) + (k14 % 10) + (k14/10) + (k16 % 10) + (k16/10);
        t2 = t1 + k1 + k3 + k5 + k7 + k9 + k11 + k13 + k15;
        printf("%d, %d\n", t1, t2);

        if (t2 % 10 == 0)
        {
            if ((nDigits == 15) && (((k15 == 3) && (k14 == 4)) || ((k15 == 3) && (k14 == 7))))
            {
                printf("AMEX\n");
            }
            else if ((nDigits == 16) && (((k16 == 5) && (k15 == 1)) || ((k15 == 5) && (k14 == 2)) || ((k15 == 5) && (k14 == 3)) || 
            ((k15 == 5) && (k14 == 4)) || ((k15 == 5) && (k14 == 5))))
            {
                printf("MASTERCARD\n");
            }
            else if (((nDigits == 13) && (k13 == 4)) || ((nDigits == 16) && (k16 == 4)))
            {
                printf("VISA\n");
            }
            else
            {
                printf("INVALID\n");
            }
        }
        else
        {
            printf("INVALID\n");
        }
    }
    while (n <= 0);
}

Hello again, i try to figure out "Bad Credit" problem. it gives me always "INVALID". Still couldn't figure out where is my wrong? Thanks for help in advance :)

** And also if you want to give me some shortcuts about my long calculations and logic it ll be wonderful :)

*** Got it again, after multiply all k values with 2, k15 and k14 changes... gonna fix that.

k2 = 2*l2; 
k4 = 2*l4; 
k6 = 2*l6; 
k8 = 2*l8; 
k10 = 2 *l10; 
k12 = 2 *l12; 
k14 = 2 *l14; 
k16 = 2 *l16;

t1 = (k2 % 10) + (k2/10) + (k4 % 10) + (k4/10) + (k6 % 10) + (k6/10) + (k8 % 10) + 
(k8/10) + (k10 % 10) + (k10/10) + (k12 % 10) + (k12/10) + (k14 % 10) + (k14/10) + (k16 % 10) + (k16/10);
t2 = t1 + l1 + l3 + l5 + l7 + l9 + l11 + l13 + l15;

if (t2 % 10 == 0)
{
    if (nDigits == 15 && l15 == 3 && (l14 == 4 || l14 == 7))

Fixed like this! But im open to any simple suggestions to modify codes.

2
  • 1
    I'm pretty sure an array would be a great option to simplify your code...
    – ChrisG
    Aug 15 '16 at 13:45
  • Thanks willl try to learn about that :) Aug 15 '16 at 13:59
0

What you are doing in your isn't understandable at all without struggling at each line of code, coming back and forth. That is not good. You should use comments.

Without reading all of your code, in both version, you seem to repeat several times the same or nearly the same opperations. C allows you to create loops. It's up to you to find which loop would be the one you need.

There is also this 'k' variable and the 't' one, that you created like 'k1', 'k2', 'k3'... And there's also a C feature for that kind of stuff, arrays.

I suggest you to read this.

Hoping I've helped, good luck.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .