input two numbers greater than zero and print which one of them is nearest to 21 without getting over the number 21
import
java.io.*;
class
Nearer
{
public
static
void
main(String args[])
throws
IOException
{
BufferedReader br=
new
BufferedReader(
new
InputStreamReader(System.in));
System.out.print(
"Enter 1st number : "
);
int
a = Integer.parseInt(br.readLine());
System.out.print(
"Enter 2nd number : "
);
int
b = Integer.parseInt(br.readLine());
int
d1 = Math.abs(
21
-a);
int
d2 = Math.abs(
21
-b);
if
(d1 > d2)
System.out.println(b+
" is nearer to 21."
);
else
if
(d2 > d1)
System.out.println(a+
" is nearer to 21."
);
else
System.out.println(
"Both "
+a+
" and "
+b+
" are at equal distance from 21."
);
}
}
No comments:
Post a Comment