Day 3: Into to Conditional Statements | 30 Days Of Code | HackerRank Solution

Hello coders, today we are going to solve Day 3: Into to Conditional Statements HackerRank Solution in C++, Java and Python.

Day 3: Into to Conditional Statements

Objective

In this challenge, we learn about conditional statements.

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

Solution – Day 3: Into to Conditional Statements

C

#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>
char* readline();
int main()
{
    char* N_endptr;
    char* N_str = readline();
    int N = strtol(N_str, &N_endptr, 10);
    // Complete the solve function below.
    
 if(N%2==0)
    {
        if(N>=2 && N<=5)
        {
            printf("Not Weird"); 
  }
        else if(N>=6 && N<=20)
        {
         printf("Weird");
  }
        else
        {
         printf("Not Weird");
  }    
    }
    else
    {
     printf("Weird"); 
 }
        
    if (N_endptr == N_str || *N_endptr != '\0') { exit(EXIT_FAILURE); }
    return 0;
}
char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);
    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);
        if (!line) { break; }
        data_length += strlen(cursor);
        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') { break; }
        size_t new_length = alloc_length << 1;
        data = realloc(data, new_length);
        if (!data) { break; }
        alloc_length = new_length;
    }
    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';
    }
    data = realloc(data, data_length);
    return data;
}

C++

#include <bits/stdc++.h>

using namespace std;

int main()
{
    int N;
    cin >> N;
    cin.ignore(numeric_limits<streamsize>::max(), '\n');
    // Complete the solve function below.
    if(N%2==0)
    {
        if(N>=2 && N<=5)
        {
            cout<<"Not Weird";
        }
        else if(N>=6 && N<=20)
        {
            cout<<"Weird";
        }    
        else
        {
            cout<<"Not Weird";
        }
    }
    else
        cout<<"Weird";
    return 0;
}

Java

import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.regex.*;

public class Solution {



    private static final Scanner scanner = new Scanner(System.in);

    public static void main(String[] args) {
        int N = scanner.nextInt();
        scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
if(N%2==0)
        {
        if(N>=2 && N<=5)
            System.out.print("Not Weird");
        else if(N>=6 && N<=20)
            System.out.print("Weird");
            else
            System.out.print("Not Weird");
    }
    else
        System.out.print("Weird");
        scanner.close();
    }
}

Python

#!/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")  

Disclaimer: The above Problem (Day 3: Intro to Conditional Statements) is generated by Hacker Rank but the Solution is Provided by CodingBroz. This tutorial is only for Educational and Learning Purpose.

Leave a Comment

Your email address will not be published.