Question:
Please help on this. Produce C++ function that accepts an integer argument to determine even or odd.?
David D
2014-03-10 11:04:47 UTC
Please help on this. I am new in C++ Programming. This is just a practice for learning. Thank you in advance.

Produce C++ function that accepts an integer argument and determines whether the passed integer is odd or even. The function returns 1 if the number is odd and returns 2 if the number is even.

Write main ( ) function that asks the user to interactively input an integer number, calls the function to determine whether the number is odd or even, and then displays the outcome as one of the two lines accordingly
The number x is odd
The number x is even
Test your program for the following data: 55 -7 44 13 0
The program should continue getting one number at a time until the user enters 0 (zero)

There should be two function at least.

logicn -->

( integer % 2 == 0 ) for even

else

odd
Three answers:
Jack
2014-03-10 11:43:27 UTC
//think this works, you will have to test it

#include

using namespace std;



int main()

{

//prototype declaration

int getParity(int);

int num;

cout << "\nInput a number: ";

cin >> num;

int parity = getParity(num);

if(parity == 1){

cout << "The number " << num << " is even";

}

else{

cout << "The number " << num << " is odd";

}

while(num != 0){

cout << "\nInput a number: ";

cin >> num;

parity = getParity(num);

if(parity == 1){

cout << "The number " << num << " is even";

}

else{

cout << "The number " << num << " is odd";

}



}

system("pause");

return(0);

}



int getParity(int n){

int par;

if((n & 1) == 0){

par = 1; //even



}

else

{

par = 2; //odd

}

return par;

}
?
2014-03-10 19:54:41 UTC
bool is_even(int j){// returns 1 for odd 2 for even

return !(j&1) +1;

}
David W
2014-03-10 18:32:06 UTC
bool IsEven(int iValue)

{

return (iValue % 2 == 0);

}


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...