Guest User

Untitled

a guest
May 25th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.20 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<cs50.h>
  3.  
  4. int main(void)
  5. {
  6. long long ccnum;
  7. do{
  8. printf("Enter a number: ");
  9. ccnum = get_long_long();
  10. }
  11. while(ccnum<0);
  12.  
  13. //"count" variable to count no. of digits in ccnumber
  14. int count=0;
  15. long num=ccnum;
  16.  
  17. //this while loop counts no.of digits in ccnumber
  18. while(num>0)
  19. {
  20. num=num/10;
  21. count++;
  22. }
  23.  
  24. //below if condn checks ccnumber contains how many digits and sees if valid or not
  25. if(count!=15 && count!=13 && count!=16)
  26. {
  27. printf("INVALID\n");
  28. }
  29. else{
  30. //below array stores the ccnum in reverse order =>1 digit each
  31. int number[count];
  32. for(int i=0;i<count;i++)
  33. {
  34. number[i]=(int) (ccnum%10);
  35. ccnum=ccnum/10;
  36. }
  37.  
  38. //below array stores the same reverse array by eliminating 1st num and makes it '0' kind of empty
  39. int orignumber[count];
  40. for(int i=1;i<count;i++)
  41. {
  42. orignumber[i]=number[i];
  43. }
  44.  
  45. //below process multiplies '2' to the 'number' array elements in index 1,3,5...so on and
  46. //if any of it is double digit it makes it single als0(while loop)
  47. int digit =0;
  48. for(int i=1;i<count;i+=2)
  49. {
  50. number[i]*=2;
  51. while(number[i]>9)
  52. {
  53. digit=number[i]%10;
  54. number[i]=number[i]/10;
  55. number[i]=number[i]+digit;
  56. }
  57.  
  58. }
  59. //this process adds all the digits which are multiplied by 2 and total is called 'sum2nd'
  60. int sum2nd=0;
  61. for(int i=1;i<count;i+=2)
  62. {
  63. sum2nd=sum2nd+number[i];
  64. }
  65.  
  66. //this process adds 'sum2nd' and the remaining elements which 'not' multiplied by 2
  67. int checksum=sum2nd;
  68. for(int i=0;i<count;i+=2)
  69. {
  70. checksum=checksum+number[i];
  71. }
  72. //this checks if the totalsum(checksum) ends in zero or not
  73. if(checksum%10==0)
  74. {
  75.  
  76. //visa starts with 4
  77. if(orignumber[count-1]==4)
  78. {
  79. printf("VISA\n");
  80. }
  81. //visa starts with 4
  82. else if((orignumber[count-2]==7) && (orignumber[count-1]==4||orignumber[count-1]==3))
  83. {
  84. printf("AMEX\n");
  85. }
  86. //Amex starts with 34,37
  87. else if((orignumber[count-1]==5) && (orignumber[count-2]==1||orignumber[count-2]==2||orignumber[count-2]==3||orignumber[count-2]==4||orignumber[count-2]==5))
  88. {
  89. printf("MASTERCARD\n");
  90. }
  91. //master card starts with 51,52,53,54,55
  92. else
  93. {
  94. printf("INVALID\n");
  95. }
  96. }
  97. else
  98. {
  99. printf("INVALID\n");
  100. }
  101. }
  102. }
Add Comment
Please, Sign In to add comment