Conditional Statements in Cpp - Hackerrank Solution

 

Conditional Statements


Objective:

if and else are two of the most frequently used conditionals in C/C++, and they enable you to execute zero or one conditional statement among many such dependent conditional statements. We use them in the following ways:

if: This executes the body of bracketed code starting with if evaluates to true.

if (condition) {
statement1;
...
}
if - else: This executes the body of bracketed code starting with if evaluates to true, or it executes the body of code starting with if evaluates to false. Note that only one of the bracketed code sections will ever be executed.

if (condition) {
statement1;
...
}
else {
statement2;
...
}
if - else if - else: In this structure, dependent statements are chained together and the for each statement is only checked if all prior conditions in the chain evaluated to false. Once a evaluates to true, the bracketed code associated with that statement is executed and the program then skips to the end of the chain of statements and continues executing. If each in the chain evaluates to false, then the body of bracketed code in the else block at the end is executed.

if(first condition) {
...
}
else if(second condition) {
...
}
.
.
.
else if((n-1)'th condition) {
....
}
else {
...
}
Given a positive integer n, do the following:

If 1<=n<=9, print the lowercase English word corresponding to the number (e.g., one for , two for , etc.).
If n>9, print Greater than 9.


Solution:

#include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; cin.ignore(numeric_limits<streamsize>::max(), '\n'); switch(n){ case 1: cout << "one"<<endl; break; case 2: cout << "two"<<endl; break; case 3: cout << "three"<<endl; break; case 4: cout << "four"<<endl; break; case 5: cout << "five"<<endl; break; case 6: cout << "six"<<endl; break; case 7: cout << "seven"<<endl; break; case 8: cout << "eight"<<endl; break; case 9: cout << "nine"<<endl; break; default: cout << "Greater than 9"<<endl; } return 0; }



Note:

This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.

Disclaimer:-

The above hole problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.

Conditional Statements in Cpp - Hackerrank Solution Conditional Statements in Cpp - Hackerrank Solution Reviewed by CodexRitik on October 16, 2020 Rating: 5

No comments:

Powered by Blogger.