I have written a (clumsy) solution to the credit pset1. However, the solution only works for "Mastercard" credit card numbers (i.e. 5555555555554444). For "Visa" and "Amex" my code does return the proper credit card type, but at the same time it displays "Invalid". My question is: how to fix this if-else statement issue?
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
//Prompt for the credit card number
printf("What is your creditcard number?\n");
long number = get_long("Enter number: ");
int d = 2;
long ten = 10;
long base = number;
long x1, x2, x3, x4, x5, x6, x7, x8;
long y1, y2, y3, y4, y5, y6, y7, y8;
long a1, a2, a3, a4, a5, a6, a7;
long b1, b2, b3, b4, b5, b6, b7, b8;
//Only consider VISA credit cards starting with a 4 and 16 digits total
if(4000000000000000 <= base && base < 5000000000000000)
{
//Capture each digit separately
b8 = base % 10;
x1 = (base / (pow(ten, 1)));
y1 = x1 % 10;
a1 = (base / (pow(ten, 2)));
b1 = a1 % 10;
x2 = (base / (pow(ten, 3)));
y2 = x2 % 10;
a2 = (base / (pow(ten, 4)));
b2 = a2 % 10;
x3 = (base / (pow(ten, 5)));
y3 = x3 % 10;
a3 = (base / (pow(ten, 6)));
b3 = a3 % 10;
x4 = (base / (pow(ten, 7)));
y4 = x4 % 10;
a4 = (base / (pow(ten, 8)));
b4 = a4 % 10;
x5 = (base / (pow(ten, 9)));
y5 = x5 % 10;
a5 = (base / (pow(ten, 10)));
b5 = a5 % 10;
x6 = (base / (pow(ten, 11)));
y6 = x6 % 10;
a6 = (base / (pow(ten, 12)));
b6 = a6 % 10;
x7 = (base / (pow(ten, 13)));
y7 = x7 % 10;
a7 = (base / (pow(ten, 14)));
b7 = a7 % 10;
x8 = (base / (pow(ten, 15)));
y8 = x8 % 10;
//Print the separate digits to check
printf("VISA\n");
printf("First number: %ld\n", y8);
printf("Second number: %ld\n", b7);
printf("Third number: %ld\n", y7);
printf("Fourth number: %ld\n", b6);
printf("Fifth number: %ld\n", y6);
printf("Sixth number: %ld\n", b5);
printf("Seventh number: %ld\n", y5);
printf("Eight number: %ld\n", b4);
printf("Ninth number: %ld\n", y4);
printf("Tenth number: %ld\n", b3);
printf("Eleventh number: %ld\n", y3);
printf("Twelveth number: %ld\n", b2);
printf("Thirteenth number: %ld\n", y2);
printf("Fourteenth number: %ld\n", b1);
printf("Fifteenth number: %ld\n", y1);
printf("Sixteenth number: %ld\n", b8);
//Multiply every other digit by 2
long xy1 = y1 * d;
long xy2 = y2 * d;
long xy3 = y3 * d;
long xy4 = y4 * d;
long xy5 = y5 * d;
long xy6 = y6 * d;
long xy7 = y7 * d;
long xy8 = y8 * d;
//Capture each digit separately after multiplying
long xy11 = xy1 % 10;
long xy12 = xy1 / 10;
long xy21 = xy2 % 10;
long xy22 = xy2 / 10;
long xy31 = xy3 % 10;
long xy32 = xy3 / 10;
long xy41 = xy4 % 10;
long xy42 = xy4 / 10;
long xy51 = xy5 % 10;
long xy52 = xy5 / 10;
long xy61 = xy6 % 10;
long xy62 = xy6 / 10;
long xy71 = xy7 % 10;
long xy72 = xy7 / 10;
long xy81 = xy8 % 10;
long xy82 = xy8 / 10;
//Calculate the separate sums
long xysum = xy11 + xy12 + xy21 + xy22 + xy31 + xy32 + xy41 + xy42 + xy51 + xy52 + xy61 + xy62 + xy71 + xy72 + xy81 + xy82;
long bsum = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8;
long total = xysum + bsum;
//Determine the last digit of the total sum
long modus = total % 10;
printf("The first sum is: %ld\n", xysum);
printf("The second sum is: %ld\n", bsum);
printf("The total sum is: %ld\n", total);
//Check if the last digit of the total sum is equal to zero
if(modus == 0)
{
printf("VISA\n");
}
}
//Only consider VISA credit cards starting with a 4 and 13 digits total
if(4000000000000 <= base && base < 5000000000000)
{
//Capture each digit separately
b8 = base % 10;
x1 = (base / (pow(ten, 1)));
y1 = x1 % 10;
a1 = (base / (pow(ten, 2)));
b1 = a1 % 10;
x2 = (base / (pow(ten, 3)));
y2 = x2 % 10;
a2 = (base / (pow(ten, 4)));
b2 = a2 % 10;
x3 = (base / (pow(ten, 5)));
y3 = x3 % 10;
a3 = (base / (pow(ten, 6)));
b3 = a3 % 10;
x4 = (base / (pow(ten, 7)));
y4 = x4 % 10;
a4 = (base / (pow(ten, 8)));
b4 = a4 % 10;
x5 = (base / (pow(ten, 9)));
y5 = x5 % 10;
a5 = (base / (pow(ten, 10)));
b5 = a5 % 10;
x6 = (base / (pow(ten, 11)));
y6 = x6 % 10;
a6 = (base / (pow(ten, 12)));
b6 = a6 % 10;
//Print the separate digits to check
printf("VISA\n");
printf("First number: %ld\n", b6);
printf("Second number: %ld\n", y6);
printf("Third number: %ld\n", b5);
printf("Fourth number: %ld\n", y5);
printf("Fifth number: %ld\n", b4);
printf("Sixth number: %ld\n", y4);
printf("Seventh number: %ld\n", b3);
printf("Eight number: %ld\n", y3);
printf("Ninth number: %ld\n", b2);
printf("Tenth number: %ld\n", y2);
printf("Eleventh number: %ld\n", b1);
printf("Twelveth number: %ld\n", y1);
printf("Thirteenth number: %ld\n", b8);
//Multiply every other digit by 2
long xy1 = y1 * d;
long xy2 = y2 * d;
long xy3 = y3 * d;
long xy4 = y4 * d;
long xy5 = y5 * d;
long xy6 = y6 * d;
//Capture each digit separately after multiplying
long xy11 = xy1 % 10;
long xy12 = xy1 / 10;
long xy21 = xy2 % 10;
long xy22 = xy2 / 10;
long xy31 = xy3 % 10;
long xy32 = xy3 / 10;
long xy41 = xy4 % 10;
long xy42 = xy4 / 10;
long xy51 = xy5 % 10;
long xy52 = xy5 / 10;
long xy61 = xy6 % 10;
long xy62 = xy6 / 10;
//Calculate the separate sums
long xysum = xy11 + xy12 + xy21 + xy22 + xy31 + xy32 + xy41 + xy42 + xy51 + xy52 + xy61 + xy62;
long bsum = b1 + b2 + b3 + b4 + b5 + b6;
long total = xysum + bsum;
//Determine the last digit of the total sum
long modus = total % 10;
printf("The first sum is: %ld\n", xysum);
printf("The second sum is: %ld\n", bsum);
printf("The total sum is: %ld\n", total);
//Check if the last digit of the total sum is equal to zero
if(modus == 0)
{
printf("VISA\n");
}
}
//Only consider AMEX credit cards starting with a 34 and 15 digits total
if(340000000000000 <= base && base < 350000000000000)
{
//Capture each digit separately
b8 = base % 10;
x1 = (base / (pow(ten, 1)));
y1 = x1 % 10;
a1 = (base / (pow(ten, 2)));
b1 = a1 % 10;
x2 = (base / (pow(ten, 3)));
y2 = x2 % 10;
a2 = (base / (pow(ten, 4)));
b2 = a2 % 10;
x3 = (base / (pow(ten, 5)));
y3 = x3 % 10;
a3 = (base / (pow(ten, 6)));
b3 = a3 % 10;
x4 = (base / (pow(ten, 7)));
y4 = x4 % 10;
a4 = (base / (pow(ten, 8)));
b4 = a4 % 10;
x5 = (base / (pow(ten, 9)));
y5 = x5 % 10;
a5 = (base / (pow(ten, 10)));
b5 = a5 % 10;
x6 = (base / (pow(ten, 11)));
y6 = x6 % 10;
a6 = (base / (pow(ten, 12)));
b6 = a6 % 10;
x7 = (base / (pow(ten, 13)));
y7 = x7 % 10;
a7 = (base / (pow(ten, 14)));
b7 = a7 % 10;
//Print the separate digits to check
printf("AMEX\n");
printf("First number: %ld\n", b7);
printf("Second number: %ld\n", y7);
printf("Third number: %ld\n", b6);
printf("Fourth number: %ld\n", y6);
printf("Fifth number: %ld\n", b5);
printf("Sixth number: %ld\n", y5);
printf("Seventh number: %ld\n", b4);
printf("Eight number: %ld\n", y4);
printf("Ninth number: %ld\n", b3);
printf("Tenth number: %ld\n", y3);
printf("Eleventh number: %ld\n", b2);
printf("Twelveth number: %ld\n", y2);
printf("Thirteenth number: %ld\n", b1);
printf("Fourteenth number: %ld\n", y1);
printf("Fifteenth number: %ld\n", b8);
//Multiply every other digit by 2
long xy1 = y1 * d;
long xy2 = y2 * d;
long xy3 = y3 * d;
long xy4 = y4 * d;
long xy5 = y5 * d;
long xy6 = y6 * d;
long xy7 = y7 * d;
//Capture each digit separately after multiplying
long xy11 = xy1 % 10;
long xy12 = xy1 / 10;
long xy21 = xy2 % 10;
long xy22 = xy2 / 10;
long xy31 = xy3 % 10;
long xy32 = xy3 / 10;
long xy41 = xy4 % 10;
long xy42 = xy4 / 10;
long xy51 = xy5 % 10;
long xy52 = xy5 / 10;
long xy61 = xy6 % 10;
long xy62 = xy6 / 10;
long xy71 = xy7 % 10;
long xy72 = xy7 / 10;
//Calculate the separate sums
long xysum = xy11 + xy12 + xy21 + xy22 + xy31 + xy32 + xy41 + xy42 + xy51 + xy52 + xy61 + xy62 + xy71 + xy72;
long bsum = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8;
long total = xysum + bsum;
//Determine the last digit of the total sum
long modus = total % 10;
printf("The first sum is: %ld\n", xysum);
printf("The second sum is: %ld\n", bsum);
printf("The total sum is: %ld\n", total);
//Check if the last digit of the total sum is equal to zero
if(modus == 0)
{
printf("AMEX\n");
}
}
//Only consider AMEX credit cards starting with a 37 and 15 digits total
if(370000000000000 <= base && base < 380000000000000)
{
//Capture each digit separately
b8 = base % 10;
x1 = (base / (pow(ten, 1)));
y1 = x1 % 10;
a1 = (base / (pow(ten, 2)));
b1 = a1 % 10;
x2 = (base / (pow(ten, 3)));
y2 = x2 % 10;
a2 = (base / (pow(ten, 4)));
b2 = a2 % 10;
x3 = (base / (pow(ten, 5)));
y3 = x3 % 10;
a3 = (base / (pow(ten, 6)));
b3 = a3 % 10;
x4 = (base / (pow(ten, 7)));
y4 = x4 % 10;
a4 = (base / (pow(ten, 8)));
b4 = a4 % 10;
x5 = (base / (pow(ten, 9)));
y5 = x5 % 10;
a5 = (base / (pow(ten, 10)));
b5 = a5 % 10;
x6 = (base / (pow(ten, 11)));
y6 = x6 % 10;
a6 = (base / (pow(ten, 12)));
b6 = a6 % 10;
x7 = (base / (pow(ten, 13)));
y7 = x7 % 10;
a7 = (base / (pow(ten, 14)));
b7 = a7 % 10;
//Print the separate digits to check
printf("AMEX\n");
printf("First number: %ld\n", b7);
printf("Second number: %ld\n", y7);
printf("Third number: %ld\n", b6);
printf("Fourth number: %ld\n", y6);
printf("Fifth number: %ld\n", b5);
printf("Sixth number: %ld\n", y5);
printf("Seventh number: %ld\n", b4);
printf("Eight number: %ld\n", y4);
printf("Ninth number: %ld\n", b3);
printf("Tenth number: %ld\n", y3);
printf("Eleventh number: %ld\n", b2);
printf("Twelveth number: %ld\n", y2);
printf("Thirteenth number: %ld\n", b1);
printf("Fourteenth number: %ld\n", y1);
printf("Fifteenth number: %ld\n", b8);
//Multiply every other digit by 2
long xy1 = y1 * d;
long xy2 = y2 * d;
long xy3 = y3 * d;
long xy4 = y4 * d;
long xy5 = y5 * d;
long xy6 = y6 * d;
long xy7 = y7 * d;
//Capture each digit separately after multiplying
long xy11 = xy1 % 10;
long xy12 = xy1 / 10;
long xy21 = xy2 % 10;
long xy22 = xy2 / 10;
long xy31 = xy3 % 10;
long xy32 = xy3 / 10;
long xy41 = xy4 % 10;
long xy42 = xy4 / 10;
long xy51 = xy5 % 10;
long xy52 = xy5 / 10;
long xy61 = xy6 % 10;
long xy62 = xy6 / 10;
long xy71 = xy7 % 10;
long xy72 = xy7 / 10;
//Calculate the separate sums
long xysum = xy11 + xy12 + xy21 + xy22 + xy31 + xy32 + xy41 + xy42 + xy51 + xy52 + xy61 + xy62 + xy71 + xy72;
long bsum = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8;
long total = xysum + bsum;
//Determine the last digit of the total sum
long modus = total % 10;
printf("The first sum is: %ld\n", xysum);
printf("The second sum is: %ld\n", bsum);
printf("The total sum is: %ld\n", total);
//Check if the last digit of the total sum is equal to zero
if(modus == 0)
{
printf("AMEX\n");
}
}
//Only consider MASTERCARD credit cards starting with 51, 52, 53, 54 or 55 and 16 digits total
if(5100000000000000 <= base && base < 5600000000000000)
{
//Capture each digit separately
b8 = base % 10;
x1 = (base / (pow(ten, 1)));
y1 = x1 % 10;
a1 = (base / (pow(ten, 2)));
b1 = a1 % 10;
x2 = (base / (pow(ten, 3)));
y2 = x2 % 10;
a2 = (base / (pow(ten, 4)));
b2 = a2 % 10;
x3 = (base / (pow(ten, 5)));
y3 = x3 % 10;
a3 = (base / (pow(ten, 6)));
b3 = a3 % 10;
x4 = (base / (pow(ten, 7)));
y4 = x4 % 10;
a4 = (base / (pow(ten, 8)));
b4 = a4 % 10;
x5 = (base / (pow(ten, 9)));
y5 = x5 % 10;
a5 = (base / (pow(ten, 10)));
b5 = a5 % 10;
x6 = (base / (pow(ten, 11)));
y6 = x6 % 10;
a6 = (base / (pow(ten, 12)));
b6 = a6 % 10;
x7 = (base / (pow(ten, 13)));
y7 = x7 % 10;
a7 = (base / (pow(ten, 14)));
b7 = a7 % 10;
x8 = (base / (pow(ten, 15)));
y8 = x8 % 10;
//Print the separate digits to check
printf("MASTERCARD\n");
printf("First number: %ld\n", y8);
printf("Second number: %ld\n", b7);
printf("Third number: %ld\n", y7);
printf("Fourth number: %ld\n", b6);
printf("Fifth number: %ld\n", y6);
printf("Sixth number: %ld\n", b5);
printf("Seventh number: %ld\n", y5);
printf("Eight number: %ld\n", b4);
printf("Ninth number: %ld\n", y4);
printf("Tenth number: %ld\n", b3);
printf("Eleventh number: %ld\n", y3);
printf("Twelveth number: %ld\n", b2);
printf("Thirteenth number: %ld\n", y2);
printf("Fourteenth number: %ld\n", b1);
printf("Fifteenth number: %ld\n", y1);
printf("Sixteenth number: %ld\n", b8);
//Multiply every other digit by 2
long xy1 = y1 * d;
long xy2 = y2 * d;
long xy3 = y3 * d;
long xy4 = y4 * d;
long xy5 = y5 * d;
long xy6 = y6 * d;
long xy7 = y7 * d;
long xy8 = y8 * d;
//Capture each digit separately after multiplying
long xy11 = xy1 % 10;
long xy12 = xy1 / 10;
long xy21 = xy2 % 10;
long xy22 = xy2 / 10;
long xy31 = xy3 % 10;
long xy32 = xy3 / 10;
long xy41 = xy4 % 10;
long xy42 = xy4 / 10;
long xy51 = xy5 % 10;
long xy52 = xy5 / 10;
long xy61 = xy6 % 10;
long xy62 = xy6 / 10;
long xy71 = xy7 % 10;
long xy72 = xy7 / 10;
long xy81 = xy8 % 10;
long xy82 = xy8 / 10;
//Calculate the separate sums
long xysum = xy11 + xy12 + xy21 + xy22 + xy31 + xy32 + xy41 + xy42 + xy51 + xy52 + xy61 + xy62 + xy71 + xy72 + xy81 + xy82;
long bsum = b1 + b2 + b3 + b4 + b5 + b6 + b7 + b8;
long total = xysum + bsum;
//Determine the last digit of the total sum
long modus = total % 10;
printf("The first sum is: %ld\n", xysum);
printf("The second sum is: %ld\n", bsum);
printf("The total sum is: %ld\n", total);
//Check if the last digit of the total sum is equal to zero
if(modus == 0)
{
printf("MASTERCARD\n");
}
}
else
{
printf("INVALID\n");
}
}