Guest User

Untitled

a guest
Mar 17th, 2018
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.92 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3.  
  4. int check(long long c);
  5.  
  6. int main(void)
  7. {
  8. long long card = 0; /* card is to get card input, i is to run while loops until every digit
  9. of card is checked */
  10.  
  11. while ((1000000000000 > card) || (card > 9999999999999999))
  12. {
  13. printf("Number: ");
  14. card = get_long_long();
  15. if ((1000000000000 > card) || (card > 9999999999999999))
  16. {
  17. printf("INVALID\n");
  18. break;
  19. }
  20.  
  21. }
  22.  
  23.  
  24. if (check(card) == 1)
  25. {
  26. if ((card / 10000000000000) == 34 || (card / 10000000000000) == 37)
  27. {
  28. printf("AMEX\n");
  29. }
  30. else if (50 < (card / 100000000000000) && (card / 100000000000000) < 56)
  31. {
  32. printf("MASTERCARD\n");
  33. }
  34. else if ((card / 1000000000000) == 4 || (card / 1000000000000000))
  35. {
  36. printf("VISA\n");
  37. }
  38. else
  39. {
  40. printf("INVALID\n");
  41. }
  42. }
  43. else
  44. {
  45. printf("INVALID\n");
  46. }
  47. }
  48.  
  49. int check(long long card)
  50. {
  51. int store1, sum1 = 0; /* variables for gathering every other digit starting with the second
  52. multiplying by 2 and summing */
  53. int sum2 = 0; /* variables for gethering every other digit starting with the first
  54. one and summing */
  55. long long i; /* i is to run while loops until every digit
  56. of card is checked */
  57.  
  58. i = 10;
  59. while (i < card)
  60. {
  61. store1 = ((card / i) % 10) * 2;
  62. if (store1 > 9)
  63. {
  64. store1 = (store1 / 10) + (store1 % 10);
  65. }
  66. sum1 += store1;
  67. i *= 100;
  68. }
  69.  
  70. i = 1;
  71. while (i < card)
  72. {
  73. sum2 += ((card / i) % 10);
  74. i *= 100;
  75. }
  76.  
  77. if ((sum1 + sum2) % 10 == 0)
  78. {
  79. return 1;
  80. }
  81. else
  82. {
  83. return 0;
  84. }
  85. }
Add Comment
Please, Sign In to add comment