Java program to check that average of 5 numbers is greater than 150 or not using if-else and java ternary operator.
- public class Average {
- /**
- * Check that average of 5 number is greater than 150 or not
- *
- * @param args
- */
- public static void main(String[] args) {
- // using java if-else
- System.out.println("==== using java if-else ===");
- checkNumber(37, 45, 87, 100, 200); // Output -> Less than 150
- checkNumber(0, 500, 245, 784, 1); // Output -> Greater than 150
- checkNumber(27, 0, 8, 1, 88); // Output -> Less than 150
- // using java ternary operator
- System.out.println("==== using java ternary operator ===");
- checkNumberUsingTernary(37, 45, 87, 100, 200); // Output -> Less than 150
- checkNumberUsingTernary(0, 500, 245, 784, 1); // Output -> Greater than 150
- checkNumberUsingTernary(27, 0, 8, 1, 88); // Output -> Less than 150
- }
- // using java if-else
- private static void checkNumber(int n1, int n2, int n3, int n4, int n5) {
- int avg = (n1 + n2 + n3 + n4 + n5) / 5;
- if (avg > 150) {
- System.out.println("Greater than 150");
- } else {
- System.out.println("Less than 150");
- }
- }
- // using java ternary operator
- private static void checkNumberUsingTernary(int n1, int n2, int n3, int n4, int n5) {
- int avg = (n1 + n2 + n3 + n4 + n5) / 5;
- String result = avg > 150 ? "Greater than 150" : "Less than 150";
- System.out.println(result);
- }
- }
Output
==== using java if-else ===
Less than 150
Greater than 150
Less than 150
==== using java ternary operator ===
Less than 150
Greater than 150
Less than 150
No comments:
Post a Comment