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

int main(void)
{
 
    long CardNumber = get_long ("Card number: ");
 
    long d5;
 
    d5 = CardNumber % pow (10, 5) / pow (10, 4);
    
    printf("Digit 1 =  %li\n", d5);
    
}

Is it really that the modulus operator % doesn't work with function pow or did I do something wrong? If I replace pow (10, 5) by 100000 number my code works. If I define pow (10, 5) as another one variable it works too. I deal with numbers of many zeros and I don't want to count them by hand every time as well as add extra variable.

error: invalid operands to binary expression ('long' and 'double')
    d5 = CardNumber % pow (10, 5) / pow (10, 4);
         ~~~~~~~~~~ ^ ~~~~~~~~~~~
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Ivan
  • 7
  • 3
  • 4
    Does this answer your question? [Why does modulus division (%) only work with integers?](https://stackoverflow.com/questions/6102948/why-does-modulus-division-only-work-with-integers) – Passerby Dec 08 '21 at 03:18
  • Yeah, as mentioned by @passerby, the default modulus operator only works with integers in C/C++. – ListsOfArrays Dec 08 '21 at 03:22
  • 3
    Is this code related to credit card numbers? Don't use integers for that. – paddy Dec 08 '21 at 03:29
  • The other piece of the puzzle is that `pow` is a floating-point function and returns `double`. You don't want to use it here. Unfortunately C has no standard function for integer powers. – Nate Eldredge Dec 08 '21 at 03:30
  • and don't use `pow(10, 5)`/`pow(10, 4)` even when a floating point value is required. Use `1e5`/`1e4` instead. See also [Why does pow(5,2) become 24? [closed]](https://stackoverflow.com/q/22264236/995714), [Why the result of pow(10,2) 99 instead of 100? (duplicate)](https://stackoverflow.com/q/54057687/995714) – phuclv Dec 22 '21 at 03:10

0 Answers0