Introduction To Conditional Statement In C#

Here we are going to solve the particular challenge of the conditional statement where we have to print a different kind of word or statement by comparing the result of operations of the particular input given by the user. So, what we have to do is,

  • We have n number as an integer which is input by the user
  • Print weird if n is odd.
  • Print not weird if the n is even and in the range of 2 to 5
  • Print weird if the n is even and in the range of 6 to 20
  • Print not weird if the n is even and greater than 20

So, this is the task of the problem statement, and now one by one we are going to check the condition and as per the condition we are going to return the string and we are using the approach of the function. So, we can reuse it also whenever something like this challenge we have.

So, defining the function,

 public static string CheckValue(int N)
 {
    //Body Part....
 }

Now, We have a function with a proper integer argument and now we are going to check the first condition and return the string as per that.

if (N % 2 == 1)
{
    return "Weird";     //For the Odd Value....
}

For the Second Condition,

else if (N % 2 == 0 && N > 2 && N < 5 )
{
    return "Not Weird";    //For even number and between 2 to 5
}

For the third Condition,

else if (N % 2 == 0 && N > 6 && N <= 20)
{
     return "Weird";  //For n as Even number and between 6 to 20
}

For the last condition 

else if (N % 2 == 0 && N > 20)
{
    return "Not Weird";     //For n as even number and n greater than 20...
}

Now when you are going to merge this whole thing one by one inside the function and directly run it then it may give you an error. So, for that, the complete function should look like this.

public static string CheckValue(int N)
{
    if (N % 2 == 1)
    {
        return "Weird";
    }
    else if (N % 2 == 0 && N > 2 && N < 5 )
    {
        return "Not Weird";
    }
    else if (N % 2 == 0 && N > 6 && N <= 20)
    {
        return "Weird";
    }
    else if (N % 2 == 0 && N > 20)
    {
        return "Not Weird";
    }
    else
    {
        return "";
    }
}

NOTE
Following is the full source code for the hackerrank 30daysofCode day 3 solution.

using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution
{
    public static void Main(string[] args)
    {
        int N = Convert.ToInt32(Console.ReadLine().Trim());
        
        Console.WriteLine(CheckValue(N));
    }
    public static string CheckValue(int N)
    {
        if (N % 2 == 1)
        {
            return "Weird";
        }
        else if (N % 2 == 0 && N > 2 && N < 5 )
        {
            return "Not Weird";
        }
        else if (N % 2 == 0 && N > 6 && N <= 20)
        {
            return "Weird";
        }
        else if (N % 2 == 0 && N > 20)
        {
            return "Not Weird";
        }
        else
        {
            return "";
        }
    }
}


Similar Articles