Day 3: Intro to Conditional Statements| 30 Days of Code Challange| Hackerrank | Dushyant Singh | Truth Power Info. |2022

Dushyant Singh
2 min readJul 6, 2022

--

Day 3: Intro to Conditional Statements

Code on Github: https://github.com/Dushyantsingh-ds/30-Days-of-Code-hackerrank

Objective

In this challenge, we learn about conditional statements. Check out the Tutorial tab for learning materials and an instructional video.

Task

Given an integer,n , 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
Complete the stub code provided in your editor to print whether or not n is weird.

Input Format

A single line containing a positive integer, n.

Constraints

1 <= n <= 100
Output Format
Print Weird if the number is weird; otherwise, print Not Weird.

Example

Sample Input 0

3

Sample Output 0

Weird

Sample Input 1

24

Sample Output 1

Not Weird

Explanation

Sample Case 0: n = 3
n is odd and odd numbers are weird, so we print Weird.

Sample Case 1: n = 24
n > 20 and n is even, so it is not weird. Thus, we print Not Weird.

Solution — Day 3: Intro to Conditional Statements solution

C++

using namespace std;string ltrim(const string &);
string rtrim(const string &);
int main()
{
string N_temp;
getline(cin, N_temp);
int N = stoi(ltrim(rtrim(N_temp))); 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;
}
string ltrim(const string &str) {
string s(str);
s.erase(
s.begin(),
find_if(s.begin(), s.end(), not1(ptr_fun<int, int>(isspace)))
);
return s;
}
string rtrim(const string &str) {
string s(str);
s.erase(
find_if(s.rbegin(), s.rend(), not1(ptr_fun<int, int>(isspace))).base(),
s.end()
);
return s;
}

Connect with me:

Medium: https://dushyantsingh-ds.medium.com/
Linkedin: https://linkedin.com/in/dushyantsingh-ds/
Instagram: https://www.instagram.com/dushyantsingh.ds/
Twitter: https://twitter.com/dushyantsingh_d
Facebook: https://www.facebook.com/dushyantsingh.india
Github: https://github.com/Dushyantsingh-ds
Telegram : https://t.me/dushyantsingh_d

--

--

Dushyant Singh
Dushyant Singh

Written by Dushyant Singh

Microsoft Certified Trainer | Cloud Architect | .Net Developer

No responses yet