Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.16 KB | None | 0 0
  1. #include <stdio.h>
  2.  
  3. // Created: Joonas Kortesalmi (2006-11-10)
  4. // Finnish bank transfer reference number calculator.
  5.  
  6. unsigned long long int refnum ( unsigned long long int);
  7.  
  8. int main (int argc, char *argv[]) {
  9. unsigned long long int inputnum;
  10. if (argc != 2) {
  11. fputs("Usage: refnum <number>\n", stderr);
  12. return(-1);
  13. } else if ( sscanf(argv[1], "%llu", &inputnum) != 1 )
  14. {
  15. fputs("Usage: refnum <number>\n", stderr);
  16. return(-2);
  17. } else {
  18. printf("%llu\n", refnum(inputnum));
  19. }
  20.  
  21. return 0;
  22. }
  23.  
  24. unsigned long long int refnum (unsigned long long int input) {
  25. unsigned int i = 0;
  26. unsigned long long int temp;
  27. unsigned int sum = 0;
  28.  
  29. // Split the number from the end to the beginning - one number
  30. // at a time multiply by (7, 3, 1, 7, 3, 1, ...) and take a sum.
  31. for (temp = input; temp > 0; temp = temp/10) {
  32. switch (i++ % 3) {
  33. case 0:
  34. sum += 7 * (temp % 10);
  35. break;
  36. case 1:
  37. sum += 3 * (temp % 10);
  38. break;
  39. case 2:
  40. sum += 1 * (temp % 10);
  41. break;
  42. }
  43. }
  44. // The checksum is the distance of modulo 10 of the sum to the next
  45. // number divisible by 10.
  46. return 10*input + (10 - sum % 10) % 10;
  47. }
Add Comment
Please, Sign In to add comment