Monday, 14 March 2016

Hacker Rank solution for Conditional Statements

Problem:- Hacker Rank Solution Program In C++ For " Conditional Statements " or Hacker Rank Solution Program In C++ For Conditional Statements or Conditional Statements solution hacker rank or Hacker rank solution for c++ domain or Hacker rank solution for Conditional Statements sub domain or Conditional Statements solution in c++ of hacker rank or introduction solutions hacker rank.

Check Here:- Geeksforgeeks solution for School, Basic, Easy, Medium, Hard in C++.

Logic:- So for this problem, we have to take a strong array and as per requirement size of the array is 10, then put a condition and take a user input if number is given by user is 1 to 9 then print that number in words and if the number is greater than 9 then 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 size of 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 " and if the number is between 1 to 9 then print the number in words ex if the number is 5 then program will print "five".

Conditions

 if(n > 9)
    {
        cout << num[0]; // if number is greater than 9 so first index of array will we 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

Optional Hint:- No need to use a huge number of the header file in the program this problem can be solved by using only one header file #include <iostream> or if you want to use only one header file you can use the Master Header file. Master header in C++ is #include<bits/stdc++.h> after adding this header file no need to add any other header file.

Also Check- Hacker rank solution for Strings, Classes, STL, Inheritance in C++.

Copy the full solution or colored code and paste into the hacker rank editor and click to Run Code if the code runs successfully then click to Submit Code. Before pasting the code into the editor make sure you have chosen C++ or if C++ not worked fine then change it into C++14 an editor in the top right drop-down option.

Submit your solution here:- Click here

Tip:- Try to solve this problem using Switch Case. Before copy the program I recommended please read this full article, this will help you to build your own logic. 


If you have any query feel free to share with me, and if you like my work please share, comments and subscribe for our new and latest post related to programming this will help you update all about new post this Article.

Solution:-

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <ctime>
#include <deque>
#include <queue>
#include <stack>
#include <string>
#include <bitset>
#include <cstdio>
#include <limits>
#include <vector>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <fstream>
#include <numeric>
#include <sstream>
#include <iostream>
#include <algorithm>
#include <unordered_map>

using namespace std;

int main()
{
    int n;
    cin >> n;
    string num[10] = {"Greater than 9", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};

    cin >> n;

    if(n > 9)
    {
        cout << num[0];
    }
    else
    {
        cout << num[n];
    }

    return 0;
}

Output:


Hacker Rank solution  for Conditional Statements


Hacker Rank solution  for Conditional Statements

You May Like This:-

1. Hacker Rank Solution for 30 Days of Code

2. Hacker Rank solution for Attribute Parser

3. Java Program For Find The Gross Salary Of An Employee

4. Hacker Rank Solution For Conditional Statements

5. Hacker Rank Solution For Mini-Max Sum

6. Hacker Rank Solution For Birthday Cake Candles

7. Geeksforgeeks Solution For " Sum of Middle Elements of two sorted arrays "

8. Java Program For Converting Temperature Celsius Into Fahrenheit

9. Geeksforgeeks Solution For " Two Repeated Elements "

10. Hacker Rank Solution For Virtual Functions

No comments:
Write comments

Recommended Posts × +