Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string>
- #include <algorithm>
- using namespace std;
- int sumDigits(int inputNumber)
- {
- int firstDigit = inputNumber / 10;
- int secondDigit = inputNumber % 10;
- return firstDigit + secondDigit;
- }
- int LuhnAlgorithm(string inputString)
- {
- int currentNumber = 0;
- int sum = 0;
- for(int i =0; i<inputString.size();i++)
- {
- currentNumber = int(inputString.at(i)) - 48;
- if(i % 2 == 1)
- {
- currentNumber *= 2;
- }
- if(currentNumber >=10)
- {
- sum += sumDigits(currentNumber);
- }
- else
- {
- sum += currentNumber;
- }
- }
- cout << sum << endl;
- if(sum % 10 == 0)
- return 1;
- return 0;
- }
- int main()
- {
- string str = "354557030810924";
- cout << LuhnAlgorithm(str) << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement