Member Avatar for johny112

i have written this program.
it excecutes without any errors and even gives me the correct answer.
the program is to tell whether the integer is odd or even.
the problem is that i want a series of integers and passes them one at a time to function even.

Sample Screen display
Enter an integer: 8
8 is an even integer
Enter an integer: 3
3 is an odd integer
Enter an integer: 99
99 is an odd integer


This is the programme i have written so far, i just want to know what im doing wrong and how to correct it.

include <iostream>

bool even(int x);

using namespace std;

void main()

{
int x;

std::cout << "Enter an integer: ";

std::cin >> x;


if(even(x))

std::cout << x << " is an even number." << endl;
else

std::cout << x << " is an odd number." << endl;

}

bool even(int x1)

{

return (x1%2 == 0);

}

Use code tags :D

the problem is that i want a series of integers and passes them one at a time to function even.

If you mean that you want to implement the same algorithm for a series of numbers, use arrays. If you want to pass them one at a time to the function, then put the function call inside a loop.

Member Avatar for johny112

thanks for reply and i got the programme to work.

Now i have to modify the programme, but the outcome should be same. "The function should take an integer argument and return true if the integer is even and false otherwise."

Now i think i have to use bool statement, but cant exactly figure it out.
thanks for reply in advance.

Member Avatar for johny112

what i have figured out is the bool statements in the program i have written in my previous posts, but not sure how to correct it.

What? there is nothing to correct because the function even already returns true or false, unless you just want to rewrite it like this:

bool even(int x1)

{

return (x1%2 == 0) ? true : false;

}
Member Avatar for johny112

i you think thats ok, that thats fine thanks again for your replies.

The reason i said so was because even if i delete all bool statements, the program still executes perfectly, with same outcome.

But anyways if it doesnt make a difference, then i would leave it as it is.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.