I tried to attempt the credit.c problem. My code is as follows:

#include <stdio.h>
#include <cs50.h>
#include <math.h>
#include <string.h>

int main(void)
    printf("Please enter your credit card number: ");
    long long credit = GetLongLong();
    char str[20];
    sprintf(str, "%lld", credit);
    int length = strlen(str);
    int sum1 = 0;
    int sum2 = 0;
    for (int i = length - 1; i >= 2; i = i - 2)
        sum1 += (2 * (credit % 10^i));
    for (int i = length; i >= 2; i = i - 2)
        sum2 += (credit % 10^i);
    int sum = sum1 + sum2;
    int fstwo = ((credit % (10^length)) + (credit % (10^(length - 1))));
    if (length == 15 && (fstwo == 34 || fstwo == 37) && sum % 10 == 0)
    else if (length == 16 && (fstwo >= 51 && fstwo <= 55) && sum % 10 == 0)
    else if ((length == 13 || length == 16) && (credit % (10^length)) == 4 && sum % 10 == 0)

My logic is to convert a long long into a string via sprintf(), then use strlen() and for loops to detect every other digit in the long long. The first for loop starts from the second last digit and sums twice of each digit. The second one starts from the last digit and sums them as per normal. I used the modulo function to determine each digit in the long long. Finally, I implemented the various conditions in the if and else statements. However, all my tries so far have given me invalid results. Could someone please tell me what's wrong? I have absolutely no clue, thank you!

1 Answer 1


Firstly, you set the length of str to 20 and then determine the length of this wasting an instruction and memory.

Do your two for loops give the correct results? sum1 += (2 * (credit % 10^i)); will not give you the result you are looking for as the ^ operator is a bitwise exclusive or operator. if you want to the power of, you can use:


Also remember that the array is zero-indexed. So 0 is the first entry and 19 is the last entry.

  • Hi, regarding your first comment, isn't the str[20] the allocated size, while strlen calculates the length of the string stored? I read somewhere that strlen will give the actual length, while sizeof gives the allocated size of 20, is this true?
    – user107224
    May 20, 2016 at 16:08
  • The strlen() function looks for the position of the null \0 character and therefore yes, it will give the length of the string. my point was that if you know the length of the string will be 20, this is a waste of an instruction, If you want to check that the string is in fact 20 characters so that you dont get a segmentation fault or other undefined behaviour, then you should actauly do that. Dont bother running the loops when the string isn't the correct lengh. Just return an error. Otherwise you will get results, just not the correct results when the string isn't 20 characters.
    – Daniel Lee
    May 24, 2016 at 17:34
  • @DanielLee, we need to calculate the length of the credit card number for the purposes of the problem, this person just set 20 as the max length of the array, that isn't always the size of the number entered. May 22 at 12:07
  • @user107224, as mentioned in this answer, the pow() function needs to be used, but even then you wouldn't get the right answer. Here's the logic: When you perform credit % pow(10,i), you get the last i digits of the number, not the ith-digit from the end, like you were expecting. May 22 at 12:09

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .