3 min to read
HackerRank 30 Days of Code Solution Day 3: Conditional Statements
In this tutorial, we will guide you through the process of solving the Day 3 Conditional Operators programming problem from HackerRank’s “30 Days of Code” challenge.
We will provide comprehensive code solutions in C++, Python, and JavaScript programming languages. With our help, you will gain a better understanding of the problem and learn how to approach and solve similar coding challenges.
Disclaimer: We encourage you to solve this challenge yourself before reading our tutorial. We have provided a detailed explanation of the problem and our solutions to help you check your work.
Conditional Statements
Conditional statements are a crucial component of programming that allows the execution of a block of code only if certain conditions are met. One type of conditional statement is the IF/Elif/Else statement, which checks if a condition is true and executes a specific block of code accordingly.
This enables developers to create more dynamic and flexible programs that can respond to varying circumstances in a sophisticated way. Conditional statements execute code if certain conditions are met. IF/Elif/Else are examples.
In this problem, we will print Weird/Not Weird
based on the number provided by the user.
Task
Based on the input number, n apply the following conditional statements:
- If the input number is odd then print
Weird
- If the input number is even and in the inclusive range of 2 to 5 then print
Not Weird
- If the input number is even and in the inclusive range of 6 to 20 then print
Weird
- If the input number is even and greater than 20 then print
Not Weird
It is a simple use case of conditional statements. We have to check the number and print the result based on the number.
Conditional Statement Solution Hackerrank Solution In Python
Conditional Statement Solution Hackerrank Solution In JavaScript(Js)
Explanation
In this problem, we have to print Weird/Not Weird
based on the input number. We have to use conditional statements to solve this problem.
- First, we will take input from the user and store it in a variable
N
. - If the number is not divisible by 2, then it is an odd number and we will print
Weird
. - If the number is divisible by 2, then we will check if the number is in the inclusive range of 2 to 5. If the number is in the inclusive range of 2 to 5 then we will print
Not Weird
. - If the number is divisible by 2, then we will check if the number is in the inclusive range of 6 to 20. If the number is in the inclusive range of 6 to 20 then we will print
Weird
. - If the number is divisible by 2, then we will check if the number is greater than 20. If the number is greater than 20 then we will print
Not Weird
.
Test Case
Tested on the Hackerrank platform for the following test case and passed all the test cases. Enclosed below is the screenshot of the test case.