🔢 Day 3: Introduction to Conditional Statements – HackerRank Practice with C#
🎯 Objective
In this challenge, you'll practice using conditional statements (if, else if, else) to control the program flow based on input values.
🧠 Problem Statement
Given an integer N, apply the following rules:
If N is odd, print "Weird".
If N is even and in the inclusive range 2 to 5, print "Not Weird".
If N is even and in the inclusive range 6 to 20, print "Weird".
If N is even and greater than 20, print "Not Weird".
📥 Input Format
A single line containing a positive integer N.
📤 Output Format
Print either "Weird" or "Not Weird" based on the rules above.
🧪 Sample Input
3
🧪 Sample Output
Weird
🔍 Explanation
Since 3 is odd, it matches the first condition. So, we print "Weird".
💡 Starter Code
using System;
class Solution
{
public static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine().Trim());
// Your code here
}
}
✅ Final Solution Code
using System;
class Solution
{
public static void Main(string[] args)
{
int N = Convert.ToInt32(Console.ReadLine().Trim());
if (N % 2 != 0) // Odd number
{
Console.WriteLine("Weird");
}
else // Even number
{
if (N >= 2 && N <= 5)
{
Console.WriteLine("Not Weird");
}
else if (N >= 6 && N <= 20)
{
Console.WriteLine("Weird");
}
else if (N > 20)
{
Console.WriteLine("Not Weird");
}
}
}
}
🧠 Key Takeaways
% (modulo) operator helps determine if a number is odd or even.
Use if, else if, and else blocks to evaluate multiple conditions in sequence.
Condition order matters! Always test from specific to general.