Conditional Statements in C++ Hackerrank Solution. if and else are two of the most frequently used conditionals in C/C++, & 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 the statement1 if the condition evaluates to true.
if (condition) {
statement1;
...
}
if - else: This executes the body of bracketed code starting with a statement1 if the condition evaluates to true, or it executes the body of code starting with statement2 if the condition 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 condition for each statement is only checked if all prior conditions in the chain are evaluated as false. Once a condition 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 condition 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 1, two for 2, etc.).
If n > 9, print Greater than 9.
Input Format
A single integer, n.
Constraints
1 <= n <= 10^9
Output Format
If 1 <= n <= 9, then print the lowercase English word corresponding to the number (e.g., one for 1, two for 2, etc.); otherwise, print Greater than 9.
Sample Input
5
Sample Output
five
Submit your solution here: Click here
Conditional Statements in C++ Hackerrank Solution
#include <bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin >> n;
if (n == 1)
{
cout << "one";
}
else if (n == 2)
{
cout << "two";
}
else if (n == 3)
{
cout << "three";
}
else if (n == 4)
{
cout << "four";
}
else if (n == 5)
{
cout << "five";
}
else if (n == 6)
{
cout << "six";
}
else if (n == 7)
{
cout << "seven";
}
else if (n == 8)
{
cout << "eight";
}
else if (n == 9)
{
cout << "nine";
}
else
{
cout << "Greater than 9";
}
return 0;
}
The Output of Conditional Statements Hackerrank Solution
Conditional Statements Logic
So for this problem, we have to take a strong array and as per the required size of the array is 10, then put a condition and take a user input if the number given by the user is 1 to 9 then print that number in words and if the number is greater than 9 then the program should print the number is greater than 9. let take an example so we can easily understand the full problem easily. see the below explanation of the problem.
Explanation:- First declare the string array like below and the size of the array should be 10 cause our requirement is for only 10 indexes,
String Array
string num[10] = {"Greater than 9", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
Now use the if else condition, if the number is greater than 9 it should print the message " Greater than 9 " If the number is between 1 to 9 then print the number in words ex if the number is 5 then the program will print "five".
Conditions
if(n > 9)
{
cout << num[0]; // if the number is greater than 9 so first index of the array will be printed.
}
else
{
cout << num[n]; // index message will be printed.
}
Example
1. First User Enter number = 1
Output = first
2. Second User Enter number = 9
Output = nine
3. Third User Enter number = 10
Output = Greater than 9
Explanation:- First declare the string array like below and the size of the array should be 10 cause our requirement is for only 10 indexes,
String Array
string num[10] = {"Greater than 9", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
Now use the if else condition, if the number is greater than 9 it should print the message " Greater than 9 " If the number is between 1 to 9 then print the number in words ex if the number is 5 then the program will print "five".
Conditions
if(n > 9)
{
cout << num[0]; // if the number is greater than 9 so first index of the array will be printed.
}
else
{
cout << num[n]; // index message will be printed.
}
Example
1. First User Enter number = 1
Output = first
2. Second User Enter number = 9
Output = nine
3. Third User Enter number = 10
Output = Greater than 9
Similar to Conditional Statements
- Reverse a Linked List Hackerrank Solution
- Input And Output Hackerrank Solution in C++
- Solve Me First Hackerrank Solution in C & C++
- A Very Big Sum Hackerrank Solution C++
- Deque STL Hackerrank Solution in C++
- Mini Max Sum Hackerrank Solution in C++
- Staircase Hackerrank Solution in C
- Attribute Parser Hackerrank Solution in C++
- Inheritance Introduction Hackerrank Solution in C++
- StringStream Hackerrank Solution in C++
- Attribute Parser Hackerrank Solution in C++
- Basic Data Types HackerRank Solution in C++
0 Comments: