Day 3: Intro to Conditional Statements (HackerRank – 30 Days of Code)

Task:

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


Solution:

1)Javascript:

function main() {
    const N = parseInt(readLine().trim(), 10);
    if(N%2){
        console.log("Weird");
    }else{
        if(N>=2 && N<=5)console.log("Not Weird");
        else if(N>=6 && N<=20) console.log("Weird");
        else console.log("Not Weird");
    }
}

2)Java:

 public static void main(String[] args) throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));

        int N = Integer.parseInt(bufferedReader.readLine().trim());
        if(N%2==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");
    }
        bufferedReader.close();
   }

Leave a Reply