3/14/2016

HackerRank solution for " For Loop " In C++

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 subdomain or For Loop  solution in c++ of hacker rank or introduction solutions hacker rank.


Logic:- For Loop problem solution in hackerRank 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 both numbers in words and also write number is even or odd. So without 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.


Submit your solution here:- Click here


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:

Post a Comment