Question

I'm following CS50 course of Harvard University and I have to create a program in C that finds if a credit card number is valid or not. For example, I want from this number 378282246310005 to get the every other digit like this 7224300 so I started like this:

printf("Number\n");
long long cardNum = GetLongLong();

I don't know the methods I need to use to get the other digits to make my calculations.

Was it helpful?

Solution 2

Make use of simple division (/) and % operator to get this

int number[20];
int count = 0;
long long cardNum = GetLongLong();
long long prevNum = cardNum;
while (cardNum < 10)
{
   cardNum = prevNum / 10;
   number[count] = prevNum % 10; 
   prevNum = cardNum;

   count++;
}

OTHER TIPS

One way to do it would be like this:

int digits[7];               // storage for odd digits

cardNum /= 10;               // throw away the least significant digit
for (int i = 0; i < 7; ++i)  // for each odd digit
{
    digits[i] = cardNum %10; // extract the digit
    cardNum /= 100;          // throw away two digits
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top