# Greatest of two numbers without using comparison operators..

• (3 Pages)
• 1
• 2
• 3

## 36 Replies - 45646 Views - Last Post: 24 December 2008 - 07:47 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=77582&amp;s=211e5a633181c5ed232b27f79e727f81&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 suganthi14

Reputation: -1
• Posts: 8
• Joined: 13-December 08

# Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 01:34 AM

Is This A Good Question/Topic? 0

## Replies To: Greatest of two numbers without using comparison operators..

### #2 siddude

Reputation: 0
• Posts: 7
• Joined: 15-December 08

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 01:51 AM

suganthi14, on 20 Dec, 2008 - 12:34 AM, said:

```void great(int a,int b)
{
int c;
c=a-b;
if(c>0)
{
cout<<"a is greater then b"<<endl;
}
else
cout<<"b is greater then a"<<endl;
}

```

i hope comparison operator can be used in this way,if not let me know

### #3 Bench

• D.I.C Lover

Reputation: 859
• Posts: 2,343
• Joined: 20-August 07

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 03:13 AM

You might find this thread interesting (The discussion is on essentially the same subject)
http://www.dreaminco...showtopic=43479

This post has been edited by Bench: 20 December 2008 - 03:21 AM

### #4 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 04:39 AM

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Thank you for helping us helping you.

--------
What is the definition of "comparison operator" that applies to this challenge?
I am assuming the relational operators " < ", " > ", " <= " and " >= " can't be used.
Can the equality operators " == " and " != " be used?

What is the definition of "number" that applies to this challenge?
Are negative values allowed?

----
OK I have my code without any use of " < ", " > ", " <= ", " >= ", " == " or " != ".
Where's yours so we can compare?

This post has been edited by janotte: 20 December 2008 - 05:46 AM

### #5 suganthi14

Reputation: -1
• Posts: 8
• Joined: 13-December 08

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 06:25 AM

Here too the comparison operator is used na..? I too gave the same answer when I was asked. But its wrong

siddude, on 20 Dec, 2008 - 12:51 AM, said:

suganthi14, on 20 Dec, 2008 - 12:34 AM, said:

```void great(int a,int b)
{
int c;
c=a-b;
if(c>0)
{
cout<<"a is greater then b"<<endl;
}
else
cout<<"b is greater then a"<<endl;
}

```

i hope comparison operator can be used in this way,if not let me know

My code is
```main()
{
int a,b,c;
cin>>a>>b;
c=a-b;
if(c>0)
cout<<"a is the biggest";
elseif(c<0)
cout<<"b is the biggest";
elseif(c==0)
cout<<"Both r equal"
}
```

But the problem is, here also, comparison operators r used

janotte, on 20 Dec, 2008 - 03:39 AM, said:

Dream.In.Code has a policy by which we prefer to see a good faith effort on your part before providing source code for homework assignments. Please post the code you have written in an effort to resolve the problem, and our members would be happy to provide some guidance. Be sure to include a description of any errors you are encountering as well.

Thank you for helping us helping you.

--------
What is the definition of "comparison operator" that applies to this challenge?
I am assuming the relational operators " < ", " > ", " <= " and " >= " can't be used.
Can the equality operators " == " and " != " be used?

What is the definition of "number" that applies to this challenge?
Are negative values allowed?

----
OK I have my code without any use of " < ", " > ", " <= ", " >= ", " == " or " != ".
Where's yours so we can compare?

### #6 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 06:46 AM

Well it was obvious that siddude's solution was wrong.
That kinda went without saying.

Bit sad you have nothing better to offer so far.
You set the challenge.
Now show us what you can do.

Where's yours?

Just to prove I'm not lying here's the start of my code (with some snipped out because it was too much of a clue):
```/* Challenge Code   **********************************
**   Determine the greater of 2 numbers without use **
**   of comparison operators						**
*****************************************************/

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main()
{
char NEG_1[2] = "-";

cout << endl << "Enter the first number: ";
int x;
cin >> x;
cout << endl << "Enter the second number: ";
int y;
cin >> y;

int z = x - y;

// <snip>
cout << endl << "The two numbers are equal." << endl << endl;
//...

```

### #7 suganthi14

Reputation: -1
• Posts: 8
• Joined: 13-December 08

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 10:17 AM

I cant get the logic.
Please give me a clue.. I ll develop the code

### #8 KYA

• yay verily

Reputation: 3155
• Posts: 19,200
• Joined: 14-September 07

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 10:20 AM

Let's say we have two numbers 'a' and 'b'.

If 'b' is greater then 'a' then 'a' minus 'b' would be negative. If they are equal then it would be zero. So on and so forth.

edited because i double posted for some reason

This post has been edited by KYA: 20 December 2008 - 10:21 AM

### #9 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 04:05 PM

KYA, on 20 Dec, 2008 - 09:20 AM, said:

Let's say we have two numbers 'a' and 'b'.

If 'b' is greater then 'a' then 'a' minus 'b' would be negative. If they are equal then it would be zero. So on and so forth.

edited because i double posted for some reason

KYA is giving you the same clue that this line in my code gives you:
```char NEG_1[2] = "-";

```

Now my answer (at this point) is only good for two numbers greater than zero but I could generalise it further if I cared enough (which I don't at this point).

No need to follow my path.
This is a logic puzzle based on what you know about numbers and C++ (or C).
There is no special coding knowledge required just some lateral thinking.

Because I suspect this may be for school I'm not giving you the answer now.
When is the assignment due?
I might share a week after that (maybe).

### #10 suganthi14

Reputation: -1
• Posts: 8
• Joined: 13-December 08

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 09:21 PM

Its not assignment. It was asked as one of my viva questions.. I answered everythin but this! So the due date is over.

### #11 no2pencil

• Professor Snuggly Pants

Reputation: 5963
• Posts: 29,130
• Joined: 10-May 07

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 09:27 PM

Doing a Google search for the phrase :

Greatest of two numbers without using comparison operators

shows some pretty relevant references.

When your post simply says "Thanx in advance", it's pretty offensive. It's not even like you are asking for help, just demanding the results, & oh by the way, thanks. Dream In Code is not the fast food coding site of the internet. You don't place an order & then pick up at the end of the bar.

### #12 suganthi14

Reputation: -1
• Posts: 8
• Joined: 13-December 08

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 09:34 PM

``` c=a/b;
if(c!=0)
cout<<"a is the biggest";
else
cout<<"b is the biggest";
```

but this doesnt work for negativ values

Excuse me No2pencil.. Its not that way. I dont need the code. I just need a clue about the logic..

### #13 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Greatest of two numbers without using comparison operators..

Posted 20 December 2008 - 11:38 PM

suganthi14, on 20 Dec, 2008 - 08:34 PM, said:

<snip>
if(c!=0)
<snip>

You can't use any of these:
" < ", " > ", " <= ", " >= ", " == " or " != "
If you include them there is no point in posting your code because it must be wrong.
You have to think in a different way.
Can you do it?
Here is a huge hint
(obscured somewhat but the hint is there if you are able to think like a programmer)
```while(true)

```

---

BTW - What is a "viva question"?
Just asking out of interest because I have no idea.

This post has been edited by janotte: 21 December 2008 - 12:25 AM

### #14 suganthi14

Reputation: -1
• Posts: 8
• Joined: 13-December 08

## Re: Greatest of two numbers without using comparison operators..

Posted 21 December 2008 - 02:49 AM

viva voce refers to oral questions asked by the external examiner during the practical exam..

```c=a/b;
if(c)
cout<<a is the biggest;
else
cout<<b is the biggest;

```

I think this may be correct..

janotte, on 20 Dec, 2008 - 10:38 PM, said:

suganthi14, on 20 Dec, 2008 - 08:34 PM, said:

<snip>
if(c!=0)
<snip>

You can't use any of these:
" < ", " > ", " <= ", " >= ", " == " or " != "
If you include them there is no point in posting your code because it must be wrong.
You have to think in a different way.
Can you do it?
Here is a huge hint
(obscured somewhat but the hint is there if you are able to think like a programmer)
```while(true)

```

---

BTW - What is a "viva question"?
Just asking out of interest because I have no idea.

### #15 janotte

• code > sword

Reputation: 990
• Posts: 5,141
• Joined: 28-September 06

## Re: Greatest of two numbers without using comparison operators..

Posted 21 December 2008 - 03:08 AM

suganthi14, on 21 Dec, 2008 - 01:49 AM, said:

viva voce refers to oral questions asked by the external examiner during the practical exam..

Ah yes - of course.
Sorry, I was being dense.

suganthi14, on 21 Dec, 2008 - 01:49 AM, said:

I think this may be correct..
```c=a/b;
if(c)
cout<<a is the biggest;
else
cout<<b is the biggest;

```

You have made the breakthrough with this line:
```if(c)

```

but this:
```c=a/b;

```

is terrible thinking.

Try compiling and running your code (wrapped in the appropriate support code) and see what it does.
```if(c)

```

does if c is:
- zero
- greater than zero
- less than zero
(or if you can't remember how an if() statement works just try various values of c in a simple program to see what if© does).

But be of good heart you have made the first major breakthrough.
Just thinking about what you have found out will allow you to mostly solve this.
You still need to make another breakthrough but the one you have made will get you 70% of the way to the solution.