# Simple Challenge C++

• (5 Pages)
• 1
• 2
• 3
• 4
• 5

## 71 Replies - 47278 Views - Last Post: 19 July 2013 - 07:04 PM

### #31 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

## Re: Simple Challenge C++

Posted 01 July 2011 - 10:25 AM

Spoiler

If we cannot change any lines, neither can you and 10 is and always will be an even number.

This post has been edited by Karel-Lodewijk: 01 July 2011 - 10:25 AM

• Changed Man With Different Priorities

Reputation: 949
• Posts: 2,361
• Joined: 15-February 11

## Re: Simple Challenge C++

Posted 01 July 2011 - 10:37 AM

I must admit I laughed when you said 10 will always be an even number. You're absolutely correct but you failed to realized 10 was only used as an example. Do you think your program will work if f was equal to 11? The other examples work no matter what number is sent as the parameter.

### #33 Karel-Lodewijk

Reputation: 454
• Posts: 864
• Joined: 17-March 11

## Re: Simple Challenge C++

Posted 01 July 2011 - 10:38 AM

Yes I realized it was just an example anyway, a real entry, you can use xor 1 to add 1 to an even number and substract 1 from an odd number:

Spoiler

This post has been edited by Karel-Lodewijk: 01 July 2011 - 10:47 AM

### #34 7HUND3R

Reputation: 0
• Posts: 11
• Joined: 27-June 11

## Re: Simple Challenge C++

Posted 02 July 2011 - 08:03 AM

The simplest technique is to use a bitwise and operator (&) against 1.

### #35 PlasticineGuy

• mov dword[esp+eax],0

Reputation: 281
• Posts: 1,436
• Joined: 03-January 10

## Re: Simple Challenge C++

Posted 02 July 2011 - 08:05 AM

Yes, we know. The purpose of this thread has since become to find creative solutions.

### #36 raspinudo

Reputation: 61
• Posts: 232
• Joined: 19-September 11

## Re: Simple Challenge C++

Posted 29 February 2012 - 03:29 PM

here is my solution:
Spoiler

Now I look forward to reading all of the smart solutions you guys/gals came up with!

This post has been edited by raspinudo: 29 February 2012 - 03:30 PM

### #37 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Simple Challenge C++

Posted 08 April 2012 - 09:27 PM

Here's a round about way of doing it (literally)
```bool isEven(int n) {
return ((n >> 1) | (n << 31)) < 2147483648;
}

```

This post has been edited by jjl: 08 April 2012 - 09:40 PM

### #38 vidit.ochani

Reputation: 0
• Posts: 2
• Joined: 06-April 12

## Re: Simple Challenge C++

Posted 27 April 2012 - 05:46 PM

Here's my solution..

Spoiler

### #39 dilipkumarc

Reputation: 0
• Posts: 3
• Joined: 05-May 12

## Re: Simple Challenge C++

Posted 06 May 2012 - 12:03 AM

codeprada, on 01 March 2011 - 04:18 PM, said:

Complete this program in just one line. It tells the user whether a number is even or odd.

Limitations:
• No use of the Mod(%) operator
• no use of functions
• no use of classes
• no use of / * - +
• a line is counted by the amount of semicolons used. so your return statement counts as one line
• you cannot alter any other line in the code

Good luck
```#include <iostream>
using namespace std;

bool isEven(int f)
{
//one line here
}

int main (int argc, char * argv[])
{
int f = 10;
cout << f << ( isEven(f)  ?  " is even" : " is odd" ) << endl;
return 0;
}
```

### #40 dilipkumarc

Reputation: 0
• Posts: 3
• Joined: 05-May 12

## Re: Simple Challenge C++

Posted 06 May 2012 - 12:16 AM

Hi every one.. I think I have a Best solution..take a look

SIMPLE... JUST ADD BELOW CODE IN THE isEven()

return !(f%2);

What do you say???

Please post if my solution is wrong ..Ty

### #41 ishkabible

• spelling expret

Reputation: 1667
• Posts: 5,811
• Joined: 03-August 09

## Re: Simple Challenge C++

Posted 06 May 2012 - 03:28 PM

the challenge disallows the use of '%'

### #42 aaa111

• D.I.C Regular

Reputation: 88
• Posts: 284
• Joined: 21-February 07

## Re: Simple Challenge C++

Posted 09 May 2012 - 07:39 AM

This is what i can came with:
Spoiler

### #43 jjl

• Engineer

Reputation: 1120
• Posts: 4,646
• Joined: 09-June 09

## Re: Simple Challenge C++

Posted 10 May 2012 - 06:35 PM

aaa111, on 09 May 2012 - 03:39 PM, said:

This is what i can came with:
Spoiler

Note that the ternary operator is a waste of a conditional check.

since anything anded with 0x01 is either 0x01 or 0x00 (true of false) you can just return the result directly.

This post has been edited by jjl: 10 May 2012 - 06:35 PM

### #44 timkd127

Reputation: 2
• Posts: 74
• Joined: 27-February 10

## Re: Simple Challenge C++

Posted 11 May 2012 - 06:53 AM

This was a welcome distraction from studying for my calc 3 final

Spoiler

This post has been edited by jimblumberg: 11 May 2012 - 06:55 AM
Reason for edit:: Fixed tags

### #45 alzy101

Reputation: 0
• Posts: 21
• Joined: 22-May 12

## Re: Simple Challenge C++

Posted 22 May 2012 - 12:54 PM

codeprada, on 01 March 2011 - 11:50 AM, said:

Spoiler

How does this work exactly? (I am pretty novice). I keep trying to break it down but to no avail since I don't even know what the '&' operator does in this expression.