• arunslb123

MemberOct 21, 2011

## find largest of three numbers without using comparsion operator

Hello Friends.

I am not able to solve a question in C If u have any idea pls help me

The Question is

program to find largest of three numbers without using comparsion operator
Howdy guest!
Dear guest, you must be logged-in to participate on CrazyEngineers. We would love to have you as a member of our community. Consider creating an account or login.
Replies

Share your attempts at coding this program and tell us where exactly you're stuck.
Are you sure? This action cannot be undone.
• MemberOct 21, 2011

ct=0;
while(var1 && var2 && var3){
var1++;
var2++;
var3++;
ct--;
}
printf("%d",ct);

could u pls explain this
Are you sure? This action cannot be undone.
• MemberOct 21, 2011

@arunslb123: This is the beauty of programming, you can make it dance the way you want. The program you have given produces correct result. Lets start it with taking an example, suppose a(var1)=4, b(var2)=5, c(var3)=6 and u have already said ct=0. As u might know, that while loop runs till the condition is true and to stop it one of the variables has to be 0. So, when you start the execution the values of each variable starts increasing and ct starts to go in negative. Now our poor integer can take only 32768 values. So when each of the variable reaches 32768 it starts running in negative(whoa...😉). And as you are doing incremental operation, and as the value -5 is greater than -6, all the variables start to run towards 0.

NOw u can question, what d hell is ct doing?? Well, it is also in race, but it is running in opposite direction,coz u gave it the sign "--". It goes to negative side first and it will cover positive side. So, the race is on and we know that as soon as any of the variable becomes zero, the loop execution will stop. So, the big question is who wins the race, and you guessed it right, the variable with the biggest value. It started first, which is not fair though...😉, but still he is the winner. As soon as any of the variable becomes zero loop stops and guess what the ct is now reached to the position of highest value variable. As they were running fairly, each of them having one step at a time. So, ct will stop at the highest value and print it.
Are you sure? This action cannot be undone.
• MemberOct 21, 2011

thanks a lot bro......
Are you sure? This action cannot be undone.
• MemberOct 22, 2011

i didnt get the logic of while loop cn sm1 explain plz
Are you sure? This action cannot be undone.
• MemberOct 22, 2011

the logic of while loop is that it will execute till the arguement we put in becomes false.
the false is represented by the value 0.
so for ex,
while ( x)
{
statements;
x--;
}
this loop will execute till the value of x becomes 0.
Are you sure? This action cannot be undone.
• MemberOct 22, 2011

no no i know the while loop logic,,i mean logic of coding in this program and can you plz write full program.
Are you sure? This action cannot be undone.
• MemberOct 22, 2011

@diwana: I guess i wrote the whole logic already, u can read it again and if you any problem ask it specifically, i mean where exactly u have the problem. And its the whole program, u just need to add header files and yeah, braces too, and u might wanna declare the variables also before using them....😛 , thats it, thats the whole program. And i see you have started another thread for the same program namely "question of programming in C". Well, you have the answer in this thread only....😀
Are you sure? This action cannot be undone.
• MemberJan 11, 2012

*CROSS POSTER*