why doesn´t ((num / i) % 1 == 0) work in C++ when num is a double? and how would I instead write this code, that checks for factorials by checking if it leaves a remainder (etc 0.3333).
int getFactorials (double num)
{
int total = 0; // if (total / 2) is equal too 'num' it is a perfect number.
for (int i = 1; i < num; i++)
{
if ((num / i) % 1 == 0)
{
cout << i << endl;
}
}
return 0;
}
The % operator is only allowed on integral types (and user defined
types which overload it). For floating point, you need the function
fmod.
Actually what you want to do is check if n is divisible by i so all you have to change is
if ((num / i) % 1 == 0)
into
if (num % i == 0)
You should know that this is error-prone because you are using double as a type for num. You should use an int instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With