Saturday, July 11, 2009

How do you find greatest number among three without "conditional operator" and "Math Library Functions"?

I got this question from my friend vengat : How do you find greatest number among three without "conditional operator" and "Math Library Functions"?
First of all,I would like to thank him ..



This is what i done,


import java.io.Writer;
import java.util.Scanner;

public class Teknoturf {
public static void main(String[] args){

System.out
.println("This is Print Greater No without Using < > symbol and Library");
Scanner scan = new Scanner(System.in);
System.out.println("Enter No1------------->>");
int no1 = scan.nextInt();
System.out.println("Enter No2------------->>");
int no2 = scan.nextInt();
System.out.println("Enter No3------------->>");
int no3 = scan.nextInt();

Utility(no1, no2, no3);

}

static void Utility(int a, int b, int c) {
int temp = 0, temp1 = 0;

Integer AminusB = a - b;
String strAminusB = AminusB.toString();
if (strAminusB.substring(0, 1).equalsIgnoreCase("-")) {
temp = (a + b) / 2 - -(a - b) / 2;

} else {
temp = (a + b) / 2 - (a - b) / 2;
}

temp1 = a + b;
temp = temp1 - temp;

int help1 = temp - c;
Integer tempMinusC = temp - c;
String strtempMinusC = tempMinusC.toString();


if (strtempMinusC.substring(0, 1).equalsIgnoreCase("-")) {
temp1 = (temp + c) / 2 - -(temp - c) / 2;
} else {
temp1 = (temp + c) / 2 - (temp - c) / 2;
}

temp = temp + c;
temp1 = temp - temp1;
System.out.println("Greatest Number----------------------->>" + temp1);

}
}


this is only work for positive numbers ....