HackerRank Java IfElse Problem Solution

Published October 03, 2021

In this Java example tutorial will explain about HackerRank Java IfElse Problem solution.

Java IfElse Challenge

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   to  , print  Not Weird
  • If N is even and in the inclusive range of   to  , print  Weird
  • If  N is even and greater than  , print  Not Weird

 

Java IfElse HackerRank problem solution

 

Input Format
The Enter any number with in the range of 100
Constraints 1<=N<=100

Output Format
Print  Weird  if the number is weird; otherwise, print  Not Weird

 

 

Solution

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int n = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");

        scanner.close();

        if((1<=n)&&(n<=100)){
            if(n%2==0)
            {
                if(((2<=n)&&(n<=5))||(n>20))
                {
                    System.out.println("Not Weird");
                }else if((6<=n)&&(n<=20))
                {
                    System.out.println("Weird");
                }

            }else
            {
                System.out.println("Weird");
            }
        }
    }
}

 

Input 3:

Output Weird

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

270 Views