Day 3: Intro to Conditional Statements | 30 Days of Code | Hackerrank Solution
Hello coders,
Today we are going to Solve Day 3: Intro to Conditional Statement problem which is the third day problem of HackerRank 30 Days of Code.
In this post we will mainly know about If statement, elif statement and else statement. These conditional statements are very crucial in any of the programming language.
So. it is must to know about these topics. After going through this post you will completely understand what is Conditional Statements and how to imply them in your program.
So, Without further Ado let' jump into the question.
Contents
Objective
In this challenge, we're getting started with conditional statements.
Conditional Statements
These are a way of programming different workflows depending on some boolean condition. The if-else statement is probably the most widely used conditional in programming.
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.
Sample Input 0
3
Sample Output 0
Weird
Sample Input 1
24
Sample Output 1
Not Weird
Explanation
Sample Case 0: n = 3
n is odd and odd numbers are weird, so we print Weird.
Sample Case 1: n = 24
n > 20 and n is even, so it isn't weird. Thus, we print Not Weird.
Solution - Day 3: Intro to Conditional Statements in Python
Python 3 Solution
#!/bin/python3 import math import os import random import re import sys if __name__ == '__main__': N = int(input()) if N % 2 != 0: print("Weird") elif N % 2 == 0 and 2 <= N <= 5: print("Not Weird") elif N % 2 == 0 and 6 <= N <= 20: print("Weird") else: print("Not Weird")
![]() |
Intro to Conditional Statements - Solution |
![]() |
Intro to Conditional Statements - Results |
Conclusion
This was the solution of our Day 3: Intro to Conditional Statements problem. If you have any query regarding the problem (Day 3: Intro to Conditional Statements), feel free to contact in the Comment Section.
Don't forget to Share this post with your friends and Subscribe our Blog to get the latest updates related to coding.
Disclaimer: The above problem is generated by HackerRank but the solution is provided by Sloth Coders.
Happy Coding!!
A Sloth Who loves to Code.