Asked in
Hobbies & Collectibles
Math and Arithmetic
Java Programming
Numbers

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

Answer

User Avatar
Wiki User
July 13, 2009 7:00AM

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 Math 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 ....

Thanks and Regards,

P.Marimuthu

www.teknoturfian.blogspot.com