Advertisement
193030

02. Visteon Luhn Algorithm (checksum)

Jun 26th, 2021
1,433
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.74 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <algorithm>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int sumDigits(int inputNumber)
  9. {
  10.     int firstDigit = inputNumber / 10;
  11.     int secondDigit = inputNumber % 10;
  12.     return firstDigit + secondDigit;
  13. }
  14.  
  15.  
  16. int LuhnAlgorithm(string inputString)
  17. {
  18.     int currentNumber = 0;
  19.     int sum = 0;
  20.     for(int i =0; i<inputString.size();i++)
  21.     {
  22.         currentNumber = int(inputString.at(i)) - 48;
  23.         if(i % 2 == 1)
  24.         {
  25.             currentNumber *= 2;
  26.         }
  27.         if(currentNumber >=10)
  28.         {
  29.             sum += sumDigits(currentNumber);
  30.         }
  31.         else
  32.         {
  33.             sum += currentNumber;
  34.  
  35.         }
  36.     }
  37.     cout << sum << endl;
  38.     if(sum % 10  == 0)
  39.         return 1;
  40.     return 0;
  41.    
  42. }
  43.  
  44. int main()
  45. {
  46.     string str = "354557030810924";
  47.     cout << LuhnAlgorithm(str) << endl;
  48.    
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement