Advertisement
Jodyone

credit.c

Aug 20th, 2014
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.64 KB | None | 0 0
  1. /**
  2.  * credit.c
  3.  *
  4.  * Jody W Moore
  5.  *
  6.  * this program takes in a credit card number
  7.  * and returns VALID or INVALID number.  
  8.  *
  9.  **/
  10. #include <cs50.h>
  11. #include <math.h>
  12. #include <stdio.h>
  13.  
  14. int main(void)
  15. {
  16.  
  17.     long long num = 0;
  18.     int digit = 0;
  19.     int odd = 0;
  20.     int ctr = 0;
  21.     int prod = 0;
  22.     int prod2 = 0;
  23.     int cheksum = 0;
  24.     int validater = 0;
  25.     int even = 0;
  26.     int id1 = 0;
  27.  
  28.     // promt user for credit card # using GetLongLong
  29.     do
  30.     {
  31.         printf("Number: ");
  32.         num = GetLongLong();
  33.     }
  34.     while(num <= 0);
  35.  
  36.     while (num > 0 )
  37.     {
  38.         digit = num % 10;  
  39.         num = num / 10;
  40.         ctr++;
  41.    
  42.         if ( ctr != 0 )
  43.         {
  44.             odd = digit + odd;
  45.         }
  46.         if ( ctr == 0 )
  47.         {
  48.             prod = digit * 2 ;
  49.                  
  50.             if (prod <= 9)
  51.             {
  52.                 even = even + prod;
  53.             }
  54.             else
  55.             {
  56.                 int temp_num = prod % 10;
  57.                 prod2 = temp_num + 1;
  58.                 even = even + prod2 ;
  59.             }            
  60.          }
  61.     }
  62.    
  63.     id1 = digit;
  64.     cheksum = even + odd;
  65.     validater = cheksum % 10;
  66.    
  67.     if ( validater != 0)
  68.     {
  69.         printf("INVALID\n");    
  70.     }
  71.     else
  72.     {
  73.         if ( id1 == 3 )
  74.         {
  75.             printf("AMEX\n");
  76.         }
  77.         else if ( id1 == 4 )
  78.         {
  79.             printf("VISA\n");
  80.         }
  81.         else if (id1 == 5 )
  82.         {
  83.             printf("MASTERCARD\n");
  84.         }
  85.         else if ( id1 == 6)
  86.         {
  87.             printf("Discover\n");
  88.         }
  89.     }  
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement