u/Aurora-1983

▲ 1 r/cs50

// Program to check the whether a given credit card number is valid or not
    #include <cs50.h>
    #include <stdio.h>
    // Function declaration
    int check(long n);
    void checkvisa(long n);
    void checkamex(long n);
    void checkmastercard(long n);
    int main(void)
    {
        long card_number;
        int length = 0;
        do
        {
            card_number = get_long("Number: ");
        }
        while (card_number < 1);
        int temp = card_number;
        int sum = check(card_number);
        while (temp != 0)
        {
            temp /= 10;
            length++;
        }
        if (sum % 10 == 0)
        {
            if (length == 13 || length == 16)
            {
                checkvisa(card_number);
            }
            if (length == 15)
            {
                checkamex(card_number);;
            }
            if (length == 16)
            {
                checkmastercard(card_number);
            }
        }
        else
        {
            printf("INVALID\n");
        }
    }
    // Function definition
    int check(long n)
    {   long n1,n2,n3;
        int sum1 = 0,sum2 = 0,sum;
        long x = n;
        long m = n;
        int counter = 0;
        while (n != 0)
        {
            n1 = n % 10;
            n /= 10;
            n2 = 2*(n % 10);
            n /= 10;
            sum2 = sum2 + (n2 / 10 + n2 % 10);
            sum1 += n1;
        }
        return sum = sum1 + sum2;
    }
    void checkvisa(long n)
    {
        int a = n;
        do
        {
            a /= 10;
        }
        while (a != 4);
        if (a == 4)
        {
            printf("VISA\n");
        }
    }
    void checkamex (long n)
    {
        int b = n;
        do
        {
            b /= 10;
        }
        while (b > 38);
        if (b == 34 || b == 37)
        {
            printf("AMEX\n");
        }
    }
    void checkmastercard(long n)
    {
        int c = n;
        do
        {
            c /= 10;
        }
        while (c > 56);
        if (c >= 51 && c <= 55)
        {
            printf("MASTERCARD");
        }
    }

The check50 is:

check50

So I have encountered a problem with my code and I know it's working as it's not printing INVALID but the other things like identification of the card is not working. I mean it's entering my if conditional and exiting with out printing anything. I have included the check50 website which details my problem. Hope somebody can help.

reddit.com
u/Aurora-1983 — 21 days ago
▲ 2 r/cs50

I have been trying the credit card program from PSET-1 and I don't know where I am going wrong. Can someone help?

Here's my code:

// Program to check the whether a given credit card number is valid or not

#include <cs50.h>

#include <stdio.h>

// Function declaration

int check(long n);

void checkvisa(long n);

void checkamex(long n);

void checkmastercard(long n);

int main(void)

{

long card_number;

int length = 0;

do

{

card_number = get_long("Number: ");

}

while (card_number < 1);

int temp = card_number;

int sum = check(card_number);

while (temp != 0)

{

temp /= 10;

length++;

}

if (sum % 10 == 0)

{

if (length == 13 || length == 16)

{

checkvisa(card_number);

}

if (length == 15)

{

checkamex(card_number);;

}

if (length == 16)

{

checkmastercard(card_number);

}

}

else

{

printf("INVALID\n");

}

}

// Function definition

int check(long n)

{ long n1,n2,n3;

int sum1 = 0,sum2 = 0,sum;

long x = n;

long m = n;

int counter = 0;

while (n != 0)

{

n1 = n % 10;

n /= 10;

n2 = 2*(n % 10);

n /= 10;

sum2 = sum2 + (n2 / 10 + n2 % 10);

sum1 += n1;

}

return sum = sum1 + sum2;

}

void checkvisa(long n)

{

int a = n;

do

{

a /= 10;

}

while (a != 4);

if (a == 4)

{

printf("VISA\n");

}

}

void checkamex (long n)

{

int b = n;

do

{

b /= 10;

}

while (b > 38);

if (b == 34 || b == 37)

{

printf("AMEX\n");

}

}

void checkmastercard(long n)

{

int c = n;

do

{

c /= 10;

}

while (c > 56);

if (c >= 51 && c <= 55)

{

printf("MASTERCARD");

}

}

reddit.com
u/Aurora-1983 — 22 days ago
▲ 5 r/cs50

So i have started the credit program form PSET-1 and I have solved the checksum part, but I am not able to write the code for identifying those cards. Can somebody help?

u/Aurora-1983 — 22 days ago