sanjay
sanjay

Reputation: 23

Find the biggest number among two using only one if condition?

Today was my first class at the institute for java developer course. The lecturer put forward a question for the whole class:

Find the biggest number among two numbers by using only one condition, i.e., if condition only. Do not use any other conditions like using if-else, else-if, using if two times, ternary, loops, etc.

Upvotes: 2

Views: 646

Answers (2)

Vivek
Vivek

Reputation: 436

int first = 10;
int second = 20;
int max = first;
if (second > max) {
    max = second;
}
System.out.println(max);

Upvotes: 0

John Kugelman
John Kugelman

Reputation: 361595

int max = a;

if (b > a) {
    max = b;
}

Upvotes: 9

Related Questions