Problem Statement
Given an integer N, perform the following conditional actions:
- If N is odd, print Weird
- If N is even and in the inclusive range of 2 to 5, print Not Weird
- If N is even and in the inclusive range of 6 to 20, print Weird
- If N is even and greater than 20, print Not Weird
Complete the stub code provided in your editor to print whether or not N is weird.
Input Format: A single line containing a positive integer, N.
Constraints: 1<=N<=100
Output Format: Print Weird if the number is weird; otherwise, print Not Weird.
My Solution
if(N%2!=0)
System.out.println("Weird");
else{
if(N>=2&&N<=5||N>20)
System.out.println("Not Weird");
else
System.out.println("Weird");
}
Another Solution-
if(N%2==1)
System.out.println("Weird");
else
{
if(N>=2&&N<=5)
System.out.println("Not Weird");
else if(N<=20)
System.out.println("Weird");
else
System.out.println("Not Weird");
}
My question is, does one algorithm have better performance than the other, in other words, is there any advantage to using else if statements rather than logical operators.
P.S- I am new to coding in Java, and was just curious if there is an optimum way to write code that has better performance.