Such programs are too simple and requires the fundamental logic for checking whether a number is odd or ever. Any number which is divisible by 2 is even else odd. In the below code snippet, you will find two operators % and /.
x=4
y=2
x%y will computes to 0 which is the remainder!!
x/y will compute to 2 which is the quotient.
# Below is the code:
class EvenOrOdd {
public String compute(int x) {
if (x % 2 == 0) {
return x + " is even";
} else
return x + " is odd";
}
}
public class TestEvenOrOdd {
public static void main(String[] args) {
EvenOrOdd ref = new EvenOrOdd();
System.out.println(ref.compute(5));
System.out.println(ref.compute(10));
}
}
#Output:
x=4
y=2
x%y will computes to 0 which is the remainder!!
x/y will compute to 2 which is the quotient.
# Below is the code:
class EvenOrOdd {
public String compute(int x) {
if (x % 2 == 0) {
return x + " is even";
} else
return x + " is odd";
}
}
public class TestEvenOrOdd {
public static void main(String[] args) {
EvenOrOdd ref = new EvenOrOdd();
System.out.println(ref.compute(5));
System.out.println(ref.compute(10));
}
}
#Output:
Comments
Post a Comment