Day 3: Intro to Conditional Statements|| hackerrank || 30 days of cod || programming_info

Objective
In this challenge, we're getting started with conditional statements. Check out the Tutorial tab for learning materials and an instructional video!

Task
Given an integer, , perform the following conditional actions:

  • 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

Complete the stub code provided in your editor to print whether or not  is weird.

Input Format

A single line containing a positive integer, .

Constraints

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: 
 is odd and odd numbers are weird, so we print Weird.

Sample Case 1: 
 and  is even, so it isn't weird. Thus, we print Not Weird.

Now here's the solution in C/C++/JAVA:-

code snippet:
1.using c++ language:

 int main(){
    int n;
    cin >> n;
    if ((n & 1) || (6 <= n && n <= 20))
        cout << "Weird" << endl;
    else
        cout << "Not Weird" << endl;
    return 0
alternate solution:-

#include <bits/stdc++.h>

using namespace std;

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

2.using java language:

import java.util.*;
public class Solution {   
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(); 
        scan.close();
        System.out.println( ((n & 1) == 1 || (6 <= n && n <= 20)) ? "Weird" : "Not Weird");
    }
}
alternate solution:-

import java.util.*;
public class Solution {
    private static String weird = "Weird";
    private static String notWeird = "Not Weird";
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        int n = scan.nextInt(); 
        scan.close();
        String ans="";

        // if 'n' is NOT evenly divisible by 2 (i.e.: n is odd)
        if(n%2==1){
            ans = weird;
        }
        // 'n' must be even; proceed if 'n' >= 2
        else if(n >= 2){ 
            if(n > 20){ 
                ans = notWeird;
            }
            else if(n >= 6){ 
                ans = weird;
            }
            else if(n >= 2){
                ans = notWeird;
            }
        }
        System.out.println(ans);
    }
}

3.using c language:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 int main()
{
    int N;
    scanf("%d",&N);
    if(N%2==0)
        {
            if(2<=N && N<5)
                {
                printf("Not Weird");
                }
            else if(6<=N && N<=20)
                {
                    printf("Weird");
                }
            else if(N>20)
                printf("Not Weird");
        }
    else
    printf("Weird");
    return 0;
}

Share:

No comments:

Post a Comment

Translate

Recommended platforms

  1. codechef
  2. hackerrank
  3. codeforces
  4. leetcode
  5. hackerearth

Popular Posts

programming_info. Powered by Blogger.

Blog Archive

Recent Posts

other platforms

  • geeks for geeks
  • w3schools
  • codepen
  • skillshare
  • udemy

Pages

reader support Support