Algorithm


 Problem Name: 30 days of code - Day 3: Intro to Conditional Statements

Problem Link: https://www.hackerrank.com/challenges/30-conditional-statements/problem?isFullScreen=true

In this HackerRank in 30 Days of Code - Day 3: Intro to Conditional Statements problem solution,

Objective
In this challenge, we learn about conditional statements. Check out the Tutorial tab for learning materials and an instructional video.

Task:

Given an integer,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

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 is not weird. Thus, we print Not Weird.

 

 

Code Examples

#1 Code Example with C Programming

Code - C Programming


#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// If  is odd, print Weird
// If  is even and in the inclusive range of  to , print Not Weird
// If  is even and in the inclusive range of  to , print Weird
// If  is even and greater than , print Not Weird

int main()
{
    int n;
    scanf("%d",&n);

    if(n%2==1 || (n%2==0 && n>=6 && n<=20))
    printf("Weird\n">;
    else if(n%2==0 && ( (n>=2 && n<=5> || n>20))
    printf("Not Weird\n");

}
Copy The Code & Try With Live Editor

#2 Code Example with C++ Programming

Code - C++ Programming


#include <iostream>

using namespace std;

int main() {
    int N;
    cin >> N;

    if (N % 2 != 0) cout << "Weird";
    else {
        if (N  < = 5) cout << "Not Weird";
        else if (N  < = 20) cout << "Weird";
        else cout << "Not Weird";
    }
}
Copy The Code & Try With Live Editor

#3 Code Example with C# Programming

Code - C# Programming


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());
        if (N % 2 != 0) Console.WriteLine("Weird");
        else
        {
            if (N %2==0 && N  < = 5) Console.WriteLine("Not Weird");
            else if (N %2==0 && N <= 20) Console.WriteLine("Weird");
            else if (N %2==0 && N > 20) Console.WriteLine("Not Weird");
        }
    }
}

Copy The Code & Try With Live Editor

#4 Code Example with Java Programming

Code - Java Programming


import java.util.Scanner;

public class Solution {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt();
        scan.close();

        if (n % 2 == 0) {
            if (n  <  6) System.out.println("Not Weird");
            else if (n < 21) System.out.println("Weird");
            else System.out.println("Not Weird");
        } else System.out.println("Weird");
    }
}
Copy The Code & Try With Live Editor

#5 Code Example with Javascript Programming

Code - Javascript Programming


function main() {
    const N = parseInt(readLine(), 10);
    if (N % 2 != 0) {
        console.log('Weird')
    } else if (N > 20) {
        console.log('Not Weird')
    } else if (N >= 6) {
        console.log('Weird')
    } else if (N >= 2) {
        console.log('Not Weird')
    }
}
Copy The Code & Try With Live Editor

#6 Code Example with Python Programming

Code - Python Programming


N = int(input())

if N % 2 != 0:
    print("Weird")
else:
    if N <= 5:
        print("Not Weird")
    elif N <= 20:
        print("Weird")
    else:
        print("Not Weird")
Copy The Code & Try With Live Editor

#7 Code Example with PHP Programming

Code - PHP Programming


$stdin = fopen("php://stdin", "r");
fscanf($stdin, "%d\n", $i);
fclose($stdin);
if($i > 0 && $i <= 100){
    if($i%2==0>{
        if($i >=2 && $i <=5>{
            print "Not Weird";
        }
        elseif($i >=6 && $i <=20>{
            print "Weird";
        }
        elseif($i>20){
            print "Not Weird";
        }
    }else{
        print "Weird";
    }
}
Copy The Code & Try With Live Editor
Advertisements

Demonstration


Previous
[Solved] Day 2: Operators solution in Hackerrank - Hacerrank solution C, C++, C#, java, Js, PHP, Python in 30 days of code
Next
[Solved] Day 4 Class vs. Instance solution in HackerRank - HackerRank solution C, C++, C#, java, Js, PHP, Python in 30 days of code