Java If-Else Hackerrank Solution
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
int N = Integer.parseInt(bufferedReader.readLine().trim());
if (N % 2 != 0) {
System.out.println("Weird");
} else {
if (N >= 2 && N <= 5) {
System.out.println("Not Weird");
} else if (N >= 6 && N <= 20) {
System.out.println("Weird");
} else {
System.out.println("Not Weird");
}
}
bufferedReader.close();
}
}
Code Explanation
The code takes an integer input N from the user and uses if-else statements to perform the required conditional actions. Here's how the logic works:
If N is odd (N % 2 != 0), it prints "Weird."
If N is even (N % 2 == 0):
If N is between 2 and 5 (inclusive), it prints "Not Weird."
If N is between 6 and 20 (inclusive), it prints "Weird."
If N is greater than 20, it prints "Not Weird."
This logic fulfills the requirements of the given task.