2

Basically all I'm trying to do is run a loop that returns the value of X/Y Mod 10 where X is 1234567890123456 and Y is 10^i, so that I can get each individual digit of 1234567890123456 separated. But after i = 7 it returns 11 instead of the correct value (9). All numbers involved are of the long data type. I've tried doing it with the pow() function, and manually by just inputting the powers of ten and still get the same result.

#include <stdio.h>
#include <math.h>
long Cardno;
long Digits;
long Divider;
int main(void)
{
    Cardno = 1234567890123456;
    Digits = log10(Cardno) +1;

    if (Digits == 13 || Digits == 15 || Digits == 16)
    {
        for(int i = 0; i <= Digits; i++)
    {
       printf("%lo\n", Cardno/10000000 % 10);
    }
    }
    else
    {
        printf("INVALID\n");
    }
}
9
  • 3
    That number can't fit in a long, try unsigned long long. And append ULL to the end of your literal. Also, log10 expects double. Enable warnings when compiling '-Wall -Wextra' and you should get compilation errors. Commented Aug 30, 2022 at 6:58
  • Changing it to an unsigned long long fixed the problem, I think my bad at math brain didn't realize how large the number I was actually dealing with was. Thank you. Commented Aug 30, 2022 at 7:08
  • 3
    General rule: Card numbers (and phone numbers) should not be considered to be integers but strings. Commented Aug 30, 2022 at 7:28
  • @Gerhardh Hear, hear!! When it comes time to do the checksum (left to right) the even/odd lengths (15 v. 16) will inspire some off-the-wall recursive function!!! Heaven help us all! :) Commented Aug 30, 2022 at 7:33
  • 1
    @Gerhardh okay yeah that's significantly easier. Thank you. Commented Aug 30, 2022 at 7:50

1 Answer 1

2

so you have to use long long instead of long , I don't why the compiler didn't give this warning , but the compiler of mine gave me this warning :

Implicit conversion from 'long long' to 'long' changes value from 1234567890123456 to 1015724736

also variable called Digits doesn't need to be long , int will work just fine. also , function called log10() takes double and returns double so you have to cast that.

also in your code you are printing Cardno/10000000 % 10 which is wrong , it sould be (Cardno/ (long long )pow(10, i)) % 10

and here is the full code :

#include <stdio.h>
#include <math.h>
long long Cardno;
int Digits;
long long Divider;
int main(void)
{
    Cardno = 1234567890123456;
    Digits = (int)log10((double )Cardno) +1;

    if (Digits == 13 || Digits == 15 || Digits == 16)
    {
        for(int i = 0; i <= Digits; i++)
        {
            printf("%ld\n",(Cardno/ (long long )pow(10, i)) % 10);
        }
    }
    else
    {
        printf("INVALID\n");
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

The casts in the call to log10() are not necessary because the compiler has the necessary information to apply them automatically — including <math.h> deals with them. That said, they are not completely pointless. But it is probably better to determine the number of digits using repeated integer division rather than using floating-point arithmetic.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.