Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
How can I ask if a number is even/odd and positive/negative at the same time?
I've already done the program that asks if a number is positive or negative, but I have no idea how to add (input % 2 == 0) into it.

Thank you.

What I have tried:

CSS
int main()
{
        int input;
        cout << "Please enter a number\n";
        cout << ":";
        cin >> input;
    
    
        if (input > 0)         {
            
            cout << input << " is a positive number" << endl;
            
        }
        else if (input == 0)
        {
            
            cout << input << " is neither positive nor negative number " << endl;
        }
        else
        {
            cout << input << " is a negative number " << endl;
        }
            return 0;
    }
Posted
Updated 20-May-17 20:11pm
Comments
[no name] 20-May-17 17:43pm    
https://msdn.microsoft.com/en-us/library/c6s3h5a7.aspx

1 solution

This a good example of "Algorithm First, Coding Second".
The strategy here is "Divide and Conquer" - First find out the positiveness and evenness of the input SEPARATELY, then make the decision based on the combination of these findings.
Use this pseudo code as a guide:
GET input
SET isEven = 0 // default to odd
SET isPositive = 0  // default to neutral

// Determine positiveness
IF (input > 0) THEN
    SET isPositive = 1  // is positive
ELSE IF (input < 0) THEN
    SET isPositive = -1 // is negative

// Determine evenness
IF (input % 2 == 0) THEN
    SET isEven = 1 // is even

// Decision based on the combined finding of positiveness and evenness
IF (isPositive == 1 AND isEven == 1) THEN
   PRINT 'input is positive and even.'
// Figure out the rest yourself...
ELSE IF () THEN
// ...
ELSE
//...
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900