I’m writing a little program for a beginner C++ class, I’m supposed to take an input (I’m using double, because I like to make things harder for myself) from a user and output whether it’s prime or composite, and if it’s composite, I need to output at least one number which the number is divisible by.
I have too much time on my hands so I’m going a little out of my way to make it harder. I’m trying to do everything without using anything but iostream. I’ve gotten most of it, but this bit is stumping me: How can I determine if a double is an integer without using a math library?(I found questions that had good solutions using math.h here, but nothing without it) I’d like to think there’s a way to do it without writing fifty lines of code for something that seems so simple …
You should probably not be doing any computations in floating point, but a simple way of testing whether a number has decimals or not is taking the floor and comparing it with the original. In C++ you can take the floor by converting to an
int(assuming only positive numbers) and then converting back todouble: