Determining if a Float is odd/even

Oct 22, 2013 at 3:09am
I need help determining if a value entered in loop is an odd or even number. Also, the value can't be int because it may be a decimal value(therefore i cant use the if(x%==0).

I need to replace the if(value%2 ==0) else num_even++ statement in my code with something else that will work with float to determine odd vs even.

Any suggestions?
-------------------------------------------------------
#include <iostream>
using namespace std;

int main()
{

int num_values;
float sum_values = 0;
int num_neg_values = 0;
int num_pos_values = 0;
int num_zero_values = 0;
float avg_values;
int num_even = 0;
int num_odd = 0;
int loop_n;
float value;

cout << "Enter number of values ";
cin >> num_values;


for(loop_n = 1; loop_n <= num_values; loop_n++)
{

cout << "Enter value: ";
cin >> value;
sum_values = (sum_values + value);
{

if(value%2 ==0) // If the value when divided by 2 is 0 it is odd
num_even++;

else
num_odd++;

if(value>0)
num_pos_values++;

else
num_neg_values++;

if(value ==0)
num_zero_values++;


} // if statement
} // for loop

avg_values = (sum_values/num_values);

cout << num_values << " is the number of values entered" << endl;
cout << sum_values << " is the sum of the values entered" << endl;
cout << avg_values << " is the average of the values entered" << endl;
cout << num_neg_values << " is the number of negative values entered" << endl;
cout << num_pos_values << " is the number of positive values entered" << endl;
cout << num_even << " is the number of even values entered" << endl;
cout << num_odd << " is the number of odd values entered" << endl;

return 0;
} //main
Oct 22, 2013 at 3:29am
According to Wikipedia, parity (being even or odd) is a property of an integer, not any floating point number. In fact, it is specifically stated that non-integers have no parity.
Topic archived. No new replies allowed.