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!