Saturday, 25 March 2017

Hackerrank Solution For Day 3: Intro to Conditional Statements

Problem:- Write a Hackerrank Solution For Day 3: Intro to Conditional Statements or Hacker Rank Solution Program In C++ For " Day 3: Intro to Conditional Statements " or Hackerrank 30 days of code Java Solution: Day 3 Intro to Conditional Statements or Hackerrank solution for 30 Days of Code Challenges.

Hackerrank Solution For Day 3: Intro to Conditional Statements

Task:- According to Hacker Rank program should follow these four given conditions.

1. If n is odd, print Weird.
2. If n is even and in the inclusive range of 2 to 5, print Not Weird.
3. If n is even and in the inclusive range of 6 to 20, print Weird.
4. If n is even and greater than 20, print Not Weird.

Logic:- Let's start, so we have a number n and we need to write a program that follows the above four condition as we can see that our first condition is if n is odd that program will print number "Weird" so for this first condition we divide a number by 2 if a number is divisible by 2 then number is even and if number is not divisible by 2 then it will print number is "Weird'. Now come to the second condition if the number is even and range of 2 to 5 the program will print number "Not Weird", for that again each number we divide by 2 if number is even and number is between range(2 to 5) the program will print Number is "Not  Weird". Now comes to the third condition if the number is between 6 to 20 then program will print number is " Weird". same as the second condition. Again check for the fourth condition if the number is greater than 20 then the program will print number is " Weird".

If you want to 30 days solution from Day 0 please check the below link. And also you can find more program in below of this post. 

1. Hackerrank Solution For Day 0: Hello, World.

2. Hackerrank Solution For Day 1: Data Types. 

3. Hackerrank Solution For Day 2: Operators.

Copy the colored code and paste it into hacker rank editor. If you have any query feel free to share with me, and if you like my work please share this Article.

Submit Your Solution Here:- Click Here

Solution:-

#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>=2 && N<=5)
            cout<<"Not Weird";
        else if(N>=6 && N<=20)
            cout<<"Weird";
            else
            cout<<"Not Weird";
    }
    else
        cout<<"Weird";

    return 0;
}

Output:-


Hackerrank Solution For Day 3: Intro to Conditional Statements

You May Also Like:-









9. Hackerrank Solution For Day 12: Inheritance

10. Hackerrank Solution For Day 13: Abstract Classes

No comments:
Write comments

Recommended Posts × +