#include // A nine-digit integer passes the checksum used // for a Canadian Social Insurance Number (SIN) // if the following algorithm returns true: // // Multiply each digit by either 1 or 2 according // to the following pattern: // // N N N N N N N N N // 1 2 1 2 1 2 1 2 1 // // If a product has two digits, add those two // digits together to produce a single digit. // // Finally, add all nine resulting digits. // If the sum is divisible by 10, the number // passes the SIN checksum. // // For example, given 718860574, we have // // 7 1 8 8 6 0 5 7 4 // 1 2 1 2 1 2 1 2 1 // 7 2 8 16 6 0 5 14 4 // 7 2 8 7 6 0 5 5 4 // // 7 + 2 + 8 + 7 + 6 + 0 + 5 + 5 + 4 = 44 // // Thus, this number does not pass the SIN checksum. // // The code below hard-codes the processing of each // digit. Rewrite it using a single for loop. // // Do not enter your own SIN. Use only fictitious // nine-digit numbers for this exercise. // // Reference: // https://en.wikipedia.org/wiki/Social_insurance_number // Function declarations int main(); // Function definitions int main() { int N{}; std::cout << "Enter a fictitious 9-digit number: "; std::cin >> N; int N0{ N%10 }; int N1{ (N/10)%10 }; int N2{ (N/100)%10 }; int N3{ (N/1000)%10 }; int N4{ (N/10000)%10 }; int N5{ (N/100000)%10 }; int N6{ (N/1000000)%10 }; int N7{ (N/10000000)%10 }; int N8{ (N/100000000)%10 }; N1 *= 2; N3 *= 2; N5 *= 2; N7 *= 2; if ( N1 >= 10 ) { N1 = N1%10 + N1/10; } if ( N3 >= 10 ) { N3 = N3%10 + N3/10; } if ( N5 >= 10 ) { N5 = N5%10 + N5/10; } if ( N7 >= 10 ) { N7 = N7%10 + N7/10; } int sum{ N0 + N1 + N2 + N3 + N4 + N5 + N6 + N7 + N8 }; if ( sum%10 == 0 ) { std::cout << N << " passes the SIN checksum." << std::endl; } else { std::cout << N << " does not pass the SIN checksum." << std::endl; } return 0; } // Hint: // // Have some fun with this one. :-) // // Remember that N%10 gives you the final digit // of 'N', while N /= 10 removes that digit. // // As you work from right to left, the multiplier // alternates between 1 and 2. // Here is one solution: /* int main() { int N{}; std::cout << "Enter a fictitious 9-digit number: "; std::cin >> N; int original{ N }; int multiplier{ 1 }; int sum{ 0 }; for ( int k{ 0 }; k < 9; ++k ) { int digit{ N%10 }; N /= 10; digit *= multiplier; if ( digit >= 10 ) { digit -= 9; } sum += digit; if ( multiplier == 1 ) { multiplier = 2; } else { multiplier = 1; } } if ( sum%10 == 0 ) { std::cout << original << " passes the SIN checksum." << std::endl; } else { std::cout << original << " does not pass the SIN checksum." << std::endl; } return 0; } */ // Question: // // The following for loop can replace the for loop // in the solution above. Compare the two versions. // // Which do you consider more readable? Which do you // expect to be more efficient? For a calculation // involving only nine digits, how important is the // difference in efficiency compared with readability? /* for ( int k{ 0 }; k < 9; ++k ) { int digit{ N%10 }; N /= 10; if ( k%2 == 0 ) { sum += digit; } else { digit *= 2; if ( digit >= 10 ) { digit -= 9; } sum += digit; } } */