Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<cs50.h>
- int main(void)
- {
- long long ccnum;
- do{
- printf("Enter a number: ");
- ccnum = get_long_long();
- }
- while(ccnum<0);
- //"count" variable to count no. of digits in ccnumber
- int count=0;
- long num=ccnum;
- //this while loop counts no.of digits in ccnumber
- while(num>0)
- {
- num=num/10;
- count++;
- }
- //below if condn checks ccnumber contains how many digits and sees if valid or not
- if(count!=15 && count!=13 && count!=16)
- {
- printf("INVALID\n");
- }
- else{
- //below array stores the ccnum in reverse order =>1 digit each
- int number[count];
- for(int i=0;i<count;i++)
- {
- number[i]=(int) (ccnum%10);
- ccnum=ccnum/10;
- }
- //below array stores the same reverse array by eliminating 1st num and makes it '0' kind of empty
- int orignumber[count];
- for(int i=1;i<count;i++)
- {
- orignumber[i]=number[i];
- }
- //below process multiplies '2' to the 'number' array elements in index 1,3,5...so on and
- //if any of it is double digit it makes it single als0(while loop)
- int digit =0;
- for(int i=1;i<count;i+=2)
- {
- number[i]*=2;
- while(number[i]>9)
- {
- digit=number[i]%10;
- number[i]=number[i]/10;
- number[i]=number[i]+digit;
- }
- }
- //this process adds all the digits which are multiplied by 2 and total is called 'sum2nd'
- int sum2nd=0;
- for(int i=1;i<count;i+=2)
- {
- sum2nd=sum2nd+number[i];
- }
- //this process adds 'sum2nd' and the remaining elements which 'not' multiplied by 2
- int checksum=sum2nd;
- for(int i=0;i<count;i+=2)
- {
- checksum=checksum+number[i];
- }
- //this checks if the totalsum(checksum) ends in zero or not
- if(checksum%10==0)
- {
- //visa starts with 4
- if(orignumber[count-1]==4)
- {
- printf("VISA\n");
- }
- //visa starts with 4
- else if((orignumber[count-2]==7) && (orignumber[count-1]==4||orignumber[count-1]==3))
- {
- printf("AMEX\n");
- }
- //Amex starts with 34,37
- else if((orignumber[count-1]==5) && (orignumber[count-2]==1||orignumber[count-2]==2||orignumber[count-2]==3||orignumber[count-2]==4||orignumber[count-2]==5))
- {
- printf("MASTERCARD\n");
- }
- //master card starts with 51,52,53,54,55
- else
- {
- printf("INVALID\n");
- }
- }
- else
- {
- printf("INVALID\n");
- }
- }
- }
Add Comment
Please, Sign In to add comment