Hackerrank Conditional Statements Solution

Hackerrank Conditional Statements Solution

.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

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 denoting , do the following:

  • If , then print the lowercase English word corresponding to the number (e.g., one for , two for , etc.).
  • If , print Greater than 9.

Input Format

A single integer denoting .

Constraints.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

Output Format.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

If , then print the lowercase English word corresponding to the number (e.g., one for , two for , etc.); otherwise, print Greater than 9 instead.

Sample Input 05

Sample Output 0.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} five

Explanation 0.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

five is the English word for the number .

Sample Input 18

Sample Output 1.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} eight

Explanation 1.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

eight is the English word for the number .

Sample Input 244

Sample Output 2.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0} Greater than 9

Explanation 2.MathJax_SVG_LineBox {display: table!important} .MathJax_SVG_LineBox span {display: table-cell!important; width: 10000em!important; min-width: 0; max-width: none; padding: 0; border: 0; margin: 0}

is greater than , so we print Greater than 9.

Solution in cpp

Approach 1.

#include <iostream>
#include <cstdio>
using namespace std;

string num[] = {"zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "Greater than 9"};
int main() {
    // Complete the code.
    int n;
    std::cin >> n;
    
    if (n > 9) {
        n = 10;
    }
    std::cout << num[n] << std::endl;
    return 0;
}

Approach 2.

#include <stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    
    if(n==1)
        printf("one\n");
    else if(n==2)
        printf("two\n");
    else if(n==3)
        printf("three\n");
    else if(n==4)
        printf("four\n");
    else if(n==5)
        printf("five\n");
    else if(n==6)
        printf("six\n");
    else if(n==7)
        printf("seven\n");
    else if(n==8)
        printf("eight\n");
    else if(n==9)
        printf("nine\n");
    else 
        printf("Greater than 9\n");
        
    return 0;
}

Approach 3.

#include <iostream>
#include <cstdio>
using namespace std;

int main() {
    // Complete the code.
    int i;
    scanf("%d",&i);
    if(i==1){
        printf("one");
    } else if(i==2){
        printf("two");
    } else if(i==3){
        printf("three");
    } else if(i==4){
        printf("four");
    } else if(i==5){
        printf("five");
    } else if(i==6){
        printf("six");
    } else if(i==7){
        printf("seven");
    } else if(i==8){
        printf("eight");
    } else if(i==9){
        printf("nine");
    } else {
        printf("Greater than 9");
    }
    return 0;
}

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe