473,395 Members | 1,343 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Smallest of 3 numbers without using Comparator operator.

How to Write a C program to find the smallest of three integers, without using any of the comparision operators.?
Nov 9 '08 #1
6 7164
JosAH
11,448 Expert 8TB
How to Write a C program to find the smallest of three integers, without using any of the comparision operators.?
Why do you want to do that? Is it a riddle from your textbook? If so, this is not
a do-my-homework service; first give it a try yourself.

kind regards,

Jos (moderator)

hint: check whether or not the sign bit of (a-b) is set.
Nov 9 '08 #2
whodgson
542 512MB
say 3 nums are n1,n2 & n3
say n1=3,n2=15 & n3=9
ni*n2==45................(1)
n1*n3==27...............(2)
n2*n3==135.............(3)
the smallest num is common to expression 1 and 2 which is n1 or 3
how rediculous!
Nov 10 '08 #3
Ganon11
3,652 Expert 2GB
Yes, but to determine which result was smallest, you'd have to use...a comparison operator.
Nov 10 '08 #4
JosAH
11,448 Expert 8TB
As I wrote before: use the sign bit of an expression. Something like this:

Expand|Select|Wrap|Line Numbers
  1. #define ALL ((unsigned int)~0)
  2. #define SGN (ALL^(ALL>>1))
  3. #define LESS(X,Y) (((X)-(Y))&SGN)
  4.  
LESS(x, y) will be true (not 0) when x < y. This trickery-dickery only works for
ints.

kind regards,

Jos
Nov 10 '08 #5
whodgson
542 512MB
Gannon11 says:
Yes, but to determine which result was smallest, you'd have to use...a comparison operator.

Why couldn't the 3 products be printed out as shown and then
cout<<"Which int: ";
cin>>numsmall;
user chooses n1 or n2 or n3 on the basis of which n is common to two smallest products
cout<<"The smallest int is: "<<numsmall;
QED but agreed <<<<<< than elegant
Nov 12 '08 #6
Let your three integers be:

int num1, num2, num3;

//starts computing smallest number

if ( num1 < num2 ) {
if ( num1 < num3 ) {
printf( "Smallest if is %d\n", num1 );
}
}
if ( num2 < num1 ) {
if ( num2 < num3 ) {
printf( "Smallest if is %d\n", num2 );
}
}
if ( num3 < num1 ) {
if ( num3 < num2 ) {
printf( "Smallest if is %d\n", num3 );
}
}
Oct 11 '10 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

11
by: mjdeesh_hi | last post by:
How can we perfom multiplication programatically without using + or * operator. Can any one help out in this one. Jagadeesh.
16
by: codergem | last post by:
How to add two numbers without using the plus operator?
94
by: krypto.wizard | last post by:
Last month I appeared for an interview with EA sports and they asked me this question. How would you divide a number by 7 without using division operator ? I did by doing a subtraction and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.