All pastes #1913440 Raw Edit

Credit.c

public c v1 · immutable
#1913440 ·published 2010-08-08 20:11 UTC
rendered paste body
/****************************************************************************** credit.c** Robert Skinner** Checks if a credit card number is valid and outputs if it is American* Express, VISA, or MasterCard.*****************************************************************************/#include <stdio.h>intmain(int argc, char *argv[]){    int digits = 0, sum1 = 0, doubleDigitFix = 0, sum2 = 0, grandTotal = 0;    int switcher = 0;    long long cardNum, tempCardNum, cardTypeFinder = 10;    //gets the card number    printf("Enter the credit card number(No hyphens please): ");    scanf("%lld", &cardNum);    //creates throwaway version of card number    tempCardNum = cardNum;    //determines # of digits in the card number    while(tempCardNum){	tempCardNum /= 10;	digits++;    }    //resets throwaway to card number    tempCardNum = cardNum;    //algorithm, difficult to explain    for(int i = digits; i > 0; i--){	//switcher uses a different algorithm for every other digit in card	// number starting with the last one, bool could be used but i dont	// know how to yet, so used an int	if(switcher == 0){	    sum1 += tempCardNum % 10;	    tempCardNum /= 10;	    switcher++;	}else {	    //if calculation is 2 digits, they must be added together, if not,	    // the single digit number can just be added to sum2.	    if(tempCardNum % 10 * 2 >= 10){		doubleDigitFix = (tempCardNum % 10 * 2 % 10) +		    (tempCardNum % 10 * 2 / 10);		sum2 +=  doubleDigitFix;	    } else		sum2 += tempCardNum % 10 * 2;	//happens regardless to decrease temp for next run through and to	// switch to other half of for loop.	tempCardNum /= 10;	switcher--;	}    }    //adds two halves of the for loop.    grandTotal= sum1 + sum2;    //readies temp for checking card type below.    tempCardNum = cardNum;    //creates a number to divide the card number by so that you can get the     // first 2 digits so you can check what type of card it is    for(int j = digits - 3; j > 0; j--){	cardTypeFinder *= 10;    }    tempCardNum /= cardTypeFinder;    //tells user if it is a valid card or not.    if(grandTotal % 10 == 0){	printf("Valid card\n");	//determines card type(Mastercard, Am.Ex, Visa)	if(tempCardNum == 34 || tempCardNum == 37)	    printf("This is an American Express card.\n");	else	    if(tempCardNum >= 51 && tempCardNum <= 55)		printf("This is a Mastercard.\n");	    else		if(tempCardNum >= 40 && tempCardNum < 50)		    printf("This is a Visa card.\n");		else		    printf("Card type unknown.\n");    } else	printf("Invalid card");}