Last active
October 16, 2020 10:09
-
-
Save teeschorle/5c1ce78c220abf6d8471907b7aaf4baf to your computer and use it in GitHub Desktop.
CS50 Problem Set 1 (Fall 2019) - Credit
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
//CS50 Problem Set 1 (Fall 2019): Credit | |
//Author: teeschorle | |
#include <cs50.h> | |
#include <stdio.h> | |
#include <math.h> | |
void checksum(string card); | |
int digit(int position); | |
long number; | |
int digits; | |
int main(void) | |
{ | |
number = get_long("Number: "); | |
{ | |
//count digits | |
digits = trunc(log10(number)) + 1; | |
//case: AmEx | |
if (digit(1) == 3 && (digit(2) == 4 || digit(2) == 7) && digits == 15) | |
{ | |
checksum("AMEX"); | |
} | |
//case: MasterCard | |
else if (digit(1) == 5 && ((digit(2) < 6) || (digit(2) > 0)) && digits == 16) | |
{ | |
checksum("MASTERCARD"); | |
} | |
//case: Visa | |
else if (digit(1) == 4 && (digits == 13 || digits == 16)) | |
{ | |
checksum("VISA"); | |
} | |
//case: none of the above | |
else | |
{ | |
printf("INVALID\n"); | |
} | |
} | |
} | |
//return digit at n-th position in card number | |
int digit(int n) | |
{ | |
return ((long)(number / pow(10, digits - n))) % 10; | |
} | |
//check sum | |
void checksum(string card) | |
{ | |
int sum = 0; | |
//doubling every other digit and summing up results by digits | |
for (int i = digits - 1; i > 0; i = i - 2) | |
{ | |
if (digit(i) < 5) | |
{ | |
sum = sum + 2 * digit(i); | |
} | |
else | |
{ | |
sum = sum + 1 + ((2 * digit(i)) % 10); | |
} | |
} | |
//adding those digits that haven't been doubled | |
for (int i = digits; i > 0; i = i - 2) | |
{ | |
sum = sum + digit(i); | |
} | |
//check if last digit of sum equals zero | |
if (sum % 10 == 0) | |
{ | |
printf("%s\n", card); | |
} | |
else | |
{ | |
printf("INVALID\n"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
help me
#include <stdio.h>
#include <cs50.h>
void print_credit_card_brand(long long ccn);
bool check_validity(long long credit_card_number);
int find_length(long long n);
bool checksum(long long ccn);
int main(void){
long long credit_card_number;
do
{
credit_card_number= get_long_long("number: ");
}while(credit_card_number<0);
if((credit_card_number))
print_credit_card_brand(credit_card_number);
else
printf("INVALID\n");
}
bool check_validity(long long credit_card_number )
{
int len = find_length (credit_card_number);
return (len==13 || len==15 || len ==16) && checksum(credit_card_number);
}
int fine_length(long long n)
{
int len ;
for(len =0 ; n != 0; n /= 10 , len++);
return len;
}
bool checksum(long long ccn)
{
int sum=0;
for(int i=0 ; ccn != 0 ;i++ , ccn /=10)
{
if(i%2==0)
sum += ccn % 10;
else{
int digit= 2 * (ccn % 10);
sum += digit /10 + digit % 10;
}
}
return (sum %10)==0;
}
void print_credit_card_brand(long long ccn)
{
if((ccn>=34e13 && ccn<35e13) ||(ccn>=37e13 && ccn<38e13) )
printf("AMEX\n");
else if (ccn>=51e14 && ccn<56e14)
printf("MASTERCARD\n");
if((ccn>=4e12 && ccn<5e12) ||(ccn>=4e15 && ccn<5e15) )
printf("VISA\n");
else
printf("INVALID");
}