mardi 25 août 2020

Use if-else to evaluate the starting number and the length of a number in C

I am a beginner of C.
I try to use if-else to evaluate if a number starts with 4 and has certain digit. It is fine when I test it with card_num = 4000000000000000, but I have no clue why it goes to else when I test with other numbers that start with 4 and have same length, such as card_num = 4003600000000014. These two numbers are supposed to have same result.
Please help me! Thanks!

    int main(void)
    {
    
        long card_num;
        card_num = 4003600000000014;
    
        if ( (card_num / 1e12 == 4 || card_num / 1e15 == 4) )
        {
            printf("Valid\n");
        }
        else
        {
            printf("Invalid\n");
        }
    
    }

Aucun commentaire:

Enregistrer un commentaire