Monday, 14 March 2016

Hacker Rank solution for " For Loop "

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

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

Logic:- For Loop problem solution in hacker Rank is similar to as we solve the previous problem, I recommended before solve to this problem see Conditional Statement problem so you can easily understand the full concept. In this problem, we have to take a two input and we have to write the both number in words and also write number is even or odd. So with out wasting our time take an example and try to solve problem step by step.

Explanation:- take a string array and write a number from 1 to 9 in words and run the For loop start from the first number and up to number less than and equal to number 2.

String Array

string Arr1[9] = {"one","two","three","four","five","six","seven","eight","nine"};

For loop for checking if the number is even or odd also print the index of string array so we can easily convert number integer to number in the word below is the syntax of For Loop so you can easily understand the problem and be working of For Loop.

Syntax of For Loop

for(initilization ; condition ; increment/ decreament)
{
statement .......1
statement .......2
statement .......3
statement .......4
statement .......5
}

for(int i =num1; i <= num2;i++)
{
if(i <= 9)
{
cout << Arr1[i-1] << endl;
}

As we know that if the number is divided by 2 then number is Even and if the number is not divided by 2 then number is Odd.

if(i%2 ==0)
{
cout << "even" << endl;
}
else
{
cout << "odd" << endl;

Example:-

User input 

Number 1 = 8
Number 2 = 9

Output

Eight
Nine
Even

Odd

Optional Hint:- No need to use a huge number of the header file 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 <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num1,num2;
cin >> num1 >> num2;
string Arr1[9] = {"one","two","three","four","five","six","seven","eight","nine"};
for(int i =num1; i <= num2;i++)
{
if(i <= 9)
{
cout << Arr1[i-1] << endl;
}
else
{
if(i%2 ==0)
{
cout << "even" << endl;
}
else
{
cout << "odd" << endl;
}
}
}
return 0;
}

Output:-


Hacker Rank solution for " For Loop "

Output on Local Editor

Hacker Rank solution for " For Loop "


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. C++ Program For School Management System ( SMS Project ) With Source Code

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 × +