HackerRank Java- If-Else




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.



  1. import java.util.Scanner;
  2. public class Solution {
  3. public static void main(String[] args) {
  4. /* Read input */
  5. Scanner scan = new Scanner(System.in);
  6. int n = scan.nextInt();
  7. scan.close();
  8. /* Determine answer */
  9. String ans = "";
  10. if (n % 2 == 1) {
  11. ans = "Weird";
  12. } else {
  13. if (n >= 6 && n <= 20) {
  14. ans = "Weird";
  15. } else {
  16. ans = "Not Weird";
  17. }
  18. }
  19. /* Print output */
  20. System.out.println(ans);
  21. }
  22. }




codesadda.com

Codesadda.com is your home of programming solutions, tutorials, video tutorials and much more. Sign Up for our weekly newsletter to get update about new content.

Like us on Facebook | Connect with us on LinkedIn | Subscribe our Channel on Youtube