Home30 Days Of CodeHackerrank Day 3 : Intro to conditional statements solution

Hackerrank Day 3 : Intro to conditional statements solution

In this Hackerrank Day 3 : Intro to conditional statements solution , we learn about conditional statements.

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.
  • 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.

Hackerrank Day 3 : Intro to conditional statements solution

Hackerrank Day 3 : Intro to conditional statements solution

Intro to conditional statements solution in C

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

int main(){
    int N; 
    scanf("%d",&N);
    if ((N % 2) != 0) {
        printf("Weird\n");
    } else if (N >= 2 && N <= 5) {
        printf("Not Weird\n");
    } else if (N >= 6 && N <= 20) {
        printf("Weird\n");
    } else {
        printf("Not Weird\n");
    }
    return 0;
}

Intro to conditional statements solution in C ++

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
using namespace std;


int main(){
    int N;
    cin >> N;
    if(N%2==0){
        if(N>20){
            cout<<"Not Weird";
        }
        if(N>=2 && N<=5){
            cout<<"Not Weird";
        }
        if(N>=6 && N<=20){
            cout<<"Weird";
        }
    }else{
        cout<<"Weird";
    }
    return 0;
}

Intro to conditional statements solution in Java

import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
   
    public static void main(String[] args) {
       Scanner sc=new Scanner(System.in);
       int n=sc.nextInt();            
       String ans="";
       if(n%2==1){
           ans = "Weird";
       }
           else{
               if(n>2 && n<=5){
                   ans = "Not Weird";
               }
               if(n>5 && n<=20){
                   ans = "Weird";
               }
               else{
                   ans = "Not Weird";
               }
            }
        System.out.println(ans);
    }
}

Intro to conditional statements solution in Python 3

#!/bin/python3

import math
import os
import random
import re
import sys



if __name__ == '__main__':
    N = int(input())

    if N%2 != 0:
        print("Weird")
    else:
        if N>=2 and N<5:
            print("Not Weird")
    
        if N>=6 and N<=20:
            print("Weird")
    
        if N>20:
            print("Not Weird")

Intro to conditional statements solution in Python 3

process.stdin.resume();
process.stdin.setEncoding('ascii');

var input_stdin = "";
var input_stdin_array = "";
var input_currentline = 0;

process.stdin.on('data', function (data) {
    input_stdin += data;
});

process.stdin.on('end', function () {
    input_stdin_array = input_stdin.split("\n");
    main();    
});

function readLine() {
    return input_stdin_array[input_currentline++];
}

/////////////// ignore above this line ////////////////////

function main() {
    var N = parseInt(readLine());
    if (N % 2 === 0) {
        if (N >= 2 && N <= 5) {
            console.log("Not Weird");
        } else if (N >= 6 && N <= 20) {
            console.log("Weird");
        } else if (N > 20) {
            console.log("Not Weird");
        }
    } else if (N % 2 === 1) {
        console.log("Weird");
    }
}

NEXT : Hackerrank Day 4 : Class vs. Instance

30 Days of Code HackerRank Solutions List – Day 0 to Day 29


Read More –

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

- Advertisment -

Categories