Day 3: Intro to Conditional Statements

Sort by

recency

|

1801 Discussions

|

  • + 0 comments

    Easy solution!

  • + 0 comments
    N = int(input())
    print("Weird" if N % 2 or 6 <= N <= 20 else "Not Weird")
    
  • + 0 comments
        N = int(input().strip())
        if (N % 2) > 0:
            print('Weird')
        elif (N % 2) == 0 and N in range(2,5):
            print('Not Weird')
        elif (N % 2) == 0 and N in range(6,20):
            print('Weird')
        elif (N % 2) > 0 and N > 20:
            print('Not Weird')
        else:
            print('Not Weird')
    
  • + 0 comments

    Interesting thing to note:

    In Java this line gives an error while in javascript it will execute:

    (N >= 6 && N <= 20) ? System.out.println("Weird"): System.out.println("Not Weird");

    The Java compiler expects the ternary operator to be used as an expression, not as a statement. When the compiler encounters the System.out.println statements, it expects them to be part of a larger expression, but they are not. This causes a syntax error, and the code is not compiled.

  • + 0 comments

    Below is the code that goes under main function:

    function main() { const N = parseInt(readLine().trim(), 10); if (N % 2 !== 0) { 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"); } }