Click here to Skip to main content
13,247,336 members (89,689 online)
Rate this:
 
Please Sign up or sign in to vote.
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:

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 20-May-17 12:37pm
Updated 20-May-17 21:11pm
Comments
NotPolitcallyCorrect 20-May-17 17:43pm
   
https://msdn.microsoft.com/en-us/library/c6s3h5a7.aspx

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
//...
  Permalink  
v3

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy |
Web04 | 2.8.171114.1 | Last Updated 21 May 2017
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100