Simple Challenge C++

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »

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

#1 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Simple Challenge C++

Posted 01 March 2011 - 09:18 AM

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 :bananaman:
#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;
}


Is This A Good Question/Topic? 2
  • +

Replies To: Simple Challenge C++

#2 shintetsu_80  Icon User is offline

  • D.I.C Head

Reputation: 31
  • View blog
  • Posts: 105
  • Joined: 01-July 08

Re: Simple Challenge C++

Posted 01 March 2011 - 09:55 AM

There's a couple of ways to do this I added both and updated your main a bit for easier testing. Note that the isOdd method could have just been notted in isEven.

Spoiler

Was This Post Helpful? 2
  • +
  • -

#3 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Simple Challenge C++

Posted 01 March 2011 - 11:50 AM

nice going. my method was
Spoiler

This post has been edited by ishkabible: 02 March 2011 - 10:20 AM
Reason for edit:: please use spoiler tags when posting code for a challnge

Was This Post Helpful? 3
  • +
  • -

#4 arun5500  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 1
  • Joined: 03-March 11

Re: Simple Challenge C++

Posted 04 March 2011 - 12:27 AM

View Postcodeprada, on 01 March 2011 - 09:18 AM, 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 :bananaman:
Spoiler


hi give some more

This post has been edited by NickDMax: 06 March 2011 - 09:07 AM
Reason for edit:: added spoiler tags

Was This Post Helpful? 1
  • +
  • -

#5 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Simple Challenge C++

Posted 04 March 2011 - 05:20 AM

View Postarun5500, on 04 March 2011 - 03:27 AM, said:

hi give some more


i'm working on another one at the moment which involves the knowledge of classes, inheritance...etc basically OOP
Was This Post Helpful? 0
  • +
  • -

#6 carltech  Icon User is offline

  • What did you call me?
  • member icon

Reputation: 28
  • View blog
  • Posts: 997
  • Joined: 19-October 07

Re: Simple Challenge C++

Posted 06 March 2011 - 01:44 PM

here a much uglier and more basic method

Spoiler

This post has been edited by carltech: 06 March 2011 - 01:46 PM

Was This Post Helpful? 0
  • +
  • -

#7 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2246
  • View blog
  • Posts: 9,236
  • Joined: 18-February 07

Re: Simple Challenge C++

Posted 07 March 2011 - 01:45 PM

Quote

though this breaks the no functions rule


no it does not -- sizeof is not a function, it is an operator! haha!
Was This Post Helpful? 1
  • +
  • -

#8 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 09 March 2011 - 05:46 PM

i have a really simple solution. i don't think it gets much simpler than this.

Spoiler


edit:
after spending some time hacking out an intentionally overly complicated way to do this i came up with this.
Spoiler

This post has been edited by ishkabible: 09 March 2011 - 06:48 PM

Was This Post Helpful? 3
  • +
  • -

#9 codeprada  Icon User is offline

  • Changed Man With Different Priorities
  • member icon

Reputation: 943
  • View blog
  • Posts: 2,353
  • Joined: 15-February 11

Re: Simple Challenge C++

Posted 10 March 2011 - 06:03 AM

View Postishkabible, on 09 March 2011 - 08:46 PM, said:

i have a really simple solution. i don't think it gets much simpler than this.

Spoiler


edit:
after spending some time hacking out an intentionally overly complicated way to do this i came up with this.
Spoiler


the first one is just like my solution and the second is interesting. obfuscated? sizeof(int)<<3 == 32...too tired to decipher the rest =D

here's another one i did
Spoiler

This post has been edited by ishkabible: 10 March 2011 - 01:41 PM
Reason for edit:: use spoiler tags in challenges

Was This Post Helpful? 0
  • +
  • -

#10 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 10 March 2011 - 02:00 PM

her is an explnation
Spoiler

Was This Post Helpful? 1
  • +
  • -

#11 vishal30_9  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 11
  • Joined: 22-March 11

Re: Simple Challenge C++

Posted 28 March 2011 - 10:30 PM

Hi,
here is your solution. But i would prefer to make function as isOdd(int num).

Spoiler


edit by ishkabible:please use spoiler tags when posting in a challenge

This post has been edited by ishkabible: 29 March 2011 - 12:52 PM

Was This Post Helpful? 1
  • +
  • -

#12 ishkabible  Icon User is offline

  • spelling expret
  • member icon





Reputation: 1616
  • View blog
  • Posts: 5,707
  • Joined: 03-August 09

Re: Simple Challenge C++

Posted 29 March 2011 - 12:57 PM

@vishal30_9 : interesting use of subtraction ;)
Was This Post Helpful? 0
  • +
  • -

#13 asadullah.ansari  Icon User is offline

  • New D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 11
  • Joined: 16-January 08

Re: Simple Challenge C++

Posted 01 April 2011 - 06:00 AM

View Postcodeprada, on 01 March 2011 - 09:18 AM, 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 :bananaman:

#include <iostream>
using namespace std;

bool isEven(int f)
{
   retrun f &(f-1);
   
    //one line here
}

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

Was This Post Helpful? 0
  • +
  • -

#14 RevTorA  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 76
  • View blog
  • Posts: 246
  • Joined: 22-April 11

Re: Simple Challenge C++

Posted 04 May 2011 - 07:26 AM

#include <iostream>

void main(int argc, char* argv[])
{
	if(argc > 1) std::cout << atoi(argv[1]) << " is " << (((atoi(argv[1]) & 1) == 1) ? "odd" : "even");
}



How's that? :D

This post has been edited by RevTorA: 04 May 2011 - 07:27 AM

Was This Post Helpful? 0
  • +
  • -

#15 milawynsrealm  Icon User is offline

  • New D.I.C Head

Reputation: 4
  • View blog
  • Posts: 23
  • Joined: 12-May 11

Re: Simple Challenge C++

Posted 12 May 2011 - 06:53 PM

Here's mine:

bool isEven(int f)
{
	return ((f & 1) ? true : false);
}


This post has been edited by milawynsrealm: 12 May 2011 - 06:57 PM

Was This Post Helpful? 1
  • +
  • -

  • (5 Pages)
  • +
  • 1
  • 2
  • 3
  • Last »