Skip to content

Instantly share code, notes, and snippets.

@dasunsucharith
Created April 15, 2022 07:26
Show Gist options
  • Save dasunsucharith/1cc69afb864c1fa6e3654042edf30c8b to your computer and use it in GitHub Desktop.
Save dasunsucharith/1cc69afb864c1fa6e3654042edf30c8b to your computer and use it in GitHub Desktop.
My solution to CS50 2022 psets-2 credit
#include <cs50.h>
#include <stdio.h>
//AMERX 15 STRT 34 OR 37
//MC 16 STRT 51, 52, 53, 54, 55
//VZA 13 OR 16 STRT 4
int main(void)
{
long creditNumber;
do
{
creditNumber = get_long("Number: ");
}
while (creditNumber <= 0);
long creditCard = creditNumber;
int sum = 0;
int count =0;
long divider = 10;
while (creditCard > 0)
{
int lastDigit = creditCard % 10;
sum = sum + lastDigit;
creditCard = creditCard / 100;
}
creditCard = creditNumber / 10;
while (creditCard > 0)
{
int lastDigit = creditCard % 10;
int byTwo = lastDigit * 2;
sum = sum + (byTwo % 10) + (byTwo / 10);
creditCard = creditCard / 100;
}
creditCard = creditNumber;
while (creditCard != 0)
{
creditCard = creditCard / 10;
count++;
}
for (int i = 0; i < count - 2; i++)
{
divider = divider * 10;
}
int firstDigit = creditNumber / divider;
int firstTwoDigits = creditNumber / (divider / 10);
if ((sum % 10) == 0)
{
if (firstDigit == 4 && (count == 13 || count == 16))
{
printf("VISA\n");
}
else if ((firstTwoDigits == 34 || firstTwoDigits == 37) && count == 15)
{
printf("AMEX\n");
}
else if ((firstTwoDigits > 50 && firstTwoDigits < 56) && count == 16)
{
printf("MASTERCARD\n");
}
else
{
printf("INVALID\n");
}
}
else {
printf("INVALID\n");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment