Day 3: Conditional Statements - 30 Days Code - IndianTechnoEra
Latest update Android YouTube

Day 3: Conditional Statements - 30 Days Code

 Introduction:

Welcome to another coding challenge! In this task, we will explore conditional statements. Understanding how to use conditional statements is a crucial skill in programming, and this challenge provides an opportunity to practice these concepts. By the end of this challenge, you'll be able to determine whether a given integer is "Weird" or "Not Weird" based on specified conditions.


The Challenge:

The challenge is to evaluate a given integer, 

N, and 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."


Format & Sample:

Let's explore the expected format and review a sample to better understand the challenge.


Input Format:

A single line containing a positive integer, 

N.


Output Format:

Print "Weird" if the number is weird; otherwise, print "Not Weird."


Sample Input:

3


Sample Output:

Weird


Explanation:

Sample Case:

N is odd, and odd numbers are weird, so we print "Weird."


Code Breakdown:

Now, let's break down the provided code and understand each section.


Reading Input:

string N_temp;

getline(cin, N_temp);


int N = stoi(ltrim(rtrim(N_temp)));

In this section, we read the input value for 

N from the standard input.


Printing Output:

if(N%2 != 0){

    cout << "Weird";

}

else{

    if(N >= 2 && N <= 5){

        cout << "Not Weird";

    }

    else if(N >= 6 && N <= 20){

        cout << "Weird";

    }

    else{

        cout << "Not Weird";

    }

}

This part of the code contains the logic for determining whether 

N is "Weird" or "Not Weird" based on the specified conditions. It uses nested if-else statements to cover the different cases.


Final Code:

#include <bits/stdc++.h>

using namespace std;

int main() {
    string N_temp;
    getline(cin, N_temp);

    int N = stoi(N_temp);

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

    return 0;
}


This code efficiently determines whether a given integer is "Weird" or "Not Weird" based on the specified conditions. It showcases the usage of conditional statements in programming. Feel free to experiment with different inputs to further solidify your understanding. Happy coding!





إرسال تعليق

Feel free to ask your query...
Cookie Consent
We serve cookies on this site to analyze traffic, remember your preferences, and optimize your experience.
Oops!
It seems there is something wrong with your internet connection. Please connect to the internet and start browsing again.
AdBlock Detected!
We have detected that you are using adblocking plugin in your browser.
The revenue we earn by the advertisements is used to manage this website, we request you to whitelist our website in your adblocking plugin.
Site is Blocked
Sorry! This site is not available in your country.