Guest User

Untitled

a guest
Jul 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.06 KB | None | 0 0
  1. #include <cs50.h>
  2. #include <stdio.h>
  3. #include <math.h>
  4.  
  5. int main(void)
  6. {
  7. long long card;
  8. // variable credit card number
  9. long long cc_number;
  10. // variable card number to be used for counter later on
  11. long long c;
  12. long long amex = 10 ^ 15;
  13. long long mastercard = 10 ^ 16;
  14. long long visa13 = 10 ^ 13;
  15. long long visa16 = 10 ^ 16;
  16. int counter;
  17. int sum1 = 0;
  18. int sum2 = 0;
  19.  
  20.  
  21. do
  22. {
  23. printf ("credit card number:");
  24. card = get_long_long ();
  25. }
  26. while (card < 0 );
  27.  
  28. //hey! come and declare variables here later if code dosent work
  29.  
  30. // code for sum
  31. while (cc_number > 0)
  32. {
  33. // a represents every other digit multiplied by 2
  34. int a = ( (cc_number % 100) / 10) * 2;
  35.  
  36. // b represents all other digits
  37. int b = ( (cc_number % 100) / 10) * 2;
  38.  
  39. if (a > 9)
  40. {
  41. sum1 = sum1 + (a % 10) + 1;
  42. }
  43.  
  44. else
  45. {
  46. // sum1 stores the value of the sum of all as'
  47. sum1 = sum1 + a;
  48. }
  49.  
  50. // sum2 stores the sum of all other digits
  51. sum2 = sum2 + b;
  52. cc_number = cc_number / 100;
  53. }
  54. // counts digits
  55. for (counter = 0; c > 0; counter++)
  56. {
  57. c = c / 10;
  58. }
  59. // adds the previous sums in this code,and must end with 0 to be valid
  60. int sum = sum1 + sum2;
  61.  
  62. // prints visa using the following conditions
  63. if ( (counter==16 && (sum %10) == 0) && (card / visa16) == 4 ) || ( (counter == 13 && (sum % 10) ==0 && (card / visa13) == 4) )
  64. {
  65. printf ("VISA \n");
  66. }
  67. // prints amex using the following conditions
  68. else if (counter == 15 && (sum % 10) == 0 && (card / (amex) ==34 || card / (amex) == 37)
  69. {
  70. printf ("AMEX \n");
  71. }
  72. // prints mastercard using the following conditions
  73. else if (counter == 16 && (sum % 10) == 0 && (card / mastercard > 50 && card / mastercard < 56) )
  74. {
  75. printf ("MASTERCARD \n");
  76. }
  77. // prints invalid if none of the above conditions is met
  78. else
  79. {
  80. printf ("INVALID \n")
  81. }
  82. }
Add Comment
Please, Sign In to add comment