Question : Given three ints, a b c, print true if one of b or c is "close" (differing from a by at most 1), while the other is "far", differing from both other values by 2 or more.
Note : Math.abs(num) computes the absolute value of a number.
Output:
1, 2, 10 -> true
1, 2, 3 -> false
4, 1, 3 -> true
Answer:
import java.util.*;
import java.lang.Math;
public class CloseFar
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter num 1 : ");
int a=sc.nextInt();
System.out.println("Enter num 2 : ");
int b=sc.nextInt();
System.out.println("Enter num 3 : ");
int c=sc.nextInt();
if(Math.abs(b-a)<=1 || Math.abs(c-a)<=1)
{
System.out.println("true");
}
else
{
System.out.println("false");
}
}
}
Click to download
|