Mathematical solution:
public class Test1 {
public static void main(String[] args) {
int i = Integer.parseInt(args[0]);
if((i-4)*(i-5)*(i-6) == 0 ){
System.out.println("4<=i<=6");
}else{
System.out.println("i!=4or5or6");
}
}
}
Other solution is bit tricky:
public class Test2 {
public static void main(String[] args) {
int i = Integer.parseInt(args[0]);
if("4,5,6".indexOf(String.valueOf(i)) >=0){
System.out.println("4<=i<=6");
}else{
System.out.println("i!=4or5or6");
}
}
}
Link to same question, I posted on LinkedIn:
ReplyDeleteLink To the same post on LinkedIn
what is the definition of logical operators?
ReplyDeletewhat are "==" and ">=" then? are they also logical operators:-)))
No they are not they are relational operators.
ReplyDeleteI liked the first one, simple but clever. This has given me another idea !!!
ReplyDeleteIt can be done this way as well:
ReplyDeletepublic class Test3 {
{
public static void main(String[] a)
{
int i=Integer.parseInt(a[0]);
if("456".contains(""+i+"")) {
System.out.println(i+" is 4,5or6");
}
else {
System.out.println(i+" is not 4,5or6");
}
}
}
Thanks Kulbeer..
ReplyDeleteI think Kulbeer's solution fails if we pass 56 or 45. It returns true in these cases also. But it should be checking only 4 or 5 or 6 right?
ReplyDeleteYou are right.. even it will not work for 456 also.. His solution can be made right if
ReplyDelete"456".contains(""+i+"") is replaced with
"4,5,6".contains(""+i+""
you can check that
Good one sir ji
ReplyDelete