For Loop Hackerrank Solution in C++. For Loop problem solution in HackerRank is similar to as we solve the previous problem. You can easily understand the full concept. In this problem, we have to take two inputs 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 the problem step by step. Hackerrank for loop solution C++.
A for loop is a programming language statement which allows code to be repeatedly executed. Sample input 8 11 sample output eight nine even odd.
The syntax is
for ( <expression_1> ; <expression_2> ; <expression_3> )
<statement>
- expression_1 is used for initializing variables which are generally used for controlling the terminating flag for the loop.
- expression_2 is used to check for the terminating condition. If this evaluates to false, then the loop is terminated.
- expression_3 is generally used to update the flags/variables.
A sample loop is
for(int i = 0; i < 10; i++) {
...
}
In this challenge, you will use a for loop to increment a variable through a range.
Input Format
You will be given two positive integers, a and b (a <= b), separated by a new line.
Output Format
For each integer in the inclusive interval [a, b]:
- If 1 <= n <= 9, then print the English representation of it in lowercase. That is "one" for 1, "two" for 2, and so on.
- Else if n > 9 and it is an even number, then print "even".
- Else if n > 9 and it is an odd number, then print "odd".
Sample Input
8
11
Sample Output
eight
nine
even
odd
Hint: No need to use many header files 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
For Loop Hackerrank Solution in C++
#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
/*For Loop Hackerrank Solution in C++*/
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;
}
Hackerrank C++ for Loop Solution
Take a string array and write a number from 1 to 9 in words and run the For loop starting from the first number and up to a number less than and equal to number 2.
String Array
string Arr1[9] = {"one", "two", "three", "four", "five", "six", "seven", "eight", "nine"};
For loop checking if the number is even or odd also prints the index of the 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 working of For Loop.
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 the number is Even and if the number is not divided by 2 then the number is Odd.
if (i % 2 == 0)
{
cout << "even" << endl;
}
else
{
cout << "odd" << endl;
Example:-
User Input
Number 1 = 8
Number 2 = 9
Sample Output
Eight
Nine
Even
Odd
0 Comments: