Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- // Created: Joonas Kortesalmi (2006-11-10)
- // Finnish bank transfer reference number calculator.
- unsigned long long int refnum ( unsigned long long int);
- int main (int argc, char *argv[]) {
- unsigned long long int inputnum;
- if (argc != 2) {
- fputs("Usage: refnum <number>\n", stderr);
- return(-1);
- } else if ( sscanf(argv[1], "%llu", &inputnum) != 1 )
- {
- fputs("Usage: refnum <number>\n", stderr);
- return(-2);
- } else {
- printf("%llu\n", refnum(inputnum));
- }
- return 0;
- }
- unsigned long long int refnum (unsigned long long int input) {
- unsigned int i = 0;
- unsigned long long int temp;
- unsigned int sum = 0;
- // Split the number from the end to the beginning - one number
- // at a time multiply by (7, 3, 1, 7, 3, 1, ...) and take a sum.
- for (temp = input; temp > 0; temp = temp/10) {
- switch (i++ % 3) {
- case 0:
- sum += 7 * (temp % 10);
- break;
- case 1:
- sum += 3 * (temp % 10);
- break;
- case 2:
- sum += 1 * (temp % 10);
- break;
- }
- }
- // The checksum is the distance of modulo 10 of the sum to the next
- // number divisible by 10.
- return 10*input + (10 - sum % 10) % 10;
- }
Add Comment
Please, Sign In to add comment