Created
August 20, 2018 13:29
-
-
Save AbdelrahmanSherifHadeya/a037c2eec8103e6bd06b0ba2c7590190 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <cs50.h> | |
#include <stdio.h> | |
int company0(long long Number); | |
int company1(long long Number); | |
int summation(long long Number, int digit); | |
int main(void) | |
{ | |
//getting intial valid number from the user | |
long long Number; | |
do{ | |
Number = get_long_long("Number: "); | |
}while(Number < 400000000 || Number > 10000000000000000);//it was like this before but you didn't like (Number < 4000000000000 || Number > 10000000000000000); | |
int LN = company0(Number); | |
int sum = company1(Number); | |
if((LN == 34 || LN == 37) && (sum % 10 == 0)) | |
{ | |
printf("AMEX\n"); | |
} | |
else if((LN > 50 && LN < 56) && (sum % 10 == 0)) | |
{ | |
printf("MASTERCARD\n"); | |
} | |
else if((LN > 39 && LN < 50) && (sum % 10 == 0)) | |
{ | |
printf("VISA\n"); | |
} | |
else | |
{ | |
printf("INVALID\n"); | |
} | |
} | |
int summation(long long Number, int digit) //The summation function which depends on the other 2 functions below | |
{ | |
int i, sum = 0, xsum =0, x; | |
for(i = 0; i < digit; i++) | |
{ | |
if(i%2 == 0) | |
{ | |
x = Number % 10; | |
sum += x; | |
Number /= 10; | |
} | |
else | |
{ | |
x = Number % 10; | |
x *= 2; | |
if(x < 10) | |
{ | |
xsum += x; | |
Number /= 10; | |
} | |
else | |
{ | |
x %= 10; | |
xsum += x + 1; | |
Number /= 10; | |
} | |
} | |
} | |
return sum + xsum; | |
} | |
int company1(long long Number) //calculate the number of the digits in the credit card number to use it later and call another function for that | |
{ | |
int digit = 1; | |
long long original = Number; | |
while(Number > 10) | |
{ | |
Number /= 10; | |
digit++; | |
} | |
if(digit < 13) | |
{ | |
printf("INVALID\n"); | |
return 0; | |
} | |
int sum = summation(original, digit); | |
return sum; | |
} | |
int company0(long long Number) //check initially if it's fit any of the companies | |
{ | |
int lastNumber; | |
while(Number > 100) | |
{ | |
Number /= 10; | |
} | |
lastNumber = Number; | |
return lastNumber; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment