int main(void) { printf("Number of Days in month: "); int days = GetInt(); if ((days > 31) || (days < 28)) { do{ printf("Try again. Months cannot have >31 or <28 days: "); days = GetInt(); } while ((days > 31) || (days < 28)); } printf("Number of pennies: "); long long pennies = GetLongLong(); if (pennies < 0) { do{ printf("Try again. Must be non-negative amount of pennies: "); pennies = GetLongLong(); } while (pennies < 0); } for (int i = 0; i < days; i++) pennies = pennies*2; pennies = pennies/100; printf("$%.1lld \n", pennies); }
int main(void) { //initialize variables long long t; long long p; //get the number of days do { printf("Days in month: "); t = GetInt(); } while (t < 28 || t > 31); //get the number of pennies do { printf("Pennies on first day: "); p = GetLongLong(); } while (p <= 0); //multiply by two a day, convert to dollars & print printf("$%.2f\n", ((pow(2, t)-1)*p)/100.0); }
int main(void) { int maxLength = 16; int minLength = 13; printf("Number: "); long long cardNumber = GetLongLong(); int length = maxLength; long long divider = (long long) pow(10, length - 1); int headDigit = 0; long long tailDigits = cardNumber; // Find length of card number for(length = maxLength; length >= minLength && headDigit == 0; length--) { headDigit = (int) (tailDigits / divider); tailDigits = tailDigits % divider; divider /= 10; } // Compensate -- on last iteration length++; if (headDigit == 3 && length == 15) { // Maybe AMEX checkCSFor(calculateCS(cardNumber, length), "AMEX"); } else if (headDigit == 4 && (length == 13 || length == 16)) { // Maybe VISA checkCSFor(calculateCS(cardNumber, length), "VISA"); } else if (headDigit == 5 && length == 16) { // Maybe MASTERCARD int secondDigit = (int) (tailDigits / divider); if (secondDigit != 1 && secondDigit != 2 && secondDigit != 3 && secondDigit != 4 && secondDigit != 5) { printf("INVALID\n"); } else { checkCSFor(calculateCS(cardNumber, length), "MASTERCARD"); } } else { printf("INVALID\n"); } }
int main(void) { long long cardNumber; do { printf("Number: "); cardNumber = GetLongLong(); } while(cardNumber < 0); char *sCardNo = asString(cardNumber); int length = strlen(sCardNo); if (length < 13 || length > 16) { printf("INVALID\n"); } else { int index = 0, checkSum = 0; // we start reading the individual digits from right to left // or from the least significant digit to the most significant // digit while(cardNumber) { int digit = cardNumber % 10; if ((index % 2) == 0) { checkSum += digit; } else { // if product exceeds 9, add the digits together int product = (digit << 1); checkSum += (product > 9) ? (product % 10) + 1 : product; } cardNumber /= 10; index++; } int mod = checkSum % 10; if (mod) { printf("INVALID\n"); } else { printf("%s\n", getProvider(sCardNo)); } free(sCardNo); } }
int main(void) { printf("Give me your credit card number: "); long long cardNumber = GetLongLong(); int lengthNumber = floor(log10(cardNumber));// length of card number without 1 digit. long long power = pow (10, lengthNumber); int sumDigits = 0; int firstDigit = 0; int secondDigit = 0; int i = 0; long long curdNumberLeft = 0; string typeCard = "INVALID"; //check the number - delete //printf("Your card is %lld and length is %lld\n", cardNumber, power); curdNumberLeft = cardNumber; //restuta is cool, love him - I love him already ))) for (i = 0; i <= (lengthNumber + 1); i = i + 2) { firstDigit = curdNumberLeft/power; printf("%i", firstDigit); curdNumberLeft = curdNumberLeft - firstDigit * power; power = power/10; if (power >= 10) { secondDigit = curdNumberLeft/power; printf("%i", secondDigit); curdNumberLeft = curdNumberLeft - secondDigit * power; secondDigit = secondDigit * 2; power = power/10; if (secondDigit > 9) secondDigit = secondDigit / 10 + secondDigit - (secondDigit / 10) * 10; //ask Restuta! stupid girl!!! } else secondDigit = 0; if (i == 0) { if (firstDigit == 3 && (secondDigit == 4 || secondDigit == 7)) typeCard = "AMEX"; if (firstDigit == 5 && secondDigit > 0 && secondDigit < 6) typeCard = "MASTERCARD"; if (firstDigit == 4) typeCard = "VISA"; } sumDigits = sumDigits + firstDigit + secondDigit; } if (sumDigits % 10 > 0) typeCard = "INVALID"; //printf(" Sum is %i\n", sumDigits); printf(" is %s\n", typeCard); }
long long ask_card_numb() { long long rslt; do { printf("Enter number of your credit card: "); rslt = GetLongLong(); } while (rslt < 0); return rslt; }
int UJNumericInt(UJObject obj) { switch ( ((Item *) obj)->type) { case UJT_Long: return (int) GetLong(obj); case UJT_LongLong: return (int) GetLongLong(obj); case UJT_Double: return (int) GetDouble(obj); default: break; } return 0; }
long long UJNumericLongLong(UJObject *obj) { switch ( ((Item *) obj)->type) { case UJT_Long: return (long long) GetLong(obj); case UJT_LongLong: return (long long) GetLongLong(obj); case UJT_Double: return (long long) GetDouble(obj); default: break; } return 0; }
double UJNumericFloat(UJObject *obj) { switch ( ((Item *) obj)->type) { case UJT_Long: return (double) GetLong(obj); case UJT_LongLong: return (double) GetLongLong(obj); case UJT_Double: return (double) GetDouble(obj); default: break; } return 0.0; }
int main(void) { float a = 0; long b = 0; int c = 0; printf("Enter the credit card no. : "); b = GetLongLong(); a = b / 10; if ( a > 0) c = a; printf("%i,%f",c, a); }
ull get_number(void) { ull n; do { printf("Number: "); n = GetLongLong(); } while (n < 1); return n; }
int main() { // get input from user and save it to variable printf("Enter some number\n"); long long input = GetLongLong(); unsigned long sum = sumDigits(input); // display result printf("Sum is %lu\n", sum); return 0; }
/*get credit card number*/ long long get_credit_num(void) { long long credit_number; //solicit a positive credit card number from the user do { printf("Enter credit card number: "); credit_number = GetLongLong(); } while(credit_number <=0); return credit_number; }
int main(void) { printf("Number:"); long long ccnumber = GetLongLong(); int checksum1, checksum2, cclength; checksum1 = 0; checksum2 = 0; cclength = 0; for (long long i = ccnumber; i > 0;) { checksum1 += i % 10; if(i > 0) { cclength++; } i = i / 10; checksum2 += (2*(i % 10))%10; checksum2 += ((2*(i % 10))/10)%10; if(i > 0) { cclength++; } i = i / 10; } int cccheck = checksum1 + checksum2; for (int i = 0; i < (cclength - 1); i++) { ccnumber = ccnumber / 10; } if ( (cccheck % 10 == 0) && (12 < cclength < 17) ) { switch(ccnumber) { case 3: printf("AMEX\n"); break; case 4: printf("VISA\n"); break; case 5: printf("MASTERCARD\n"); break; default: printf("INVALID\n"); } } else { printf("INVALID\n"); }; return 0; }
int main(void) { int sum1 = 0; int productRes = 0; printf("Number: "); long number = GetLongLong(); if (number >= 1000000000000 && number < 10000000000000) { //visa 13 digits printf("%d\n", (int)(number/1000000000000)); if (number/1000000000000 == 4) { for (int i = 0; i < 13; i += 2) { productRes = getDigitFromNum(number, i)*2; if (productRes > 9) { sum1 += getDigitFromNum(productRes, 0); sum1 += getDigitFromNum(productRes, 1); } else { sum1 += productRes; } } for (int i = 1; i < 13; i += 2) { sum1 += getDigitFromNum(number, i); } printf("%d", sum1); } else { printf("not Visa"); } } else if (number >= 100000000000000 && number < 10000000000000000) { //amex for (int i = 1; i < 15; i += 2) { productRes = getDigitFromNum(number, i)*2; printf("%d ", productRes); if (productRes > 9) { sum1 += getDigitFromNum(productRes, 0); sum1 += getDigitFromNum(productRes, 1); } else { sum1 += productRes; } } printf("\n%d\n", sum1); for (int i = 0; i < 15; i += 2) { sum1 += getDigitFromNum(number, i); } printf("%d\n", sum1); } else if (number >= 1000000000000000 && number < 100000000000000000) { } else { printf("not a number"); } }
int main(void) { long long number; do { printf("Number:"); number = GetLongLong(); } while (number < 0); if (number/1000000000000 == 0) { printf("INVALID\n"); return 0; } long long copy = number; int count = 0; int sum = 0; int digit = 0; while (number > 0) { digit = number - ((number/10)*10); //printf("digit = %d\n", digit); if (count % 2 != 0) digit *= 2; int digit1 = 0, digit2 = 0; if (digit > 9) { digit1 = digit - (digit/10)*10; digit2 = digit/10; digit = 0; //printf("d1= %d", digit1); //printf("d2=%d", digit2); } sum += digit1 + digit2 + digit; //printf("sum= %d\n", sum); number /= 10; count++; } //printf("%d\n", sum); if (sum % 10 == 0) { if (copy / 10000000000000 == 34 || copy / 10000000000000 == 37) printf("AMEX\n"); if (copy / 100000000000000 > 50 && copy / 100000000000000 < 56) printf("MASTERCARD\n"); if (copy / 1000000000000 == 4 || copy / 1000000000000000 == 4) printf("VISA\n"); } return 0; }
/* Determine the validity of a credit card number and identify which major card company that card belongs to. */ int main(void) { // Set up variables string output = "INVALID"; long long user_input; int cc_num_digits; bool is_valid_card_number; // Get the number as a float from user printf("Enter your credit card number:\n"); user_input = GetLongLong(); cc_num_digits = log10(user_input) + 1; // *** Determine if it is valid credit card is_valid_card_number =IsValidCreditCard(user_input); // *** If valid: Check AMEX, DISCOVER, or VISA if(is_valid_card_number) { // AMEX has 15 digits and must start with 34 or 37 int temp = GetFirstDigits(2, user_input); if( (34 == temp || 37 == temp) && (cc_num_digits == 15) ) { output = "AMEX"; } // MASTERCARD uses 16 digits and must start with 51, 52, 53, 54, or 55 if ( cc_num_digits == 16 && (51 == temp || 52 == temp || 53 == temp || 54 == temp || 55 == temp) ) { output = "MASTERCARD"; } // VISA uses 13 or 16 digits, and must start with a 4 temp = GetFirstDigits(1, user_input); bool is_visa = (cc_num_digits == 13 || cc_num_digits == 16) && 4 == temp; if(is_visa) { output = "VISA"; } } printf("%s\n", output); return 0; }
int main (void) { long long int temp_cc_num; char cc_num[16]; int cc_len, cc_type; // prompt user for input (credit card number). printf("Please provide a valid credit card number\n" "(AMEX 16 digits, MC 15 digits or VISA 13 or 16 digits): \n"); temp_cc_num = GetLongLong(); //scanf("%lli", &temp_cc_num); // check if number is negative, return 1, INVALID if ( temp_cc_num < 0 ) { printf("\nUsage: ./credit <credit card number>\n"); return 1; } // copy user input integer into a string. sprintf(cc_num, "%lli", temp_cc_num); cc_len = strlen(cc_num); cc_type = card_type(cc_num, cc_len); // run luhn test and card type check, print result. if ( luhn(cc_num, cc_len) != 0 && cc_type > 1 ) { if ( cc_type == 4 ) printf("AMEX\n"); else if ( cc_type == 3 ) printf("MASTERCARD\n"); else printf("VISA\n"); } else printf ("INVALID\n"); return 0; }
int main(void) { long long card_number, orig_number; string card_type; int digits = 0, temp = 0, current = 0, even_digits = 0, odd_digits = 0, total_sum = 0; bool is_even = false; // Get Card number do { printf("Number: "); card_number = GetLongLong(); } while (card_number < 0); // Save card number for later check; orig_number = card_number; // Process Card Number while (card_number != 0) { // Get last digit of number current = (card_number % 10); // Increase number of digits in card number digits += 1; // Check if digit is even or odd and increase appropriate sum if (is_even == true) { // Multiply digit by 2 temp = current * 2; // Add each digit of product while (temp != 0) { even_digits += temp % 10; temp /= 10; } // Tell program next digit is odd is_even = false; } else { odd_digits += current; // Tell program next digit is even is_even = true; } // Remove last digit and continue card_number /= 10; } // Get total sum total_sum = even_digits + odd_digits; // Check sum and card type by first digit if (total_sum % 10 == 0) { // Check AMEX if (digits == 15) { if ((orig_number / (long long) 10E12) == 34 || (orig_number / (long long) 10E12) == 37) { card_type = "AMEX"; } else { card_type = "INVALID"; } } else if (digits == 16) { // Check Mastercard if ((orig_number / (long long) 10E13) >= 51 && (orig_number / (long long) 10E13) <= 55) { card_type = "MASTERCARD"; } // Check some Visas else if ((orig_number / (long long) 10E14) == 4) { card_type = "VISA"; } else { card_type = "INVALID"; } } // Check Visa else if (digits == 13) { if ((orig_number / (long long) 10E11) == 4) { card_type = "VISA"; } else { card_type = "INVALID"; } } else { card_type = "INVALID"; } } else { card_type = "INVALID"; } // Print result printf("%s\n", card_type); return 0; }
int main(void) { long long cardNum; int currProduct; int currTotal = 0; int digits; //prompt user to enter a credit card printf("Please Enter Your Card Number\n"); cardNum = GetLongLong(); //lets count the number of digits digits = countDigits(cardNum); //printf("%d count \n", digits); if(digits == 13 || digits == 15 || digits == 16) { //lets check validity int last = 1; char* card_string = calloc(digits, sizeof(char)); //use this to loop through above array int pos = digits; //multiply every other number by two while (cardNum > 0) { int currDigit = cardNum % 10; card_string[pos] = (char)currDigit; pos --; //this means we are at the last and we can use this as an interval for the next iteration if(last == 0) { currProduct = currDigit * 2; //run this loop again to get the digits of the result while(currProduct > 0 ) { int currInnerDigit = currProduct % 10; currTotal += currInnerDigit; //get the next inner digit currProduct /= 10; } //switch indicator for next iteration last = 1; } else { //add to the total since we didnt multiply it by e currTotal += currDigit; //switch the indicator of positions last = 0; } //get the next digit cardNum /= 10; } int validity = currTotal % 10; // convert card num to string //sprintf(card_string, "%llu", cardNum); //ltoa(cardNum,card_string,10); printf("Card String %s\n", card_string); if(validity != 0) { printf("INVALID\n"); } else { //check the number of digits to show you card type check valid cart prefixes if ( digits == 15 ) { if(startsWith("34", card_string) || startsWith("37", card_string)) { printf("AMEX\n"); } else { printf("INVALID\n"); } } else if ( digits == 16 ) { if (startsWith("51", card_string) || startsWith("52", card_string) || startsWith("53", card_string) || startsWith("54", card_string) || startsWith("55", card_string)) { printf("MASTERCARD\n"); } else if (startsWith("4", card_string)) { printf("VISA\n"); } else { printf("INVALID\n"); } } else if (startsWith("4", card_string) ) { printf("VISA\n"); } else { printf("INVALID\n"); } } } else { printf("INVALID\n"); } return 0; }
int main(void) { // declarations unsigned long long int cardNum; int i, checksum = 0, len, rem; string cardType; // user input for credit card do { printf ("Number: "); cardNum = GetLongLong(); } while ( cardNum < 1 ); // get length len = diglen(cardNum); // initiate loop through number for ( i = 1; i <= len; i++ ) { // take every other digit, multiply by 2 and add each digit of result // to checksum if ( i % 2 == 0 ) { rem = (cardNum % 10) * 2 ; if ( rem < 10 ) checksum += rem; else while ( rem != 0 ) { checksum += rem % 10; rem /= 10; } // take remaining digit and add to checksum } else checksum += cardNum % 10; // move onto next digit of the number cardNum /= 10; // once down to the first 2 digits of the number make checks in order // to determine what type of card the person may have if ( i == len - 2 ) { if ( len == 15 && (cardNum == 34 || cardNum == 37) ) cardType = "AMEX"; else if ( len == 16 && (cardNum == 51 || cardNum == 52 || cardNum == 53 || cardNum == 54 || cardNum == 55) ) cardType = "MASTERCARD"; else if ( (len == 13 || len == 16) && cardNum / 10 == 4 ) cardType = "VISA"; else cardType = "INVALID"; } } // if the checksum is not equally divisible by 10 then invalid cardNum if ( checksum % 10 != 0 ) cardType = "INVALID"; printf ("%s\n", cardType); return 0; }
int main(void) { printf("Hello, Welcome to Credit Checker\n"); long long creditNum; //max credit card number of 16 digits(VISA, MasterCard), max 8 digits to keep track int numDigits = 0; int digit[8]; int count = 0; int sum = 0; int tempSum = 0; int temp; int valid; int checkAMEX; int checkMC; int checkVISA; int checkVISA1; long long tempNum; printf("Enter your credit card number: "); creditNum = GetLongLong(); tempNum = creditNum; //multiply every other digit by 2, starting with the number's second-to-last digit, and then add those products' digits together //gets every other digit, startign with the number's second-to-last while (tempNum > 0) { tempNum /= 10; digit[count] = tempNum % 10; tempNum /= 10; count++; } //multiply digits by 2 for (int i = 0; i < count; i++) { digit[i] *= 2; } //add products' digits together for (int i = 0; i < count; i++) { if(digit[i] > 9) { sum += digit[i] % 10; temp = digit[i] / 10; sum += temp; } else sum += digit[i] % 10; } count = 0; tempNum = creditNum; //get sum of digits not multiplied by 2 while (tempNum > 0) { tempSum += (tempNum % 10); tempNum /= 100; } sum += tempSum; valid = sum % 10; tempNum = creditNum; while (tempNum > 0) { tempNum /= 10; numDigits++; } checkAMEX = creditNum / 10000000000000; checkMC = creditNum / 100000000000000; checkVISA = creditNum / 1000000000000; checkVISA1 = creditNum / 1000000000000000; if (valid != 0) printf("INVALID\n"); else if (creditNum < 1000000000000 || creditNum > 9999999999999999) printf("INVALID\n"); else if (numDigits == 15 && (checkAMEX == 34 || checkAMEX == 37)) printf("AMEX\n"); else if (numDigits = 16 && (checkMC > 50 && checkMC < 56)) printf("MASTERCARD\n"); else if (checkVISA == 4 || checkVISA1 == 4) printf("VISA\n"); return 0; }
int main(void) { long long ccNumber = 0; do { printf("Number: "); ccNumber = GetLongLong(); } // Minimum number is a 13 digit VISA starting with 4 // Maximum number is a 16 digit MASTERCARD starting with 55 while(ccNumber < 4e12 && ccNumber > 5.5e15); // Count number of digits int nDigits = 0; long long n = ccNumber; while (n !=0 ) { n /= 10; nDigits++; } int ccDigits[nDigits]; int evenSum = 0, oddSum = 0, totalSum = 0; n = ccNumber; for (int i = nDigits-1; i >= 0 ; i--) { ccDigits[i] = n % 10; n /= 10; //printf("Current digit: %d => ", ccDigits[i]); if ((nDigits-i) % 2 == 0) { int tempDig = ccDigits[i] * 2; if (tempDig > 9) { evenSum += tempDig % 10; evenSum += tempDig / 10; // evenSum += tempDig-9; } else evenSum += tempDig; //printf("partialEven: %d\n", evenSum); } else { oddSum += ccDigits[i]; //printf("partialOdd: %d\n", oddSum); } } totalSum = evenSum + oddSum; //printf("nDigits: %d\n", nDigits); //printf("odd: %d\n", oddSum); //printf("even: %d\n", evenSum); //printf("total: %d\n", totalSum); short cardFound = totalSum % 10; if (0 == cardFound) { switch(ccDigits[0]) { case (3): // AMEX if (4 == ccDigits[1] || 7 == ccDigits[1]) printf("AMEX\n"); else printf("INVALID\n"); break; case (5): // MASTERCARD if (1 == ccDigits[1] || 2 == ccDigits[1] || 3 == ccDigits[1] || 4 == ccDigits[1] || 5 == ccDigits[1]) printf("MASTERCARD\n"); else printf("INVALID\n"); break; case (4): // VISA printf("VISA\n"); break; default: // INVALID printf("INVALID\n"); break; } } else { printf("INVALID\n"); } return 0; }
int main(void) { long long cc = 0; long long ccct = 0; int count = 0; string cctype; printf("What is your credit card number?\n"); cc = GetLongLong(); //check the length of the credit card ccct = cc; while (ccct) { ccct /= 10; count++; } if (count < 13 || count > 16) { cctype = "INVALID"; } else { int ccarray[count]; ccct = cc; int ft = 0; for (int i = 0; i < count; i++) { ccarray[i] = 0; } for (int i = 0; i < count; i++) { ccarray[i] = ccct % 10; ccct/=10; } if ((ccarray[(count - 1)] == 4) && (count == 13 || count == 16)) { ft = 4; cctype = "VISA"; } else { ft = ccarray[(count - 1)] * 10 + ccarray[(count) - 2]; if ((ft == 34 || ft == 37) && count == 15) { cctype = "AMEX"; } else if ((ft == 51 || ft == 52 || ft == 53 || ft == 54 || ft == 55) && count == 16) { cctype = "MASTERCARD"; } else { cctype = "INVALID"; } } for (int i = 1; i < count ; i = i + 2) { if (ccarray[i] >= 5) { ccarray[i] = (ccarray[i] * 2) % 10 + 1; } else { ccarray[i] *= 2; } } int total = 0; for (int i = 0; i < count ; i++) { total += ccarray [i]; } if (total % 10 != 0) { cctype = "Invalid"; } } printf("%s\n",cctype); }
int main(void) { int oddEven = 1; int rightDigit; int numbersToMultiply; int numbersToAdd = 0; int ccDigitOne = 0; int ccDigitTwo = 0; printf("Please enter your card number: "); long long int cardNumber = GetLongLong(); // reverse the digits in cardNumber while(cardNumber != 0) { rightDigit = cardNumber % 10; // if this is even repetition of the loop, multiply the digit // by 2 and add the sum of the product's digits to numbersToAdd if (oddEven % 2 == 0) { ccDigitTwo = rightDigit; rightDigit = rightDigit * 2; while(rightDigit != 0) { numbersToAdd += rightDigit % 10; rightDigit = rightDigit / 10; } } // otherwise add the odd repetitions together else { ccDigitOne = rightDigit; numbersToAdd += rightDigit; } cardNumber = cardNumber / 10; oddEven++; } if (numbersToAdd % 10 == 0) { if (ccDigitOne == 5 || ccDigitTwo == 5) { // if first digits are 51, 52, 53, 54, or 55 return MasterCard printf("MASTERCARD\n"); } else if (ccDigitOne == 3 || ccDigitTwo == 3) { // if first digits are 34 or 37 return Amex printf("AMEX\n"); } else if(ccDigitOne == 4 || ccDigitTwo == 4) { // if first digit is 4, return visa printf("VISA\n"); } else { printf("INVALID\n"); } } else { printf("INVALID\n"); } return 0; }
int main(void) { long long creditcard; int length; int digitsum = 0; bool valid; int firsttwo; //gets credit card number printf("Number: "); creditcard = GetLongLong(); length = log10(creditcard) + 1; //gets length of credit card number //checks to make sure length is valid for a credit card if (length < 13 || length > 16 || length == 14) { printf("INVALID\n"); return 0; //ends program if not valid length } //creates an int containing the first two digits of card firsttwo = (creditcard / (long long)pow(10, length - 2)) % 100; //sums the digits of every second digit * 2 starting right to left for (int i = 1; i <= length;i += 2) { int part1 = ((creditcard / (long long)pow(10, i)) % 10) * 2; int digits = 0; if (part1 >= 10) { //breaks numbers > 10 into its individual digits. for (int x = 0; x < 2; x++) { digits += (part1/(int)pow(10, x)) % 10; } digitsum += digits; } else { digitsum += part1; } } //same as the for loop above but starting from the first number on right for (int i = 0; i <= length;i += 2) { digitsum += (creditcard / (long long)pow(10, i)) % 10; } valid = !(digitsum % 10);//if the remainder is 0 assigns true if (valid) { //prints appropriate card type switch (firsttwo) { case 34: printf("AMEX\n"); break; case 37: printf("AMEX\n"); break; case 40: case 41: case 42: case 43: case 44: case 45: case 46: case 47: case 48: case 49: printf("VISA\n"); break; case 51: case 52: case 53: case 54: case 55: printf("MASTERCARD\n"); break; default: printf("INVALID\n"); } } else { printf("INVALID"); } return 0; }
int main(void) { long long credit_card_num, holder_num; int num_of_digits = 1; printf("Number: "); credit_card_num = GetLongLong(); num_of_digits = (int)log10(credit_card_num) + 1; //populate int array with credit_card_num int digit_array[num_of_digits]; int index; holder_num = credit_card_num; index = 0; while(holder_num > 0) { digit_array[index++] = holder_num % 10; //store last digit in array holder_num /= 10; } // int i; // for (i = 0; i < num_of_digits; i++) // printf("%d ",digit_array[i]); int first_d, second_d; if(num_of_digits > 12) { first_d = digit_array[num_of_digits -1]; second_d = digit_array[num_of_digits - 2]; } // printf("num_of_digits %d",num_of_digits); switch (num_of_digits) {//switch case 15: //first_d = digit_array[14]; //second_d = digit_array[13]; // printf("***%d, %d\n", first_d, second_d); // if first two digits are 37 or 34 & Luhn's algorithm validates if( (first_d == 3) && ((second_d == 4) || (second_d == 7) ) && validate(digit_array, num_of_digits) ) printf("AMEX\n"); else printf("INVALID\n"); break; case 16 : if (first_d == 4) { if (validate(digit_array,num_of_digits)) printf("VISA\n"); else printf("INVALID\n"); } else if ((first_d == 5) && (second_d >=1) && (second_d <= 5)) { if (validate(digit_array, num_of_digits)) printf("MASTERCARD\n"); else printf("INVALID\n"); } else printf("INVALID\n"); break; case 13 : if (first_d == 4) { if (validate(digit_array,num_of_digits)) printf("VISA\n"); else printf("INVALID\n"); } else printf("INVALID\n"); break; default: printf("INVALID\n"); }//switch }
int main(void) { // print prompt to user printf("Number: "); // initialize number as user input long long card = GetLongLong(); // initialize value to 0 int value = 0; // initialize cardLength to number of loops needed int cardLength = 16 / 2; // initialize cardNumber as card long long cardNumber = card; // initialize cardCopy as card long long cardCopy = card; // declare boolean valid bool valid; // loop through card numbers for(int i = 0; i < cardLength; i++) { // initialize n as second-to-last card number int n = (card / 10) % 10; // multiply number by 2 int nDouble = n * 2; // add digits to value if (nDouble < 10) { value += nDouble; } else { int firstDigit = nDouble / 10; int lastDigit = nDouble % 10; value += firstDigit + lastDigit; } // remove 2 last digits of card number for next loop card /= 100; } // loop through card numbers for (int i = 0; i < cardLength; i++) { // initialize n as last card number int n = cardCopy % 10; // add digits to value value += n; // remove 2 last digits of card number for next loop cardCopy /= 100; } // check if input is valid if (value % 10 == 0) { valid = true; } else { valid = false; } // print card type or invalid if (valid) { if (cardNumber / 10000000000000 == 34 || cardNumber / 10000000000000 == 37) { printf("AMEX\n"); } if (cardNumber / 100000000000000 >= 51 && cardNumber / 100000000000000 <= 55) { printf("MASTERCARD\n"); } if (cardNumber / 1000000000000 == 4 || cardNumber / 1000000000000000 == 4) { printf("VISA\n"); } } else { printf("INVALID\n"); } }
int main() { int numCount = GetInt(); unsigned long* matrix = (unsigned long*)malloc(numCount * sizeof(unsigned long)); //fill the matrix size_t uintSize = sizeof(unsigned long); for (size_t i = 0; i < numCount; i++) { unsigned long number = GetLongLong(); matrix[i] = number; } //read the commands char * command = GetString(); while(strcmp(command,"end")!=0) { if(strcmp(command, "up") == 0) { int lastrow = row; //check boundries if (row == 0) { row = numCount - 1; } else row--; //check next position if(matrix[row] & (1<<col)) { //has hit a 1 bit, game over GameOver(row, col); break; } else matrix[row] |= (1 << col); //clear last position matrix[lastrow] &= ~(1 << col); } if (strcmp(command, "right") == 0) { int lastcol = col; //check boundries if (col == 0) col = 31; else col--; //check next position if (matrix[row] & (1 << col)) { //has hit a 1 bit, game over GameOver(row, col); break; } else matrix[row] |= (1 << col); //clear current position matrix[row] &= ~(1 << (lastcol)); } if (strcmp(command, "left") == 0) { int lastcol = col; //check boundries if (col == 31) col = 0; else col++; //check next position if (matrix[row] & (1 << col)) { //has hit a 1 bit, game over GameOver(row, col); break; } else matrix[row] |= (1 << col); //clear current position matrix[row] &= ~(1 << (lastcol)); } if (strcmp(command, "down") == 0) { int lastrow = row; //check boundries if (row == numCount-1) row = 0; else row++; //check next position if (matrix[row] & (1 << col)) { //has hit a 1 bit, game over GameOver(row, col); break; } else matrix[row] |= (1 << col); //clear last position matrix[lastrow] &= ~(1 << col); } command = GetString(); } //print matrix for (size_t i = 0; i < numCount; i++) { printf("%lu\n", matrix[i]); } free(matrix); return 0; }
int main() { printf("Enter a CC number to check if valid\n"); ccnum = GetLongLong(); digits = log10(ccnum) + 1; for(int i = digits; i>=0; i--) { modu = ccnum % 10; ccarray[i] = modu; ccnum = ccnum / 10; } if(ccarray[1] == 3 && digits == 15){ if((ccarray[2] == 4) || (ccarray[2] == 7)) flag = 3; } if(ccarray[1] == 4){ if(digits == 16) flag = 4; } if(ccarray[1] == 5){ if(digits == 16 && ((ccarray[2] == 1) || (ccarray[2] == 2) || (ccarray[2] == 3) || (ccarray[2] == 4))) flag = 5; } if(flag == 0){ printf("INVALID\n"); return 0; } for(int k = digits - 1; k>=1; k-=2) { rema = ccarray[k] * 2; if(rema > 9){ added = added + (rema % 10); added = added + 1; } if(rema <= 9){ added = added + rema; } } if(digits == 16) { for(int l = 2; l<= digits; l+=2) { added = added + ccarray[l]; } } if(digits == 15) { for(int m = 1; m<= digits; m+=2) { added = added + ccarray[m]; } } if(added % 10 == 0) { if(flag == 3) printf("AMEX\n"); if(flag == 4) printf("VISA\n"); if(flag == 5) printf("MASTERCARD\n"); } else{ printf("INVALID\n"); } }