Skip to main content
Became Hot Network Question
edited tags; edited title; added 5 characters in body
Source Link
200_success
  • 144.2k
  • 22
  • 188
  • 473

Credit Card Validationcard validation in C

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

int main(void)
{
    long number = get_long("Number: ");

    // get the individual intergers of number
    // *2 odd digits

    int i1 = ((number / 1000000000000000) % 10); 
    int t1 = i1 * 2;
    int i2 = ((number / 100000000000000) % 10);
    int i3 = ((number / 10000000000000) % 10);
        int t3 = i3 * 2;     
    int i4 = ((number / 1000000000000) % 10);
    int i5 = ((number / 100000000000) % 10);
        int t5 = i5 * 2;
    int i6 = ((number / 10000000000) % 10);
    int i7 = ((number / 1000000000) % 10);
        int t7 = i7 * 2;
    int i8 = ((number / 100000000) % 10);
    int i9 = ((number / 10000000) % 10);
        int t9 = i9 * 2;
    int i10 = ((number / 1000000) % 10);
    int i11 = ((number / 100000) % 10);
        int t11 = i11 * 2;
    int i12 = ((number / 10000) % 10);
    int i13 = ((number / 1000) % 10);
        int t13 = i13 * 2;
    int i14 = ((number / 100) % 10);
    int i15 = ((number / 10) % 10);
        int t15 = i15 * 2;
    int i16 = (number % 10);

    // Luhns Alg
    // calculate sum of variable digits if > 9

    if (t1>9)
    {t1 = t1 - 9;}
    if (t3>9)
    {t3 = t3 - 9;}
    if (t5>9)
    {t5 = t5 - 9;}
    if (t7>9)
    {t7 = t7 - 9;}
    if (t9>9)
    {t9 = t9 - 9;}
    if (t11>9)
    {t11 = t11 - 9;}
    if (t13>9)
    {t13 = t13 - 9;}
    if (t15>9)
    {t15 = t15 - 9;}

    // check lunghs algo = true (0)
    // print card type 

    int sum = (t1+t3+t5+t7+t9+t11+t13+t15+i2+i4+i6+i8+i10+i12+i14+i16);  
    int check = (sum % 10);

    if (check != 0)
    printf("INVALID\n");
    else
    {
    // check type of card
        if(i1 == 0 && i2 == 3 && i3 == 4)
        {
            printf("AMEX\n");
        }
        else if(i1 == 0 && i2 == 3 && i3 == 7)
        {
            printf("AMEX\n");
        }
        else if (i1 == 5 && (i2 == 1 || i2 == 2 || i2 == 3 || i2 == 4 || i2 == 5))
        {
            printf("MASTERCARD\n");
        }
        else if (i1 == 4)
        {
            printf("VISA\n");
        }
        else 
            printf("INVALID\n");
    }
}
The code currently outputs the correct results below.

378282246310005 = AMEX

5555555555554444 = MASTERCARD

5105105105105100 = MASTERCARD

4111111111111111 = VISA

5673598276138003 = INVALID

4062901840 = INVALID

The code currently outputs the correct results below.

378282246310005 = AMEX
5555555555554444 = MASTERCARD
5105105105105100 = MASTERCARD
4111111111111111 = VISA
5673598276138003 = INVALID
4062901840 = INVALID

Credit Card Validation in C

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

int main(void)
{
    long number = get_long("Number: ");

    // get the individual intergers of number
    // *2 odd digits

    int i1 = ((number / 1000000000000000) % 10); 
    int t1 = i1 * 2;
    int i2 = ((number / 100000000000000) % 10);
    int i3 = ((number / 10000000000000) % 10);
        int t3 = i3 * 2;     
    int i4 = ((number / 1000000000000) % 10);
    int i5 = ((number / 100000000000) % 10);
        int t5 = i5 * 2;
    int i6 = ((number / 10000000000) % 10);
    int i7 = ((number / 1000000000) % 10);
        int t7 = i7 * 2;
    int i8 = ((number / 100000000) % 10);
    int i9 = ((number / 10000000) % 10);
        int t9 = i9 * 2;
    int i10 = ((number / 1000000) % 10);
    int i11 = ((number / 100000) % 10);
        int t11 = i11 * 2;
    int i12 = ((number / 10000) % 10);
    int i13 = ((number / 1000) % 10);
        int t13 = i13 * 2;
    int i14 = ((number / 100) % 10);
    int i15 = ((number / 10) % 10);
        int t15 = i15 * 2;
    int i16 = (number % 10);

    // Luhns Alg
    // calculate sum of variable digits if > 9

    if (t1>9)
    {t1 = t1 - 9;}
    if (t3>9)
    {t3 = t3 - 9;}
    if (t5>9)
    {t5 = t5 - 9;}
    if (t7>9)
    {t7 = t7 - 9;}
    if (t9>9)
    {t9 = t9 - 9;}
    if (t11>9)
    {t11 = t11 - 9;}
    if (t13>9)
    {t13 = t13 - 9;}
    if (t15>9)
    {t15 = t15 - 9;}

    // check lunghs algo = true (0)
    // print card type 

    int sum = (t1+t3+t5+t7+t9+t11+t13+t15+i2+i4+i6+i8+i10+i12+i14+i16);  
    int check = (sum % 10);

    if (check != 0)
    printf("INVALID\n");
    else
    {
    // check type of card
        if(i1 == 0 && i2 == 3 && i3 == 4)
        {
            printf("AMEX\n");
        }
        else if(i1 == 0 && i2 == 3 && i3 == 7)
        {
            printf("AMEX\n");
        }
        else if (i1 == 5 && (i2 == 1 || i2 == 2 || i2 == 3 || i2 == 4 || i2 == 5))
        {
            printf("MASTERCARD\n");
        }
        else if (i1 == 4)
        {
            printf("VISA\n");
        }
        else 
            printf("INVALID\n");
    }
}
The code currently outputs the correct results below.

378282246310005 = AMEX

5555555555554444 = MASTERCARD

5105105105105100 = MASTERCARD

4111111111111111 = VISA

5673598276138003 = INVALID

4062901840 = INVALID

Credit card validation in C

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

int main(void)
{
    long number = get_long("Number: ");

    // get the individual intergers of number
    // *2 odd digits

    int i1 = ((number / 1000000000000000) % 10); 
    int t1 = i1 * 2;
    int i2 = ((number / 100000000000000) % 10);
    int i3 = ((number / 10000000000000) % 10);
        int t3 = i3 * 2;     
    int i4 = ((number / 1000000000000) % 10);
    int i5 = ((number / 100000000000) % 10);
        int t5 = i5 * 2;
    int i6 = ((number / 10000000000) % 10);
    int i7 = ((number / 1000000000) % 10);
        int t7 = i7 * 2;
    int i8 = ((number / 100000000) % 10);
    int i9 = ((number / 10000000) % 10);
        int t9 = i9 * 2;
    int i10 = ((number / 1000000) % 10);
    int i11 = ((number / 100000) % 10);
        int t11 = i11 * 2;
    int i12 = ((number / 10000) % 10);
    int i13 = ((number / 1000) % 10);
        int t13 = i13 * 2;
    int i14 = ((number / 100) % 10);
    int i15 = ((number / 10) % 10);
        int t15 = i15 * 2;
    int i16 = (number % 10);

    // Luhns Alg
    // calculate sum of variable digits if > 9

    if (t1>9)
    {t1 = t1 - 9;}
    if (t3>9)
    {t3 = t3 - 9;}
    if (t5>9)
    {t5 = t5 - 9;}
    if (t7>9)
    {t7 = t7 - 9;}
    if (t9>9)
    {t9 = t9 - 9;}
    if (t11>9)
    {t11 = t11 - 9;}
    if (t13>9)
    {t13 = t13 - 9;}
    if (t15>9)
    {t15 = t15 - 9;}

    // check lunghs algo = true (0)
    // print card type 

    int sum = (t1+t3+t5+t7+t9+t11+t13+t15+i2+i4+i6+i8+i10+i12+i14+i16);  
    int check = (sum % 10);

    if (check != 0)
    printf("INVALID\n");
    else
    {
    // check type of card
        if(i1 == 0 && i2 == 3 && i3 == 4)
        {
            printf("AMEX\n");
        }
        else if(i1 == 0 && i2 == 3 && i3 == 7)
        {
            printf("AMEX\n");
        }
        else if (i1 == 5 && (i2 == 1 || i2 == 2 || i2 == 3 || i2 == 4 || i2 == 5))
        {
            printf("MASTERCARD\n");
        }
        else if (i1 == 4)
        {
            printf("VISA\n");
        }
        else 
            printf("INVALID\n");
    }
}

The code currently outputs the correct results below.

378282246310005 = AMEX
5555555555554444 = MASTERCARD
5105105105105100 = MASTERCARD
4111111111111111 = VISA
5673598276138003 = INVALID
4062901840 = INVALID

Tweeted twitter.com/StackCodeReview/status/1139910680088403970
deleted 1 character in body
Source Link
Dan Sutton
  • 111
  • 1
  • 5
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    long number = get_long("Number: ");

    // get the individual intergers of number
    // *2 odd digits

    int i1 = ((number / 1000000000000000) % 10); 
    int t1 = i1 * 2;
    int i2 = ((number / 100000000000000) % 10);
    int i3 = ((number / 10000000000000) % 10);
        int t3 = i3 * 2;     
    int i4 = ((number / 1000000000000) % 10);
    int i5 = ((number / 100000000000) % 10);
        int t5 = i5 * 2;
    int i6 = ((number / 10000000000) % 10);
    int i7 = ((number / 1000000000) % 10);
        int t7 = i7 * 2;
    int i8 = ((number / 100000000) % 10);
    int i9 = ((number / 10000000) % 10);
        int t9 = i9 * 2;
    int i10 = ((number / 1000000) % 10);
    int i11 = ((number / 100000) % 10);
        int t11 = i11 * 2;
    int i12 = ((number / 10000) % 10);
    int i13 = ((number / 1000) % 10);
        int t13 = i13 * 2;
    int i14 = ((number / 100) % 10);
    int i15 = ((number / 10) % 10);
        int t15 = i15 * 2;
    int i16 = (number % 10);

    // Luhns Alg
    // calculate sum of variable digits if > 109

    if (t1>9)
    {t1 = t1 - 9;}
    if (t3>9)
    {t3 = t3 - 9;}
    if (t5>9)
    {t5 = t5 - 9;}
    if (t7>9)
    {t7 = t7 - 9;}
    if (t9>9)
    {t9 = t9 - 9;}
    if (t11>9)
    {t11 = t11 - 9;}
    if (t13>9)
    {t13 = t13 - 9;}
    if (t15>9)
    {t15 = t15 - 9;}

    // check lunghs algo = true (0)
    // print card type 

    int sum = (t1+t3+t5+t7+t9+t11+t13+t15+i2+i4+i6+i8+i10+i12+i14+i16);  
    int check = (sum % 10);

    if (check != 0)
    printf("INVALID\n");
    else
    {
    // check type of card
        if(i1 == 0 && i2 == 3 && i3 == 4)
        {
            printf("AMEX\n");
        }
        else if(i1 == 0 && i2 == 3 && i3 == 7)
        {
            printf("AMEX\n");
        }
        else if (i1 == 5 && (i2 == 1 || i2 == 2 || i2 == 3 || i2 == 4 || i2 == 5))
        {
            printf("MASTERCARD\n");
        }
        else if (i1 == 4)
        {
            printf("VISA\n");
        }
        else 
            printf("INVALID\n");
    }
}
The code currently outputs the correct results below.

378282246310005 = AMEX

5555555555554444 = MASTERCARD

5105105105105100 = MASTERCARD

4111111111111111 = VISA

5673598276138003 = INVALID

4062901840 = INVALID
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    long number = get_long("Number: ");

    // get the individual intergers of number
    // *2 odd digits

    int i1 = ((number / 1000000000000000) % 10); 
    int t1 = i1 * 2;
    int i2 = ((number / 100000000000000) % 10);
    int i3 = ((number / 10000000000000) % 10);
        int t3 = i3 * 2;     
    int i4 = ((number / 1000000000000) % 10);
    int i5 = ((number / 100000000000) % 10);
        int t5 = i5 * 2;
    int i6 = ((number / 10000000000) % 10);
    int i7 = ((number / 1000000000) % 10);
        int t7 = i7 * 2;
    int i8 = ((number / 100000000) % 10);
    int i9 = ((number / 10000000) % 10);
        int t9 = i9 * 2;
    int i10 = ((number / 1000000) % 10);
    int i11 = ((number / 100000) % 10);
        int t11 = i11 * 2;
    int i12 = ((number / 10000) % 10);
    int i13 = ((number / 1000) % 10);
        int t13 = i13 * 2;
    int i14 = ((number / 100) % 10);
    int i15 = ((number / 10) % 10);
        int t15 = i15 * 2;
    int i16 = (number % 10);

    // Luhns Alg
    // calculate sum of variable digits if > 10

    if (t1>9)
    {t1 = t1 - 9;}
    if (t3>9)
    {t3 = t3 - 9;}
    if (t5>9)
    {t5 = t5 - 9;}
    if (t7>9)
    {t7 = t7 - 9;}
    if (t9>9)
    {t9 = t9 - 9;}
    if (t11>9)
    {t11 = t11 - 9;}
    if (t13>9)
    {t13 = t13 - 9;}
    if (t15>9)
    {t15 = t15 - 9;}

    // check lunghs algo = true (0)
    // print card type 

    int sum = (t1+t3+t5+t7+t9+t11+t13+t15+i2+i4+i6+i8+i10+i12+i14+i16);  
    int check = (sum % 10);

    if (check != 0)
    printf("INVALID\n");
    else
    {
    // check type of card
        if(i1 == 0 && i2 == 3 && i3 == 4)
        {
            printf("AMEX\n");
        }
        else if(i1 == 0 && i2 == 3 && i3 == 7)
        {
            printf("AMEX\n");
        }
        else if (i1 == 5 && (i2 == 1 || i2 == 2 || i2 == 3 || i2 == 4 || i2 == 5))
        {
            printf("MASTERCARD\n");
        }
        else if (i1 == 4)
        {
            printf("VISA\n");
        }
        else 
            printf("INVALID\n");
    }
}
The code currently outputs the correct results below.

378282246310005 = AMEX

5555555555554444 = MASTERCARD

5105105105105100 = MASTERCARD

4111111111111111 = VISA

5673598276138003 = INVALID

4062901840 = INVALID
#include <cs50.h>
#include <stdio.h>
#include <math.h>

int main(void)
{
    long number = get_long("Number: ");

    // get the individual intergers of number
    // *2 odd digits

    int i1 = ((number / 1000000000000000) % 10); 
    int t1 = i1 * 2;
    int i2 = ((number / 100000000000000) % 10);
    int i3 = ((number / 10000000000000) % 10);
        int t3 = i3 * 2;     
    int i4 = ((number / 1000000000000) % 10);
    int i5 = ((number / 100000000000) % 10);
        int t5 = i5 * 2;
    int i6 = ((number / 10000000000) % 10);
    int i7 = ((number / 1000000000) % 10);
        int t7 = i7 * 2;
    int i8 = ((number / 100000000) % 10);
    int i9 = ((number / 10000000) % 10);
        int t9 = i9 * 2;
    int i10 = ((number / 1000000) % 10);
    int i11 = ((number / 100000) % 10);
        int t11 = i11 * 2;
    int i12 = ((number / 10000) % 10);
    int i13 = ((number / 1000) % 10);
        int t13 = i13 * 2;
    int i14 = ((number / 100) % 10);
    int i15 = ((number / 10) % 10);
        int t15 = i15 * 2;
    int i16 = (number % 10);

    // Luhns Alg
    // calculate sum of variable digits if > 9

    if (t1>9)
    {t1 = t1 - 9;}
    if (t3>9)
    {t3 = t3 - 9;}
    if (t5>9)
    {t5 = t5 - 9;}
    if (t7>9)
    {t7 = t7 - 9;}
    if (t9>9)
    {t9 = t9 - 9;}
    if (t11>9)
    {t11 = t11 - 9;}
    if (t13>9)
    {t13 = t13 - 9;}
    if (t15>9)
    {t15 = t15 - 9;}

    // check lunghs algo = true (0)
    // print card type 

    int sum = (t1+t3+t5+t7+t9+t11+t13+t15+i2+i4+i6+i8+i10+i12+i14+i16);  
    int check = (sum % 10);

    if (check != 0)
    printf("INVALID\n");
    else
    {
    // check type of card
        if(i1 == 0 && i2 == 3 && i3 == 4)
        {
            printf("AMEX\n");
        }
        else if(i1 == 0 && i2 == 3 && i3 == 7)
        {
            printf("AMEX\n");
        }
        else if (i1 == 5 && (i2 == 1 || i2 == 2 || i2 == 3 || i2 == 4 || i2 == 5))
        {
            printf("MASTERCARD\n");
        }
        else if (i1 == 4)
        {
            printf("VISA\n");
        }
        else 
            printf("INVALID\n");
    }
}
The code currently outputs the correct results below.

378282246310005 = AMEX

5555555555554444 = MASTERCARD

5105105105105100 = MASTERCARD

4111111111111111 = VISA

5673598276138003 = INVALID

4062901840 = INVALID
tags
Link
dfhwze
  • 13.9k
  • 3
  • 38
  • 101
Source Link
Dan Sutton
  • 111
  • 1
  • 5
Loading