By using this site, you agree to our Privacy Policy and our Terms of Use. Close

Forums - General - Another Borland C++ Question (very very quick)

Hi, I know this is stupid to ask, but the question is:

You have to give an "n" number and the computer has to tell you if it's pair or odd, here is how I have it:

#include <iostream.h>
#include <conio.h>
#include <math.h>

float n;
void main()
{
    cout<<"Put the number: "
   cin>>n
   ??????????
}



Around the Network

You'd have to check if the number is equally divisible by 2, try an if statement like..

if(n % 2 == 0)

I think that ought to work, there are a lot more ways to do it most likely.



AdventWolf said:

You'd have to check if the number is equally divisible by 2, try an if statement like..

if(n % 2 == 0)

I think that ought to work, there are a lot more ways to do it most likely.

Pretty sure that's the quickest way. Find out if n/2 is an integer (though my mediocre coding knowledge is more in Java and HTML).



Monster Hunter: pissing me off since 2010.

n is a float, so the suggested method above will not work because you cann't do a modulo operation on a floating point number.

Is there a reason you are using a float?

EDIT: There are other problems. Comparing floats to exact numbers is questionable at best due to imprecision. If you use fmod() you wouldn't be able to compare it to 0.0 reliably. so fmod(n,2)==0, where n is an even number, might return false when it should be true. Instead you'd have to compare it to a small epsilon range which doesn't really support what you were trying to determine in the first place.




If you drop a PS3 right on top of a Wii, it would definitely defeat it. Not so sure about the Xbox360. - mancandy
In the past we played games. In the future we watch games. - Forest-Spirit
11/03/09 Desposit: Mod Bribery (RolStoppable)  vg$ 500.00
06/03/09 Purchase: Moderator Privilege  vg$ -50,000.00

Nordlead Jr. Photo/Video Gallery!!! (Video Added 4/19/10)

why a float? why care about a float being even or odd? if possible, cast it to int and do a bit and to check if the last bit is 0.



Around the Network
nordlead said:

n is a float, so the suggested method above will not work because you cann't do a modulo operation on a floating point number.

Is there a reason you are using a float?


Not really, I just do it everytime, my mistake, probably could get plenty of errors in the future.

I already tried it, since it didn't work with a float I used an int and worked great.

BTW, since my question is done, could you lock this?