# 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 = {"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.

Solution:-

#include <cmath>
#include <cstdio>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
int num1,num2;
cin >> num1 >> num2;
string Arr1 = {"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:-

Output on Local Editor

You May Like This:-

Previous Post
Next Post

#### post written by: Ghanendra Yadav

Hi, I’m Ghanendra Yadav, SEO Expert, Professional Blogger, Programmer, and UI Developer. Get a Solution of More Than 500+ Programming Problems, and Practice All Programs in C, C++, and Java Languages. Get a Competitive Website Solution also Ie. Hackerrank Solutions and Geeksforgeeks Solutions. If You Are Interested to Learn a C Programming Language and You Don't Have Experience in Any Programming, You Should Start with a C Programming Language, Read: List of Format Specifiers in C.

#### 1 comment:

1. int main()
{
int a, b;
scanf("%d\n%d", &a, &b);
for(int i = a;i <= b; i++){
if(i > 9){
if(i %2 == 0){
printf("even");
}
else{
printf("odd");
}
}
else 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");
}
printf("\n");
}

return 0;
}