Advertisement
homeworkhelp111

luhn

Feb 6th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.80 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     string card;
  7.     int digit, sum = 0, alternate = 0;
  8.  
  9.     cin >> card;
  10.  
  11.     for (int i = card.length() - 1; i >= 0; i--) {
  12.         digit = card[i] - '0'; //int cast convert digit to integer
  13.  
  14.         if (alternate == 1)
  15.             digit = digit * 2;
  16.  
  17.         sum = sum + digit / 10;
  18.         sum = sum + digit % 10;
  19.  
  20.         alternate = !alternate;
  21.     }
  22.  
  23.     if (sum % 10 == 0) {
  24.         if (card[0] == '4' && card.length() >=13 && card.length()<= 16) {
  25.             cout << "Visa";
  26.         }
  27.         else if (card[0] == '5' && card[1]>='1' && card[1]<='5' && card.length() == 16) {
  28.             cout << "Mastercard";
  29.         }
  30.         else if (card[0] == '3' && (card[1]=='4' || card[1]=='7') && card.length() == 15) {
  31.             cout << "Amex";
  32.         }
  33.         else {
  34.             cout << "Almost";
  35.         }
  36.     }
  37.     else {
  38.         cout << "Invalid";
  39.     }
  40.     return 0;
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement