Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <cs50.h>
- #include <stdio.h>
- int check(long long c);
- int main(void)
- {
- long long card = 0; /* card is to get card input, i is to run while loops until every digit
- of card is checked */
- while ((1000000000000 > card) || (card > 9999999999999999))
- {
- printf("Number: ");
- card = get_long_long();
- if ((1000000000000 > card) || (card > 9999999999999999))
- {
- printf("INVALID\n");
- break;
- }
- }
- if (check(card) == 1)
- {
- if ((card / 10000000000000) == 34 || (card / 10000000000000) == 37)
- {
- printf("AMEX\n");
- }
- else if (50 < (card / 100000000000000) && (card / 100000000000000) < 56)
- {
- printf("MASTERCARD\n");
- }
- else if ((card / 1000000000000) == 4 || (card / 1000000000000000))
- {
- printf("VISA\n");
- }
- else
- {
- printf("INVALID\n");
- }
- }
- else
- {
- printf("INVALID\n");
- }
- }
- int check(long long card)
- {
- int store1, sum1 = 0; /* variables for gathering every other digit starting with the second
- multiplying by 2 and summing */
- int sum2 = 0; /* variables for gethering every other digit starting with the first
- one and summing */
- long long i; /* i is to run while loops until every digit
- of card is checked */
- i = 10;
- while (i < card)
- {
- store1 = ((card / i) % 10) * 2;
- if (store1 > 9)
- {
- store1 = (store1 / 10) + (store1 % 10);
- }
- sum1 += store1;
- i *= 100;
- }
- i = 1;
- while (i < card)
- {
- sum2 += ((card / i) % 10);
- i *= 100;
- }
- if ((sum1 + sum2) % 10 == 0)
- {
- return 1;
- }
- else
- {
- return 0;
- }
- }
Add Comment
Please, Sign In to add comment