HackerRank.com 30 Days of Code – Day 3: Intro to Conditional Statements

Here is my solution to HackerRank.com’s 30 days of code challenge, day 3. This is a basic introduction to conditional statements which I found a bit easy. My main take away from this was ensuring the quality of the input by making sure that the the input values were within the correct range and wrapping that conditional statement around the other application logic statements. It doesn’t make sense to continue on processing the input if it is not within the acceptable bounds.

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) {
      Scanner scan = new Scanner(System.in);
      int n = scan.nextInt(); 
      scan.close();
      String ans="";
      
      //make sure n is within the constraints (between 1 and 100 inclusive)
      if( n < 1 || n > 100){
          ans="";
      }
      else
      {
          // if 'n' is NOT evenly divisible by 2 (i.e.: n is odd)
          if(n%2==1){
             ans = "Weird";
          }
          else //cover all of the even cases
          {
             //if n is between 2 and 5 inclusive 
             if(n >= 2 && n <= 5){ ans = "Not Weird"; } //if n is between 6 and 20 inclusive if(n >= 6 && n <= 20){ ans = "Weird"; } //if n is greater than 20 if(n > 20){
                 ans = "Not Weird";
             }
              
             /*
             If  is even and in the inclusive range of 2 to 5, print Not Weird
             If  is even and in the inclusive range of 6 to 20, print Weird
             If  is even and greater than 20, print Not Weird
             */
          }   
      }
       

      System.out.println(ans);
   }
}

Comments are closed.