Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <cs50.h>
- int isvalid(long n);
- int main(void)
- {
- long ccnum = get_long("Enter Credit card number: \n");
- int x = isvalid(ccnum);
- if(x != 0)
- {
- printf("INVALID\n");
- }
- else if (x == 0 && (ccnum > 5099999999999999 && ccnum < 5600000000000000))
- {
- printf("MASTERCARD\n");
- }
- else if ( x == 0 && (ccnum > 339999999999999 && ccnum < 350000000000000))
- {
- printf("AMEX\n");
- }
- else if (x == 0 && (ccnum > 369999999999999 && ccnum < 380000000000000))
- {
- printf("AMEX\n");
- }
- else if ( x == 0 && (ccnum >3999999999999999 && ccnum < 4999999999999999))
- {
- printf("VISA\n");
- }
- }
- int isvalid(long n)
- {
- if (n > 999999999999999 && n < 10000000000000000)
- {
- int x = (2 * (n % 10) + 2 * ((n % 1000 - n % 100) / 100)) + (2 * ((n % 100000 - n % 10000) / 10000)) + (2 * ((n % 10000000 - n % 1000000) / 1000000)) + (2 * ((n % 1000000000 - n % 100000000) / 100000000)) + (2 * ((n % 100000000000 - n % 10000000000)/ 10000000000)) + (2 * ((n % 10000000000000 - n % 1000000000000) / 1000000000000)) + (2 * ((n % 1000000000000000 - n % 100000000000000) / 100000000000000));
- int y = (2 * ((n % 100 - n %10) / 10)) + (2 * ((n % 10000 - n % 1000) / 1000)) + (2 * ((n % 1000000 - n % 100000) / 100000)) + (2 * ((n % 100000000 - n % 10000000) / 10000000)) + (2 * ((n % 10000000000 - n % 1000000000) / 1000000000)) + (2 * ((n % 1000000000000 - n % 100000000000) / 100000000000)) + (2 * ((n % 100000000000000 - n % 10000000000000) / 10000000000000)) + (2 * ((n % 10000000000000000 - n % 1000000000000000) / 1000000000000000));
- return (x + y) % 10;
- }
- else if (n > 99999999999999 && n < 1000000000000000)
- {
- int x = (2 * (n % 10)) + (2 * ((n % 1000 - n % 100) / 100)) + (2 * ((n % 100000 - n % 10000) / 10000)) + (2 * ((n % 10000000 - n % 1000000) / 1000000)) + (2 * ((n % 1000000000 - n % 100000000) / 100000000)) + (2 * ((n % 100000000000 - n % 10000000000)/ 10000000000)) + (2 * ((n % 10000000000000 - n % 1000000000000) / 1000000000000)) + (2 * ((n % 1000000000000000 - n % 100000000000000) / 100000000000000));
- int y = (2 * ((n % 100 - n %10) / 10)) + (2 * ((n % 10000 - n % 1000) / 1000)) + (2 * ((n % 1000000 - n % 100000) / 100000)) + (2 * ((n % 100000000 - n % 10000000) / 10000000)) + (2 * ((n % 10000000000 - n % 1000000000) / 1000000000)) + (2 * ((n % 1000000000000 - n % 100000000000) / 100000000000)) + (2 * ((n % 100000000000000 - n % 10000000000000) / 10000000000000));
- return ((x + y) % 10);
- }
- else if (n > 999999999999 && n < 10000000000000)
- {
- int x = (2 * (n % 10) + 2 * ((n % 1000 - n % 100) / 100)) + (2 * ((n % 100000 - n % 10000) / 10000)) + (2 * ((n % 10000000 - n % 1000000) / 1000000)) + (2 * ((n % 1000000000 - n % 100000000) / 100000000)) + (2 * ((n % 100000000000 - n % 10000000000)/ 10000000000)) + (2 * ((n % 10000000000000 - n % 1000000000000) / 1000000000000));
- int y = (2 * ((n % 100 - n %10) / 10)) + (2 * ((n % 10000 - n % 1000) / 1000)) + (2 * ((n % 1000000 - n % 100000) / 100000)) + (2 * ((n % 100000000 - n % 10000000) / 10000000)) + (2 * ((n % 10000000000 - n % 1000000000) / 1000000000)) + (2 * ((n % 1000000000000 - n % 100000000000) / 100000000000));
- return (x + y) % 10;
- }
- else
- {
- return 1;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement