fork(3) download
  1. Program specification
  2. ----------------------
  3.  
  4. American Express uses 15 digit numbers,
  5. MasterCard uses 16 digit numbers,
  6. and Visa uses 13 and 16 digits numbers.
  7.  
  8. American Express numbers all start with 34 or 37
  9. MasterCard numbers all start with 51, 52, 53, 54, or 55
  10. and Visa numbers all start with 4.
  11.  
  12. But credit card numbers also have a “checksum” built into them, a
  13. mathematical relationship between at least one number and others.
  14.  
  15. The formula:
  16.  
  17. i) Multiply every other digit by 2, starting with the number’s
  18. second to last digit, and then add those products’ digits together.
  19.  
  20. ii) Add the sum to the sum of the digits that weren’t multiplied by 2.
  21.  
  22. iii) If the total’s last digit is 0 (or, put more formally, if the total
  23. modulo 10 is congruent to 0), the number is valid.
  24.  
  25.  
  26. In credit.c, write a program that prompts the user for a credit card number
  27. and then reports (via printf) whether it is a valid American Express, MasterCard,
  28. or Visa card number, per the definitions of each’s format herein.
  29. So that we can automate some tests of your code, we ask that your program’s last
  30. line of output be AMEX\n or MASTERCARD\n or VISA\n or INVALID\n, nothing more,
  31. nothing less. For simplicity, you may assume that the user’s input will be entirely
  32. numeric (i.e., devoid of hyphens, as might be printed on an actual card).
  33. But do not assume that the user’s input will fit in an int! Best to use GetLongLong
  34. from CS50’s library to get users’ input. (Why?)
  35.  
  36. Of course, to use GetLongLong, you’ll need to tell gcc about CS50’s library. Be sure
  37. to put
  38.  
  39. #include <cs50.h>
  40.  
  41. toward the top of credit.c.
  42. And be sure to compile your code with a command like the below.
  43.  
  44. gcc -o credit credit.c -lcs50
  45.  
  46. Consider the below representative of how your own program should behave when passed a
  47. valid credit card number (sans hyphens); highlighted in bold is some user’s input.
  48.  
  49. user@appliance (~): ./credit
  50. Number: 378282246310005
  51. AMEX
  52.  
  53.  
  54. Of course, GetLongLong itself will reject hyphens (and more)
  55. anyway:
  56.  
  57. user@appliance (~): ./credit
  58. Number: 3782-822-463-10005
  59. Retry: foo
  60. Retry: 378282246310005
  61. AMEX
  62.  
  63.  
  64. But it’s up to you to catch inputs that are not credit card numbers
  65. (e.g., my phone number), even if numeric:
  66.  
  67. user@appliance (~): ./credit
  68. Number: 6175230925
  69. INVALID
  70.  
Not running #stdin #stdout 0s 0KB
stdin
Standard input is empty
stdout
Standard output is empty