Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help writing a C program to validate credit card numbers. I have the dire

ID: 3734410 • Letter: I

Question

I need help writing a C program to validate credit card numbers. I have the directions posted below. And the code I have so far. The replies I have gotten so far have been very confusing to me. Here is a picture showing an example of having the numbers looped through.

Code:

#include

#include

#include

int stripper(long long longNumber, int digitNumber) {

return ((longNumber - ((long long)(longNumber / pow(10, digitNumber))*pow(10, digitNumber))) / pow(10, digitNumber - 1));

}

int isOdd(long int digit)

{

if (digit % 2 == 0)

return 0;

else

return 1;

}

int main()

{

printf("Please enter credit card number");

scanf_s();

long long testNumber = 5120415296389632; //long long is a data type for a large integer

getchar();

return 0;

}

Account number Double every odd digit Sum 5 1 2 0415 29 63 8 96 3 2 10 1408 1 10 2 1866 8 186 6 2 1 140 81 12 9668 9662 70

Explanation / Answer

Explanation

This code first asks user to enter a long long int and stores this in testNumber. Store this number into a temporary number & count number of digits in the card. Now initialize an array of size equal to number of digits in the credit card number and copy digits one by one into the array using stripper(). Now Iterate over this array: if position is even, do nothing; if position is odd, double current array element and then find sum of it's ones and tens digits and store the sum into the array at the same position. Once this loop completes, iterate over this array and find sum of all the elements. If sum divisible by 10 then it's a valid card number, otherwise invalid.

#include <stdio.h>

#include <math.h>

int stripper(long long longNumber, int digitNumber) {

            return ((longNumber - ((long long)(longNumber / pow(10, digitNumber))*pow(10, digitNumber))) / pow(10, digitNumber - 1));

}

int isOdd(long int digit)

{

            if (digit % 2 == 0)

                        return 0;

            else

                        return 1;

}

int splitTens(int num){

                        return ((num/10)%10);

}

int splitOnes(int num) {

            return (num%10);

}

int main() {

            long long testNumber;

            int i;

            printf("Please enter credit card number");

            scanf("%lld", &testNumber);

            // count number of digits in the number

            long long tempnum = testNumber;

            int count=0;

            while(tempnum != 0){

                        tempnum /= tempnum;

                        count++;

            }

            int longnum[count];                                                                            // array to store digits of number

            for(i=1; i<=count; i++){

// Store the digits in reverse order in the array as stripper() returns digits starting from rightmost location

                        longnum[count-i] = stripper(testNumber, i);   

            }

            // Now array has all the digits, iterate over this array to work on each digit

            for(i=0;i<count;i++){

                        //i+1 coz indexing of array starts from 0 but we designate indexing in the number starting from 1 i.e. digit at index 0 is considered as 1st digit of the number which is odd

                        if( isOdd(i+1) == 0){   

                                    // even location digit, then keep it as it is

                                    continue;

                        }else {

                                    // odd location digit, double the number and add digits of of the number after doubling

                                    longnum[i] = longnum[i] *2;

                                    longnum[i] = splitOnes(longnum[i]) + splitTens(longnum[i]);          

                        }

            }

            long long sum =0;

           

            // Now sum array elements

            for(i=0;i<count;i++)

                        sum += longnum[i];

           

            if(sum%10 == 0)

                        printf("Valid Credit Card Number");

            else

                        printf("Invalid Credit Card Number");

            getchar();

            return 0;

}

Another possible and efficient solution:

Here we assume a credit card number is always 16 digits long. This solution iterates form 1 to 16 to operate on each digit in credit card number. Check digit location: if even, directly add the digit to sum; if odd, double th digit, find sum of ones & tens place digits and then add this to final sum variable. After this loop, sum will have summation of all the accumulated digits. Divide sum by 10, if remainer is 0 then a valid card number, otherwise, invalid.

#include <stdio.h>

#include <math.h>

int stripper(long long longNumber, int digitNumber) {

            return ((longNumber - ((long long)(longNumber / pow(10, digitNumber))*pow(10, digitNumber))) / pow(10, digitNumber - 1));

}

int isOdd(long int digit)

{

            if (digit % 2 == 0)

                        return 0;

            else

                        return 1;

}

int splitTens(int num){

                        return ((num/10)%10);

}

int splitOnes(int num) {

            return (num%10);

}

int main() {

            long long testNumber,sum=0;

            int i,currDig;

            printf("Please enter credit card number");

            scanf("%lld", &testNumber);

           

            for(i=1;i<=16;i++){

       

                        if( isOdd(i) == 0){                                                                                                                                         

                                    // even location digit

                                    sum += stripper(testNumber, i);

                                   

                        }else {

                                    // odd location digit, double the number and add digits of of the number after doubling

                                    currDig = stripper(testNumber,i);

                                    currDig *=2;

                                    sum += splitOnes(currDig) + splitTens(currDig);

                        }

            }

           

            if(sum%10 == 0)

                        printf("Valid Credit Card Number");

            else

                        printf("Invalid Credit Card Number");

            getchar();

            return 0;

}

Hope this helps. Good Luck !!