Wednesday, March 23, 2016

Java If-Else - Hacker Rank Solution

Using "if-else" you can perform decision making in Java. See the flowchart below (taken from wikipedia):

 Java If-Else -  Hacker Rank Solution

This problem will test your knowledge on "if-else" statements.
Given an integer NN as input, check the following:
  • If NN is odd, print "Weird".
  • If NN is even and, in between the range of 2 and 5(inclusive), print "Not Weird".
  • If NN is even and, in between the range of 6 and 20(inclusive), print "Weird".
  • If NN is even and N>20N>20, print "Not Weird".
We given you partially completed code in the editor, complete it to solve the problem.
Input Format
There is a single line of input: integer NN.
Constraints
1N1001N100
Output Format
Print "Weird" if the number is weird. Otherwise, print "Not Weird". Do not print the quotation marks.
Sample Input 1
3
Sample Output 1
Weird
Sample Input 2
24
Sample Output 2
Not Weird

 

Java If-Else - Hacker Rank Solution  


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

public class Solution {

    public static void main(String[] args) {
        try{
        BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        int N=Integer.parseInt(br.readLine().trim());
        if(N<1||N>100)
            throw new Exception();
        if((N&1)==1)
        {
            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");
            }
        }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}
 

Java If-Else - Hacker Rank Solution  

 

1 comment:

Powered by Blogger.