FAQ: How to test whether integer is even or odd [Archive] - C Board

PDA

View Full Version : FAQ: How to test whether integer is even or odd


giggsy
12-24-2002, 09:45 AM
Hi. I need to write a function that tests whether an integer is even or not. This is how far I've got. I wasn't sure if its best to use an 'if' statement in the function.

/* a function that tests whether an integer is even, returning a true or false
answer */

#include <iostream>

using namespace std;

int main()
{
int n = 0;

cout << "Enter a number: ";
cin >> n;

cout << test(n) << endl;

return 0;
}

int test(int n)
{
if (n

cheers.

RoD
12-24-2002, 09:48 AM
mod by two.

variable = variable % 2;

Prelude
12-24-2002, 09:48 AM
#include <iostream>

int main()
{
int val;

std::cout<<"Enter a number: ";
std::cin>> val;

if ( val % 2 == 0 )
std::cout<< val <<" is even"<<std::endl;
else
std::cout<< val <<" is odd"<<std::endl;
}

-Prelude

Polymorphic OOP
12-24-2002, 09:52 AM
Or, if you want to optimize just do:

Value & 1

if the result of the expression is true then the number was odd, if false, then the number was even

RoD
12-24-2002, 10:01 AM
hmm, both seem about the same imo, i had only ever learned the modulous way of doing it.

Polymorphic OOP
12-24-2002, 10:11 AM
Bitwise operations are faster than division, multiplication, addition, subtraction because it's just a quick check on the bits.

RoD
12-24-2002, 10:15 AM
thnx for the knowledge.

giggsy
12-24-2002, 10:43 AM
I tried the following:

/* a function that tests whether an integer is even, returning a true or false
answer */

#include <iostream>

using namespace std;

int evenTest(int);

int main()
{
int n = 0;

cout << "Enter a number: ";
cin >> n;

cout << evenTest(n) << endl;

return 0;
}

int evenTest(int n)
{
if (n % 2 == 0)
cout << n << " is even" << endl;
else
cout << n << " is odd" << endl;
}

It generates the following output:

Enter a number: 10
10 is even
98476

Why is 98476 generated?
Also, Polymorphic OOP, how would I implement Value & 1 ?

Polymorphic OOP
12-24-2002, 10:48 AM
It's result is 1 if it's odd and 0 if it's even

so


std::cout << "Value is " << ( Value & 1 ? "odd" : "even" );

Polymorphic OOP
12-24-2002, 10:55 AM
Originally posted by giggsy
Why is 98476 generated?

Because you are "cout"ing the value that evenTest returns (which is garbage cuz you didn't return anything.

What you should do is:

/* a function that tests whether an integer is even, returning a true or false
answer */

#include <iostream>

using namespace std;

bool evenTest(int);

int main()
{
int n = 0;

cout << "Enter a number: ";
cin >> n;

cout << n << " is " << ( evenTest(n) ? "even" : "odd" ) << endl;

return 0;
}

inline bool evenTest(int n)
{
return n % 2 == 0;
}

Eibro
12-24-2002, 10:57 AM
Originally posted by giggsy
I tried the following:

/* a function that tests whether an integer is even, returning a true or false
answer */

#include <iostream>

using namespace std;

int evenTest(int);

int main()
{
int n = 0;

cout << "Enter a number: ";
cin >> n;

cout << evenTest(n) << endl;

return 0;
}

int evenTest(int n)
{
if (n % 2 == 0)
cout << n << " is even" << endl;
else
cout << n << " is odd" << endl;
}

It generates the following output:

Enter a number: 10
10 is even
98476

Why is 98476 generated?
Also, Polymorphic OOP, how would I implement Value & 1 ?
That random number is generated because you're printing the result of a function which is supposed to return an int, but actually returns nothing.

Stoned_Coder
12-24-2002, 11:00 AM
const char* EvenOrOdd( int value)
{
if(value&1) return"odd";
return "even";
}